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
RadDude89RadDude89 

Update field automatically on lookup record

RadDude89RadDude89
Hi,

Is it possible to use a trigger to update a lookup record automatically if a field on the parent has been updated? I know it can be doen using a workflow rule but this requires editing and saving the lookup record for the change to take effect.

For example: Name on Reg object has been updated from 123 to ABC, the credit record (lookup) is updated automatically from 123 to ABC.

Any help is appreciated,
Thanks.
HaiderRaza77HaiderRaza77
trigger AfterUpdate on Functional_Requirement__c (after update) {
    Map<Functional_Requirement__c, List<wSystemRequirement>> ParentChildMap = new Map<Functional_Requirement__c, List<wSystemRequirement>>();
    
    for(Functional_Requirement__c fr: [SELECT Id, Name, (SELECT Id, Name FROM System_Requirements_del__r) FROM Functional_Requirement__c]){
        if(fr.System_Requirements_del__r.size()>0){
            List<wSystemRequirement> sysreq = new List<wSystemRequirement>();
            for(System_Requirement__c sr: fr.System_Requirements_del__r){
                sysreq.add(new wSystemRequirement(sr, fr.Name));
            }
            ParentChildMap.put(fr, sysreq);
        }
    }
    
    List<System_Requirement__c> srToUpdate = new List<System_Requirement__c>();
        
    for(List<wSystemRequirement> sysreqs: ParentChildMap.values()){
        for(wSystemRequirement sr: sysreqs){
           //sr.Name = ParentChildMap[sr.Functional_Requirement__r].Name; 
           sr.sr.Name = sr.Name;
           srToUpdate.add(sr.sr);
        }
    }
    
    update srToUpdate;
    
    private class wSystemRequirement{
        private System_Requirement__c sr;
        private String name;
        
        wSystemRequirement(System_Requirement__c sr, String name){
            this.sr		= sr;
            this.name	= name;
        }
    }
}
  • Functional Requirement
  • System Requirement
Functional being parent and System being child.

When the parent name is updated all the related childern name is updated as well.
Vijay NagarathinamVijay Nagarathinam
Hi,

I think its standard one no need to write trigger. My understanding about your task is when you change parentrecord means that name is automatically update all the child records.

Can you explanin more about your requirement
RadDude89RadDude89
Hi Vijay,

What I’m looking to do is automatically update a field on a related record if the parent of that record has been updated.

So currently I have 2 objects – Registration and Credit.  I want the system to be setup as to allow the record on Credit to be updated if the Name on Registration has been amended.

On registration you can access the credit record as it has been setup as a lookup field on the registration object. But on credit you can’t access info from the registration as its not setup as lookup (the reg record on credit will appear on a related list). 
So if I wanted to create a formula on credit looking at data from registration I can’t. Not all registrations have credit records either, if they do then they only have one.

Eg. Reg record A has name as 123A, user edits this name to 456B.
Automatically the credit record has name updated to 456B.

Should I setup the reg record on credit as lookup field in order to achieve what I’m looking for?
Do you think this can be done without a trigger?

Thanks
 
HaiderRaza77HaiderRaza77
From what you have explained above RadDude, you will need a trigger. Did you attempted the code I provided above that should help acheive what you looking for.