You need to sign in to do that
Don't have an account?
Grv Jain
Apex Triiger
I have Two custom Object Account And Opportunity
Account :Parent
Opportunity:Chlid
on the parent object, I have a checkbox:Request,
On the child object, I have a Picklist: Model,Completed,runnig
In opportunity Selected the Piclist Value Model And Running When Parent Account Checkbox is True otherwise Select Picklist Value Runnng When Parent Account Checkbox is False
how to write a Apex Trigger Code
I have gone through your problem.
here is the trigger :-
trigger AutoUpdateOpportunity on Opportunity (before insert,before update) {
if(trigger.isBefore){
if(trigger.isInsert){
AutoUpdateOpportunityClass.updateOpportunity(trigger.new);
}
if(trigger.isUpdate){
AutoUpdateOpportunityClass.updateOpportunity(trigger.new);
}
}
}
here is the class for the trigger:-
public class AutoUpdateOpportunityClass {
public static void updateOpportunity(List<Opportunity> oppList){
try{
List<Account> accountList = new List<Account>();
Map<Id,List<Opportunity>> idVsBooleanMap = new Map<Id,List<Opportunity>>();
Set<Id> accountIds = new Set<Id>();
For(Opportunity opp:oppList){
if(opp.AccountId != null){
accountIds.add(opp.AccountId);
if(idVsBooleanMap.containsKey(opp.AccountId)){
idVsBooleanMap.get(opp.AccountId).add(opp);
}else{
idVsBooleanMap.put(opp.AccountId,new list<Opportunity>{opp});
}
}
}
if(accountIds != null){
accountList = [SELECT Id, Request__c FROM Account Where Id IN : accountIds LIMIT 50000];
if(accountList.size() > 0){
for(Account acc:accountList){
for(Opportunity opportunityObj : idVsBooleanMap.get(acc.Id)){
if(acc.Request__c == false){
opportunityObj.Picklist__c = 'runnig';
}
}
}
}
}
}catch(Exception e){
system.debug('Exception on line:::'+e.getMessage());
}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Sachin Arora