User Tools


Convert Lead Webservice

Usage example of convert lead webservice in order to convert an existing Leads into Accounts, Contacts & Opportunity using Webservice.

Important points to note before using the following code:

  • Check table vtiger_ws_entity to know the “id” (Webservice module ID) of “Leads” module. Ensure that the Lead ID passed to vtws_convertleads function is in 'x' format.
  • Modify “vtiger_ws_operation_parameters” table as showen below to reflect right parameters for “convertlead” Webservice call.
DELETE FROM vtiger_ws_operation_parameters WHERE operationid = (SELECT operationid FROM vtiger_ws_operation WHERE name = 'convertlead');
INSERT INTO vtiger_ws_operation_parameters (operationid, name, TYPE, SEQUENCE) VALUES ((SELECT operationid FROM vtiger_ws_operation WHERE name = 'convertlead'), 'element', 'encoded', '1');

We are making the change in the table vtiger_ws_operation_parameters because parameters required in vtws_convertlead function in include/Webservices/vtws_convertlead.php has changed in vtiger crm 5.4 compared to earlier versions. But corresponding change in vtiger_ws_operation_parameters has not been made.

This change is not required in coreBOS where we have already applied it.

  • Following example uses standard vtwsclib library.
  • Sample code:
    <?php
    include_once('vtwsclib/Vtiger/WSClient.php');
    
    function vtws_login(){
            $$url = 'http://en.vtiger.com/wip';
    	$client = new Vtiger_WSClient($url);
    	$login = $client->doLogin('admin', 'KpS9EjNz16JtPmoe');
            if(!$login) return 0;
            return $client;
    }
    
    function vtws_convertleads($recordid){
            $client = vtws_login();
            if ($client <> 0){
                    $recordInfo = $client->doRetrieve($recordid);
                    $wasError= $client->lastError();
                    if($wasError) {
                            return $wasError['code'] . ':' . $wasError['message'];
                    }
                    if($recordInfo) {
                            $client1 = vtws_login();
                            $convert_lead_array = array();
                            $convert_lead_array['leadId'] = $recordInfo['id'];
                            $convert_lead_array['assignedTo'] = $recordInfo['assigned_user_id'];
                            $convert_lead_array['entities']['Accounts']['create']=true;
                            $convert_lead_array['entities']['Accounts']['name']='Accounts';
                            $convert_lead_array['entities']['Accounts']['accountname'] = $recordInfo['company'];
                            $convert_lead_array['entities']['Accounts']['industry']=$recordInfo['industry'];
                            $convert_lead_array['entities']['Potentials']['create']=true;
                            $convert_lead_array['entities']['Potentials']['name']='Potentials';
                            $convert_lead_array['entities']['Potentials']['potentialname']=$recordInfo['company'];
                            $convert_lead_array['entities']['Potentials']['closingdate']= date("Y-m-d", strtotime("+1 week Saturday"));
                            $convert_lead_array['entities']['Potentials']['sales_stage']= 'Prospecting';
                            $convert_lead_array['entities']['Potentials']['amount']= 0;
                            $convert_lead_array['entities']['Contacts']['create']=true;
                            $convert_lead_array['entities']['Contacts']['name']='Contacts';
                            $convert_lead_array['entities']['Contacts']['lastname']=$recordInfo['lastname'];
                            $convert_lead_array['entities']['Contacts']['firstname']=$recordInfo['firstname'];
                            $convert_lead_array['entities']['Contacts']['email']=$recordInfo['email'];
                            $convert_lead_json = json_encode($convert_lead_array);
                            $response = $client1->doInvoke('convertlead', array('element'=>$convert_lead_json));
                            $wasError = $client1->lastError();
                            if($wasError) {
                                    return $wasError['code'] . ':' . $wasError['message'];
                            } else {
                                    return 1;
                            }
                    }
            }else{
                    return 'Login failed';
            }
    }
    
    $d = '2x21022';
    echo vtws_convertleads($d);
    
    ?>

Thanks Ashutosh

By setting $convert_lead_array['entities'][Contacts|Accounts|Potential]['create'] to false you can avoid creating a record in that module.

By default, a Lead is Converted into a Contact, but you can indicate the module you want to have the Lead values transferred to using the variable transferRelatedRecordsTo

$convert_lead_array['entities']['transferRelatedRecordsTo'] = 'Accounts';

If you indicate that no Contact should be created the Lead values will be transferred to the Account.

If you indicate that neither a Contact nor an Account should be created the process will fail with a webservice exception.

By default the process will search for an existing Account with the same company name and relate the new records with this Account. This is done to avoid duplicates. You can override this behaviour by assigning a value to the meta-variable forcecreate like this:

$convert_lead_array['entities'][Accounts]['forcecreate'] = true;

Be careful as this will easily create duplicate account names and you won't be able to edit them unless you set the global variable Accounts_BlockDuplicateName to false.


coreBOS Documentación