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
pallavi elgondapallavi elgonda 

trigger:I am trying to update a field on Account upon updating a filed of all its contacts. But I see the update is not happening in the UI but in logs I can see it.

trigger contactTrigger on Contact (after insert,after update) {
List<Id> ids = new List<Id>();
    for(Contact con: trigger.new){
        ids.add(con.AccountId);
    }
    List<Account> aList= [select id,name, Task__c, (select id,Task__c from contacts) from account where id IN: ids];
        
    
    for(Account acc: aList){
        for(Contact con: acc.contacts){
            system.debug('task'+con.Task__C+acc.Task__C);
        if(con.Task__C=='Submitted'){
            acc.Task__C='Achieved';
            } system.debug('task'+con.Task__C+acc.Task__C+acc.Name);
        }
    }
}
    
    
    
    
    
    
    
    
 
Best Answer chosen by pallavi elgonda
Maharajan CMaharajan C
Hi Pallavi,

Try the below way:
 
trigger contactTrigger1 on Contact (after insert,after update) {
    set<Id> ids = new set<Id>();
    List<Account> accListtoUpdate = new List<Account>();
	for(Contact con: trigger.new){
        ids.add(con.AccountId);
    }
    List<Account> accList= [select id,name, Task__c, (select id,Task__c from contacts where Task__C!='Submitted') from account where id IN: ids];
    
    for(Account acc : accList){
        if(acc.contacts.size() == 0)
        {
            acc.Task__c = 'Achieved';
            accListtoUpdate.add(acc);
        }
    }
    if(!accListtoUpdate.isEmpty())
        update accListtoUpdate;
}

Thanks,
Maharajan.C

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Pallavi,

Greetings!

Seems like the DML operation is missing after saving the values on the fields in the trigger.We need to use the Update statement to commit the changes.

update contacts;

Reference:https://salesforce.stackexchange.com/questions/23338/trigger-to-update-parent-object-value-with-child-value

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
pallavi elgondapallavi elgonda
Hi Shirisha,
Thank you.
Adding DML works for a single contact. When I have multiple contacts under one Account ,my code should check for all the contacts fields. If any of the contact under one account  is not 'submitted' then the account field shouldn't update. Can you please help me to achieve this. I am new to coding environment. Thank You.
pallavi elgondapallavi elgonda
I have tried in this way and it is working. Could you please guide me is there any other way to achieve this.
List<Id> ids = new List<Id>();
    for(Contact con: trigger.new){
        ids.add(con.AccountId);
    }
    List<Account> aList= [select id,name, Task__c, (select id,Task__c from contacts) from account where id IN: ids];
    
    integer i=0;
        for(Account acc: aList){
        for(Contact con: acc.contacts){
            system.debug('task'+con.Task__C+acc.Task__C);
            if(con.Task__C=='Submitted'){
                i=i+1;
                
            } else{}
        }
        
        if(i==acc.contacts.size()){
            acc.Task__C='Achieved';
        } 
    } update aList;
Maharajan CMaharajan C
Hi Pallavi,

Try the below way:
 
trigger contactTrigger1 on Contact (after insert,after update) {
    set<Id> ids = new set<Id>();
    List<Account> accListtoUpdate = new List<Account>();
	for(Contact con: trigger.new){
        ids.add(con.AccountId);
    }
    List<Account> accList= [select id,name, Task__c, (select id,Task__c from contacts where Task__C!='Submitted') from account where id IN: ids];
    
    for(Account acc : accList){
        if(acc.contacts.size() == 0)
        {
            acc.Task__c = 'Achieved';
            accListtoUpdate.add(acc);
        }
    }
    if(!accListtoUpdate.isEmpty())
        update accListtoUpdate;
}

Thanks,
Maharajan.C
This was selected as the best answer