1

I have written a trigger by seeing various website but I am unable to. My parent object is standard object and child object is custom. How can I write a simple trigger to auto populate some fields in parent object when child record is inserted having lookup to parent?

1
  • 2
    Try using Process Builder first. You may not need to write code at all.
    – sfdcfox
    Apr 23, 2017 at 14:55

2 Answers 2

1

For your requirement, I have written a trigger on Custom Object whose parent is a Standard object.

I have created a custom object named Custom Object for make it easy to understand by its name only that it is a custom object. And the Parent of this custom object is a Standard object that is Account.

Custom Object----> Custom_Object__c Fields---> Account_Name__c(lookup for Account) -Age__c(number) -Contact_Number__c(Phone).

And I have also made some custom fields in Account to store the value from the custom object when I auto populate the value of the field via a trigger.

Standard Object---> Account Fields---> CO_Name__c(text) -CO_Age__c(number) -CO_Contact_Number__c(phone).

Trigger on Custom_Object__c

trigger TriggerOnCustomObject on Custom_Object__c (before insert){
    AutoFillTheAccountFields.demo(trigger.new);
}

Trigger Class:

public class AutoFillTheAccountFields{
    public static void demo(list<Custom_Object__c> COlist){
        set<ID> AccIDSet = new set<ID>();
        list<Account> acclist = new list<Account>();
        map<ID , Account> accmap = new map<ID , Account>();
        list<Account> updateAcclist = new list<Account>();
        for(Custom_Object__c CO : COlist){
            if(CO.Account_Name__c != Null){
                AccIDSet.add(CO.Account_Name__c);
            }
        }
        if(AccIDSet.size() > 0){
            acclist = [ SELECT Id , CO_Name__c , CO_Age__c , CO_Contact_Number__c FROM Account WHERE ID IN: AccIDSet ];
        }
        for(Account acc : acclist){
            if(accmap.get(acc.Id) == Null){
                accmap.put(acc.Id , acc);
            }
        }
        for(Custom_Object__c cust : COlist){
            if(cust.Account_Name__c != Null){
                Account acc = accmap.get(cust.Account_Name__c);
                acc.CO_Name__c= cust.Name;
                acc.CO_Age__c = cust.Age__c;
                acc.CO_Contact_Number__c = cust.Contact_Number__c;
                updateAcclist.add(acc);
            }
        }
         update updateAcclist;
    }
}

Regards,
Akshay

0

Do you really need a trigger here? Try out if you can do this in Process Builder as you might save yourself a lot of time maintaining that trigger later on.

2
  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker.
    – Eduard
    Aug 28, 2018 at 13:44
  • Sorry, I'm new to this site and can't comment on questions yet. Aug 28, 2018 at 13:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .