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
lovetolearnlovetolearn 

Using a trigger to autopopulate fields

I would like to write a trigger whereby the user can enter the name of a Contact via a lookup field and the Contact's address and phone number will be automatically displayed in textfields. Please help. Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

No that is not possible trigger will only work after you perform any dem activity. In your case insert or update. It will not populate merely by selection on native  page layout. If you want to achieve this you have to create VFP and it will increase your work. 

 

If you don't want these address fields to come on edit layout you can use formula field as well. But then also no option that it will be filled before you click save button.

 

All Answers

Shashikant SharmaShashikant Sharma

you need to write a trigger like this ,

 

I have used MyObject__c in this replace with you object and fields 

 

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
          }
       
   }

}

 

lovetolearnlovetolearn

Thanks a lot for your quick response. 

 

Wouldn't it be after update since I want the fields to auto populate after the user insert the name of the contact.

lovetolearnlovetolearn

I tried the code and I keep getting an error saying: Error: Compile Error: Method does not exist or incorrect signature: [MAP<Id,Contact>].add(String) at line 14 column 26.

Ankit AroraAnkit Arora

Irrespective of the above thread, if you want to add values in map you need to use "PUT" instead of Map.Add() ;

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Shashikant SharmaShashikant Sharma

Change mapCon.add(obj.Contact__c); to

 

mapCon.get(obj.Contact__c);

 

 

And before insert is better as in after insert you will need to make a dml statement to Update your record, incase of before insert trigger you will not required as it's getting inserted itself. In case you go for after insert , after update, your update will again take you beck to the trigger so you will need to write extra logic for avoid such recurring of this trigger. So go with before insert, before update.

 

Let me know if any issues in it.

lovetolearnlovetolearn

Thank you. That seem to have done the trick.  I dont get the error message anymore. However, the fields dont update themselves until after i hit save. If there anyway to have the values appear before i hit save. Maybe I entered something wrong, here is what I put:

 

trigger populateContact on Member_Assess__c (before insert , before update){
    
    Set<ID> setConIds = new Set<ID>();
    for(Member_Assess__c  obj : trigger.new){
        if(obj.Member_Name__c != null)
        setConIds.add(obj.Member_Name__c);
    }
    
     MAP<ID , Contact> mapCon = new MAP<ID , Contact>([Select Address__c from Contact where id in: setConIds]);
     for(Member_Assess__c  obj : trigger.new)
       {
        if(obj.Member_Name__c != null)
          {
            Contact  c = mapCon.get(obj.Member_Name__c);
            obj.Address__c = c.Address__c;
            //Similarly you can assign Address fields as well, just add those field in Contact SOQL as well
          }
       
       }

Shashikant SharmaShashikant Sharma

No that is not possible trigger will only work after you perform any dem activity. In your case insert or update. It will not populate merely by selection on native  page layout. If you want to achieve this you have to create VFP and it will increase your work. 

 

If you don't want these address fields to come on edit layout you can use formula field as well. But then also no option that it will be filled before you click save button.

 

This was selected as the best answer
lovetolearnlovetolearn

Thank you so much for all your help

Sruthi.sfdcSruthi.sfdc

hi i need to populate Trigger on account: If custom address is null and billing address is not null, populate the custom address with billing address...

i try your code but it shows
 

trigger populateContact on Account ( before insert , before update)
{
    Set<ID> setConIds = new Set<ID>();
    for(Account obj : trigger.new)
   {
         if(obj.BillingStreet != null)
            setConIds.add(obj.Billing Street);
   }

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

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

}

 

Please help me on this paste th code

  
  
Sruthi.sfdc1Sruthi.sfdc1

hi i need to populate Trigger :

If custom address is empty and billing address is have a address, need to move  custom address with billing address...

 

 

 

trigger populateAdd on Account (before insert , before update){
 
    Set<String> setConIds = new Set<String>();
    for(Account  obj : trigger.new){
        if(obj.cust_address__c != null)
        setConIds.add(obj.cust_address__c);
    }
    
     MAP<ID , Account> mapCon = new MAP<ID , Account>([Select cust_address__c from Account where id in: setConIds]);
     for(Account  obj : trigger.new)
       {
        if(obj.BillAddress__c != null)
          {
            Account  c = mapCon.get(obj.cust_address__c);
            obj.cust_address__c = c.BillAddress__c;
           
          }
       
       }
       
       }

 

 

Why its not updated

BALA_RAMBALA_RAM

Thanks for giving solution for Auto population