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
ABC XYZ 39ABC XYZ 39 

Help in Create and update trigger logic

HI,
I am writing logic for the scenarios: When a lead is created, few of the fields are written to a
custom object. When the same less record is updated, I want to push the record into the same custom object.
The problem: with creation of a record, fields a,b, c are  mandatory to save the record. These would be pushed.
Now, when the same record is updated, details are to be sent. Issue is with every creation scenario, even the update trigger is getting fired- there are 2 sets of records. I have tried to use flags but it didn't help. Eg: 
if(flag is true or 'd' field is not updated), update lead. This doesn't work. Please help. Thanks .
Best Answer chosen by ABC XYZ 39
mritzimritzi
I am sure you have a lookup/Mater-detail field on the custom object to link it with a unique Lead record.
If you requirement is to only update few fields on a custom object based on a particular Lead record, i will highly recommend you to use Process builder, if super easy to setup.

Find help here:
https://developer.salesforce.com/trailhead/en/business_process_automation/process_builder

If you need to write trigger for this, it would be like this:
(write field API names as per your requirement, correct typos & syntax errors if any)
trigger populateChildObject on Lead(after insert, after update){
	// to create new child record whenever a new lead is created
	if(Trigger.isAfter && Trigger.isInsert){
		List<customObject__c> objList = new List<customObject__c>();
		for(Lead l:Trigger.new){
			customObject__c tmpObj = new customObject__c();
			tmpObj.fieldA__c = l.fieldA;
			tmpObj.lookupField__c = l.id;
			// copy other required fields as well;
			objList.add(tmpObj);
		}
		insert objList;
	}
	// to update existing child object record based on new values on lead object record
	else if(Trigger.isAfter && Trigger.isUpdate){
		//get map of lead having list records of their respective childs
		//considering the fact that one lead may have several childs.
		// even in case on single child, the lofic would work fine
		Map<Id,Lead> leadMap = new Map<Id,Lead>([Select id, allOtherLeadFields, (Select id, allOtherCustomObjFields
									From CustomObj__c) From Lead Where id IN:Trigger.New]);
		List<customObject__c> objList = new List<customObject__c>();
		for(Lead l:Trigger.new){
			List<customObject__c> tmpList = new List<customObject__c>();
			// get list of customObject__c records where id of their parent lead matches current lead's id
			tmpList = leadMap.get(l.Id);
			for(customObject__c c:tmpList){
				//update or  populate fields on customObject__c based on your update logic
				// e.g: c.fieldD__c = l.fieldD__c;
			}
			//add all entries of tmpList in objList (objList will be finally updated)
			objList.addAll(tmpList);
		}
		update objList;
	}
}

If this helps you out, please mark it as Best Answer
 

All Answers

mritzimritzi
I am sure you have a lookup/Mater-detail field on the custom object to link it with a unique Lead record.
If you requirement is to only update few fields on a custom object based on a particular Lead record, i will highly recommend you to use Process builder, if super easy to setup.

Find help here:
https://developer.salesforce.com/trailhead/en/business_process_automation/process_builder

If you need to write trigger for this, it would be like this:
(write field API names as per your requirement, correct typos & syntax errors if any)
trigger populateChildObject on Lead(after insert, after update){
	// to create new child record whenever a new lead is created
	if(Trigger.isAfter && Trigger.isInsert){
		List<customObject__c> objList = new List<customObject__c>();
		for(Lead l:Trigger.new){
			customObject__c tmpObj = new customObject__c();
			tmpObj.fieldA__c = l.fieldA;
			tmpObj.lookupField__c = l.id;
			// copy other required fields as well;
			objList.add(tmpObj);
		}
		insert objList;
	}
	// to update existing child object record based on new values on lead object record
	else if(Trigger.isAfter && Trigger.isUpdate){
		//get map of lead having list records of their respective childs
		//considering the fact that one lead may have several childs.
		// even in case on single child, the lofic would work fine
		Map<Id,Lead> leadMap = new Map<Id,Lead>([Select id, allOtherLeadFields, (Select id, allOtherCustomObjFields
									From CustomObj__c) From Lead Where id IN:Trigger.New]);
		List<customObject__c> objList = new List<customObject__c>();
		for(Lead l:Trigger.new){
			List<customObject__c> tmpList = new List<customObject__c>();
			// get list of customObject__c records where id of their parent lead matches current lead's id
			tmpList = leadMap.get(l.Id);
			for(customObject__c c:tmpList){
				//update or  populate fields on customObject__c based on your update logic
				// e.g: c.fieldD__c = l.fieldD__c;
			}
			//add all entries of tmpList in objList (objList will be finally updated)
			objList.addAll(tmpList);
		}
		update objList;
	}
}

If this helps you out, please mark it as Best Answer
 
This was selected as the best answer
ABC XYZ 39ABC XYZ 39
Hey, thanks for your response. 

I have written a custom controller to handle this. I am calling afterinsert trigger for new lead created and afterupdate trigger for any subsequent updates.

New Lead : Lead Created with A,B and C mandatory fields created & optional fields unpopulated (Include 'DD' id field)--> Records pushed to the custom object


Lead Update :  Only When Lead Updated with changes in a specific section : 'X' with fields 'AA' , 'BB','CC' populated  --> Records pushed to the custom object  (OR) When lead is updated with optional fields populated with 'DD' id field blank)

Now when a lead is created with A,B,C, and 'DD' unfilled --->  2 set of Records pushed to custom object one for lead creation scenario
and another for lead updation (Becase 'DD' field is blank)   ---> I want to stop this behavior.  Can you please clarify whether you have handled the same scenario above?