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
HelloSanHelloSan 

i need an apex trigger code when parent object opportunity object field is updated than the custom object fields need to be updated

Best Answer chosen by HelloSan
ManojjenaManojjena
Hi San,

Please check below code .
trigger createAndUpdate  on Opportunity(after insert,after update){
	List<Service__c lstservice=new List<Service__c>();
	Map<id,Opportunity> oppMap=new Map<Id,Opportunity>();
	for(Opportunity  opp:Trigger.new){
		oppMap.put(opp.Id,opp);
	}
	if(Trigger.isInoppert){
		for(Opportunity opp:Trigger.new){
			if(opp.name!=null){
				lstservice.add(new Service__c(Service_Type__c =opp.Service_Type__c,Service_location__c=opp.Service_location__c)););
			}
		}
		if(lstservice.size()>0){
			try{
				insert lstservice;
			}catch(DmlException de){
				System.debug(de);
			}
		}
	}
	if(Trigger.isUpdate){
		for(Opportunity opp :Trigger.new){
			oppMap.put(opp.Id,opp);
		}
		List<Service__c> serList=[SELECT Service_Type__c,Service_location__c FROM Service__c WHERE opppID  IN:oppMap.keySet()];
		List<Service__c> serviceListToUpdate=new List<Service__c>();
		for(Service__c ser:serList){
			ser.Service_Type__c=oppMap.get(ser.Id).Service_Type__c;
			ser.Service_location__c=oppMap.get(ser.Id).Service_location__c
			serviceListToUpdate.add(ser);
		}
		if(serviceListToUpdate.size()>0){
			try{
				update serviceListToUpdate;
			}catch(DmlException de){
				System.debug(de);
			}
		}
	}
}

 

All Answers

ManojjenaManojjena
Which custom onject ,is there any relationship between oppotunity and custom object .What is the type of relationship ?
HelloSanHelloSan
Yes Manoj Master detail relationship between parent opportuntiy and the child custom object Service
ManojjenaManojjena
Hi San,
 What exactly want ,which all fields you want to map from opportunity to Service . You want only on update then how you will create Service .
One opportunity may have more than Service in that case what you want to do ? You want to update all service right ?
Please explain me bit more .
HelloSanHelloSan
 Service is created on opportunity insert and there will be only one service record for each opportunity,there are fields like service type(picklist) and service location on both opportunity and service object,when these fields are updated on the opportunity object they need to be updated on service object also
ManojjenaManojjena
Hi San,

Please check below code .
trigger createAndUpdate  on Opportunity(after insert,after update){
	List<Service__c lstservice=new List<Service__c>();
	Map<id,Opportunity> oppMap=new Map<Id,Opportunity>();
	for(Opportunity  opp:Trigger.new){
		oppMap.put(opp.Id,opp);
	}
	if(Trigger.isInoppert){
		for(Opportunity opp:Trigger.new){
			if(opp.name!=null){
				lstservice.add(new Service__c(Service_Type__c =opp.Service_Type__c,Service_location__c=opp.Service_location__c)););
			}
		}
		if(lstservice.size()>0){
			try{
				insert lstservice;
			}catch(DmlException de){
				System.debug(de);
			}
		}
	}
	if(Trigger.isUpdate){
		for(Opportunity opp :Trigger.new){
			oppMap.put(opp.Id,opp);
		}
		List<Service__c> serList=[SELECT Service_Type__c,Service_location__c FROM Service__c WHERE opppID  IN:oppMap.keySet()];
		List<Service__c> serviceListToUpdate=new List<Service__c>();
		for(Service__c ser:serList){
			ser.Service_Type__c=oppMap.get(ser.Id).Service_Type__c;
			ser.Service_location__c=oppMap.get(ser.Id).Service_location__c
			serviceListToUpdate.add(ser);
		}
		if(serviceListToUpdate.size()>0){
			try{
				update serviceListToUpdate;
			}catch(DmlException de){
				System.debug(de);
			}
		}
	}
}

 
This was selected as the best answer