You need to sign in to do that
Don't have an account?

Please correct my trigger code.
acount record has child contact record, if status__c(picklist) field of contact record is 'open' update account description field of account record.
trigger x4 on Account (before update) { list<account> accList = new list<account>(); set<id> idSet = new set<id>(); for(account acc : trigger.new){ idSet.add(acc.id); } list<contact> conList = [Select id, accountid, Status__c from contact where accountId in: idSet]; map<id,contact> conMap = new map<id,contact>(); for(contact con : conList){ conMap.put(con.accountId, con); } for(account a : trigger.new){ if(conmap.containsKey(a.id) || conmap.get(a.id).Status__c=='open'){ a.description = 'account is updated from contact'; accList.add(a); } } update acclist; }
Are you getting any errors as such?? and you can direcly get those records which are in the account id set and where the status is open.
Regards,
Anutej
You need to write a trigger on Contact. Go through the below code.
Mark is as best answer if it helps you.
Trigger :
Handler Class :
Thanks,
Foram Rana
There are a few issues with the code that I can see:
- The map "conmap" may not have an entry for the account if the Contact does not exist so line 13 can fail (conmap.get(a.id).Status__c=='open'). You need to check for a value first using "containsKey".
- You can add Status__c=='open' to the Contact query to reduce transaction time and SOQL rows returned.
- Because you are using a before update trigger context you don't need to use the update method on line 18 as you are modifying the Trigger.new variables
However, I believe you would be better off having the trigger on the Contact object since that is the input for the logic. See below for an example class method that should be from the afterInsert and afterUpdate trigger context on the Contact object.