function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bobby7bobby7 

I have a requirment where i am inserting a lead with status equals 'Full submission'. Now When the lead is created i want to insert a Account and also a Opportunity With some field mappings Using Trigger in both Isert and Update conditions.

Here is my code to map the fields and insert a Account and also opportunity whenever the status is Full Submission on lead. The lead should be automatically converted .
Here is my code .

trigger TriggerToConvertLEadAndCreateActAndOppo on Lead (after insert, after update) {
list<Account> lstAccountUpdate = new list<Account>();
list<Opportunity> lstOppUpdate = new list<Opportunity>();
set<id> Accountids = new set<id>();
if( trigger.isInsert){
  for(Lead Objlead : trigger.new){
   Account objacc;
   Opportunity opp;
   Database.LeadConvertResult lcr;
   list<LeadStatus> lstconvertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true limit 1];
    if((Objlead.isConverted == false)&& (Objlead.Status == 'Full Submission'))
    {
      Database.LeadConvert lc = new database.LeadConvert();
       lc.setLeadId(Objlead.Id);
       //lc.setConvertedStatus('Converted');
       string strstatus='';
       if(lstconvertStatus != null && lstconvertStatus.size()>0 ){
               strstatus = lstconvertStatus[0].MasterLabel ;
           }
           lc.setConvertedStatus(strstatus);
           lcr = Database.convertLead(lc);
           Accountids.add(lcr.getAccountId());
         system.debug(':::::strstatus'+strstatus);
       system.debug(':::::lc'+lc);
       system.debug(':::::Accountids'+Accountids);
    }
    system.debug(':::::::::::Objlead.isConverted'+Objlead.isConverted);
    if((Objlead.isConverted)){
                 objacc = new Account(id=lcr.getAccountId(),
                                         Phone = Objlead.MobilePhone,
                                         Name = Objlead.Company);
    }
    lstAccountUpdate.add(objacc);
    system.debug(':::::lstAccountUpdate'+lstAccountUpdate);
  }
  if(lstAccountUpdate != null && lstAccountUpdate.size()>0)
         update lstAccountUpdate;
}

if(trigger.isUpdate ){
  for(Lead objL:trigger.new){
   Account objacc;
   if(objL.Status == 'Full Submission' && trigger.oldMap.get(objL.id).Status != trigger.newmap.get(objL.id).Status && trigger.oldmap.get(ObjL.Id).Status != 'Full Submission'){
    Database.LeadConvert lc = new Database.Leadconvert();
    lc.setLeadId(objL.id);
    lc.setConvertedStatus('Converted');
    if((objL.isConverted && objL.ConvertedAccountId != null)){
                 objacc = new Account(id=objL.ConvertedAccountId,
                                         Phone = objL.MobilePhone,
                                         Name = objL.Name);
    }
    lstAccountUpdate.add(objacc);
   }
  }
  if(lstAccountUpdate != null && lstAccountUpdate.size()>0)
         update lstAccountUpdate;
}
}

I am not entering into this snippet Where i am mapping some fields from lead to account. Here is the snippet.

system.debug(':::::::::::Objlead.isConverted'+Objlead.isConverted);
if((Objlead.isConverted)){
                 objacc = new Account(id=lcr.getAccountId(),
                                         Phone = Objlead.MobilePhone,
                                         Name = Objlead.Company);
    }

The above debug is always showing me as false. But i am converting the lead above this. Can you get me some working code.
magicforce9magicforce9
Hi,

I've refined your trigger code....Your code was not bulkified and there were a lot of redundant statements...

Hopefully it should work, let me know if you have any problems..

trigger TriggerToConvertLEadAndCreateActAndOppo on Lead (after insert, after update) {
  
	list<Account> lstAccountUpdate = new list<Account>();
	list<Opportunity> lstOppUpdate = new list<Opportunity>();
	list<Database.LeadConvertResult> convertedResult = new list<Database.LeadConvertResult>();
	list<Database.LeadConvert> leadsToBeConverted = new list<Database.LeadConvert>();
	string lstconvertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true limit 1].MasterLabel;
	for(Lead objL : trigger.new)
		if(objL.Status == 'Full Submission' && trigger.isInsert || (trigger.oldMap.get(objL.id).Status != trigger.newmap.get(objL.id).Status && trigger.oldmap.get(ObjL.Id).Status != 'Full Submission'))
			leadsToBeConverted.add(new database.LeadConvert(leadId = objL.id, convertedStatus = lstconvertStatus));
	if(!leadsToBeConverted.isEmpty()){
		/* Database.convertLead will only accepts upto 100 LeadConvert records(See the link below)
		https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_database.htm#apex_System_Database_convertLead_2
		*/
		if(leadsToBeConverted.size() > 100)
		{	
			integer actualSize = leadsToBeConverted.size();
			integer batchSize = 100;
			for (integer i = 0; i < actualSize; i += batchSize){
				list<Database.LeadConvert> leadsToBeConvertedBatch = new list<Database.LeadConvert>();
				for(Integer j = 0; j < 100 ; j++){
					integer k = i + j;
					if(k < actualSize)
						leadsToBeConvertedBatch.add(leadsToBeConverted[k]);
				}
				convertedResult.addAll(Database.convertLead(leadsToBeConvertedBatch, false));
				leadsToBeConvertedBatch.clear();
			}
		}	
		else
			convertedResult = Database.convertLead(leadsToBeConverted, false);
	}

	//Updating the mapping fields on converted account...for which you used a snippet of code
	for(Database.LeadConvertResult result : convertedResult)
		if(result.isSuccess()){
			Account acc = new Account(id = result.getAccountId(), Phone = trigger.newMap.get(result.getLeadId()).MobilePhone, Name = trigger.newMap.get(result.getLeadId()).Company);
			lstAccountUpdate(acc);
			//Similarly you can update any mapping fields into the converted Opportunity using the above syntax
		}
	if(!lstAccountUpdate.isEmpty()) update lstAccountUpdate;
}