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
Anas AlamourAnas Alamour 

trigger to populate custom fields

I have a custom object and there are three required fields in this custom object ( Country , State, City) . this coustom object has a lookup relation with Contact object. my requirement is , when the staff create a new record for a client and the client has an existing contact file in the system , we need the system populate  these fields from the Conatct object . any one help me to write this trigger. Thanks. 
Best Answer chosen by Anas Alamour
ANUTEJANUTEJ (Salesforce Developers) 
Hi Anas,

>> https://developer.salesforce.com/forums/?id=906F00000008zdWIAQ

As mentioned in the above forum thread you can try this trigger:
 
trigger populateContact on MyObject__c ( before insert , before update)
{
    Set<ID> setConIds = new Set<ID>();
    for(MyObject__c  obj : trigger.new)
   {
         if(obj.Contact__c != null)
            setConIds.add(obj.Contact__c);
   }

  MAP<ID , Contact> mapCon = new MAP<ID , Contact>([Select Phone from Contact where id in: setConIds]);

   for(MyObject__c  obj : trigger.new)
   {
        if(obj.Contact__c != null)
          {
            Contact  c = mapCon.add(obj.Contact__c);
            obj.Phone__c = c.Phone;
            //Similarly you can assign Address fields as well, just add those field in Contact SOQL as well
          }
       
   }

}

Fetch the necessary fields that need to be assigned using soql query and assign the values to each record in the loop.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Anas,

>> https://developer.salesforce.com/forums/?id=906F00000008zdWIAQ

As mentioned in the above forum thread you can try this trigger:
 
trigger populateContact on MyObject__c ( before insert , before update)
{
    Set<ID> setConIds = new Set<ID>();
    for(MyObject__c  obj : trigger.new)
   {
         if(obj.Contact__c != null)
            setConIds.add(obj.Contact__c);
   }

  MAP<ID , Contact> mapCon = new MAP<ID , Contact>([Select Phone from Contact where id in: setConIds]);

   for(MyObject__c  obj : trigger.new)
   {
        if(obj.Contact__c != null)
          {
            Contact  c = mapCon.add(obj.Contact__c);
            obj.Phone__c = c.Phone;
            //Similarly you can assign Address fields as well, just add those field in Contact SOQL as well
          }
       
   }

}

Fetch the necessary fields that need to be assigned using soql query and assign the values to each record in the loop.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
This was selected as the best answer
Samir AliSamir Ali
this coustom object has a lookup relation with Contact object. my requirement is , when the staff create a new record for a client and the client has an existing contact file in the system , we need the system populate  these fields from the Conatct object . any one help me to write this trigger. Thanks. Sai baba images (https://statusdad.in/sai-baba-images)