User Tools

Convert Lead Webservice

Usage example of convert lead web service 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 the “Leads” module. Ensure that the Lead ID passed to vtws_convertleads function is in 'x' format.
  • Following example uses standard vtwsclib library.
  • Sample code:
    function vtws_login(){
            $$url = '';
    	$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']['accountname'] = $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_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;
                    return 'Login failed';
    $d = '2x21022';
    echo vtws_convertleads($d);
Purpose:Permits the conversion of a lead into an account, contact and potential. This service is used by the application itself when converting leads.
Profile:convertlead($entityvalues): Map
Send as:POST
 id: $entityvalues is an array. Consult the modules/Leads/LeadConvertToEntities.php to see an example.
Returns:Array with all the information of the new objects created
URL Format:
http://vtiger_url/webservice.php?operation=deleteUser&sessionName=[session id]

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 web service 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 behavior 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