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
MikeyJamJamsMikeyJamJams 

Extremely Simple Trigger is Populating a Lookup Field with a Blank Value

Hello,

 

I have the following trigger, aimed to populate a Contact Lookup field with the Contact Id of a related record called Rep ID.

 

Here is the trigger I have:

trigger AccommodationsBeforeTrigger on Accommodations__c (before insert, before update) 
{
    for (Accommodations__c a: trigger.new)
    {
        a.Contact__c = a.Rep_ID__r.Contact__r.Id;
    } 
}

 

This trigger is returning a blank value for the Contact lookup field whenever a record is saved.

However, what's interesting is that if I create a formula field, and use my trigger to set the Contact lookup value equal to this formula field, it works. 

For example, I created a Formula field: test__c whose value is Rep_ID__r.Contact__r.Id.

 

Then, I adjusted my trigger to the following

trigger AccommodationsBeforeTrigger on Accommodations__c (before insert, before update) 
{
    for (Accommodations__c a: trigger.new)
    {
        a.Contact__c = a.Test__c;
    } 
}

 I don't get why it works this way and not the first way since the formula code is the same as the code in my first trigger example. I'd like the trigger to work without the need to create a separate field.

 

Thanks!

Mikey

Best Answer chosen by Admin (Salesforce Developers) 
nothingisreal68nothingisreal68

Hi Mikey,

 

I think this happens because a trigger only loads the fields directly on the record in context when firing. The formula works because the field is directly on the record itself and is therefore made available. To use the foreign key relationship, you would need to loop through every Accommodations__c record in trigger.new and add each Rep_ID__c id to a set, query for the Rep_ID__c records, and then loop through each Accommodations__c record one last time to assign the contact's value. 

 

Take a look at this example in the Apex Developer's Reference to get an idea of what needs to be done:

 

http://www.salesforce.com/us/developer/docs/apexcodepre/Content/apex_triggers_bulk_idioms.htm#trigger_map_sets

All Answers

nothingisreal68nothingisreal68

Hi Mikey,

 

I think this happens because a trigger only loads the fields directly on the record in context when firing. The formula works because the field is directly on the record itself and is therefore made available. To use the foreign key relationship, you would need to loop through every Accommodations__c record in trigger.new and add each Rep_ID__c id to a set, query for the Rep_ID__c records, and then loop through each Accommodations__c record one last time to assign the contact's value. 

 

Take a look at this example in the Apex Developer's Reference to get an idea of what needs to be done:

 

http://www.salesforce.com/us/developer/docs/apexcodepre/Content/apex_triggers_bulk_idioms.htm#trigger_map_sets

This was selected as the best answer
sfdcfoxsfdcfox

The trigger system might be considered a "lazy" system, as it only returns just the object you're working on, and not its parents or children, at least without a query. So you'll have to do something like this:

 

trigger AccommodationsBeforeTrigger on Accommodations__c (before insert, before update) {
  map<id,rep__c> reps = new map<id,rep__c>();
  for(accommodations__c record:trigger.new) {
    reps.put(record.rep_id__c);
  }
  reps.putAll([select id,contact__c from reps__c where id in :reps.keyset()]);
  for(accommodations__c record:trigger.new) {
    if(reps.containsKey(a.rep_id__c)) {
      record.contact__c = reps.get(a.rep_id__c).contact__c;
    }
  }
}

Replace Rep__c with the intermediary object represented by Rep_Id__r in your original example.

MikeyJamJamsMikeyJamJams

Thanks guys, I will work around playing with these queries!

BALA_RAMBALA_RAM

TRY this code defenitly u get the solution for Autopopulation just you have to make Looup relation with User

 

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