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
himanshu huske 7himanshu huske 7 

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 :{
    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 :{
        if(conmap.containsKey( || conmap.get('open'){
            a.description = 'account is updated from contact';
    update acclist;

ANUTEJANUTEJ (Salesforce Developers) 
Hi Himanshu,

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.

Foram Rana RForam Rana R
Hi Himanshu,

You need to write a trigger on Contact. Go through the below code.
Mark is as best answer if it helps you.

Trigger :
trigger ContactTrigger on Contact (After insert, After update) {
    if(ContactTriggerHandler.isFirsttime == true) {
        ContactTriggerHandler.isFirsttime = false;

Handler Class :
public class ContactTriggerHandler {
    public static Boolean isFirsttime = true;
    public static void updateAcc(List<Contact> lstcon) {
        List<account> updtaccList = new list<account>();
        Set<Id> AccIds = new Set<Id>();
        for(Contact con : lstcon) {
            if(con.Status__c == 'Open') {
        List<Account> lstacc = [Select Id, description from Account where Id IN : AccIds]; 
        for(Account acc: lstacc) {
            acc.description = 'account is updated from contact';
        if(updtaccList.size() > 0) {
            update updtaccList;

Foram Rana​​​​​​​

Ricky Lowe 19Ricky Lowe 19
Hi Himanshu,

There are a few issues with the code that I can see:
  1. The map "conmap" may not have an entry for the account if the Contact does not exist so line 13 can fail (conmap.get('open'). You need to check for a value first using "containsKey".
  2. You can add Status__c=='open' to the Contact query to reduce transaction time and SOQL rows returned.
  3. 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 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.
public void afterInsertANDafterUpdate() {
    Set<Id> accIds = new Set<Id>();

    //build set of account numbers
    for(Contact con :{
        //if insert
        if(Trigger.isInsert && con.Status__c == 'open'){
        //if update then check for status change
            Contact oldCon = Trigger.oldMap.get(con.Id);
            if(oldCon.Status__c != 'open' && con.Status__c == 'open'){

    //query accounts and set description 
    List<Account> accToUpdate = new List<Account>();
    for(Account acc : [SELECT Id, description FROM Account WHERE Id IN : accIds]){
        acc.description = 'account is updated from contact';

        update accToUpdate;     //either use database.update to allow partial failure or encapsulate in try catch