+ Start a Discussion
Shivangi Singh 52Shivangi Singh 52 

Best approach to update a field in Parent object if any update is happening on either of the object

I have to 2 object having master-detail relationship. 
I want to update a Status picklist field in Parent object if any update is happening on either of the object (Parent or child).
What will be the best approach?

 
Andrew GAndrew G
Clicks before code.
So
  1. 'before save triggered flow' on the parent, and
  2. 'after save triggerd flow' on the child.
If you don't care if anything has changed, just that someone has saved the record, just invoke the flow.
If you need to detect that a change, you can use $Record and $Record_Prior.
If the number of fields make that too arduous, then i think you would have to go to a trigger and use the schema.sobject notations.
without compiling, something along the lines of:
Map<String, Schema.SobjectField> fields  =Test__c.getSObjectType().getDescribe().fields.getMap();
for(String f : fields.keySet()){
    if(f.right(4)<>'date' && !f.contains('modified')) {
    System.debug('FIELD:' + f);    
    }
}
for( Test__c newRecord : trigger.new){
    Test__c oldRecord = Trigger.oldMap.get(newRecord.Id);
    for(String f : fields.keySet()){
        if(f.right(4)<>'date' && !f.contains('modified')) {
            if(oldRecord.get(f)<> newRecord.get(f)){
                System.debug('***record changed***');
                //update the parent record
                break;
            }
        }
    }
}


regards
Andrew

 
veer soniveer soni
hy Shivang,
if you want to update parent object when parent or child is update then you have to create to trigger on both object.
#1. On Before Update on parent object
#2. After update on child object
here is simple way to reach this requirement.
trigger UpdateAccount on Contact ( After Update) {
     Set<Id> setAccountIds  = new Set<Id>();
    list<Account> lstAccount = new list<Account>();
    
    if(trigger.isAfter && trigger.isUpdate){
        for(Contact con : Trigger.new){
            if(con.AccountId != null){
            setAccountIds.add(con.AccountId);
        }
        }
        for(Account acc : [SELECT Id,Name,accountSource From Account Where Id In : setAccountIds]){
              acc.accountSource = 'web';
        lstAccount.add(acc);
        }
        system.debug('lstAccount : ' + lstAccount);
        if(lstAccount.size() > 0){
            update lstAccount;
        }
    }
}
 
trigger updateAccountV1 on Account (before update) {
 if(trigger.isBefore && trigger.isUpdate){
     for(Account acc : trigger.new){
         acc.accountSource = 'web';
     }
    }
}

don't forget to mark it as best answer if it helps you.
Thank you
Suraj Tripathi 47Suraj Tripathi 47
Hi Shivangi,
Greetings!

You can implement this from the trigger.
But use the Flow, because Flow has the standard functionality and is optimized.
So, try to less code in Salesforce.

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi
Andrew GAndrew G

@suraj
indeed, it can be done in flow.  

But the code solution I offered above gives consideration to testing that a change has occured.  The OP is a little vague in that they want "any update" to fire the automation. 

If "any update" just means someone opening and then hitting the save button, then a flow is perfect.  Remember that just hitting Save will fire a DML and invoke the flow.

If the OP actually wants to do the "update" if there is a data change in a field, then they can utilise the $Record and $Record_Prior elements.

However, if they want to detect a data change and there are an excessive number of fields (say over 100) in the object, then code comes to the fore.  Because whilst doing a flow doing 100 criteria checks on using $Record and $Record_Prior is maybe possible (i have not tried that many criteria in a single decision point in a flow yet), it could become arduous to maintain.  Any new field would need to be added to the criteria and the flow updated.  If you used the sObject schema code example above, any new fields would not need to be added to the code because the code already says get ALL the fields and detect if there is a change before doing the update on record/related record.

Whilst "Clicks before Code" is the Salesforce catch phrase, in some cases code is preferred.  Because I have already seen some Flow constructs that are quite poorly designed.


regards
Andrew