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
TheCustomCloudTheCustomCloud 

Simple Trigger

Why doesnt this trigger work?  It doesnt throw any errors it just doesnt populate the title field.  Can I not use a reference like Contact__r.Contacts_Title__c in a trigger?

 

trigger AddTitleToAttendee_BI on Attendee__c (Before Insert) {  
    
    for(Attendee__c a : trigger.new){
        a.Title__c = a.Contact__r.Contacts_Title__c;
    }
}



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

You trigger should be like this

trigger AddTitleToAttendee_BI on Attendee__c (Before Insert) {  
Set<ID> setContactIds = new Set<ID>();
MAP<Integer , ID> mapAtendeeIndex_ConID = new MAP<Integer ,ID>();
Integer i  = 0;
for(Attendee__c a : trigger.new){
        if(a.Contact__c != null)
        {
        setContactIds.add(a.Contact__c);
        mapAtendeeId_ConID.put( i , a.Contact__c);
        }
        i = i + 1;
        }

    Map<Id , Contact> mapContact =  new Map<Id , Contact>([Select Contacts_Title__c from Contact where Id in: setContactIds]);
     i = 0;
    for(Attendee__c a : trigger.new){
        if(mapAtendeeId_ConID.containsKey(i))
        {
        a.Title__c = mapContact.get(mapAtendeeId_ConID.get(i)).Contacts_Title__c;
        }
        i = i + 1;
    }
}

 

In trigger.new , trigger.old or trigger.newMap , trigger.oldMap all contain current object fields only not related object values, please use above trigger . let me know if any issue in it.

All Answers

bob_buzzardbob_buzzard

Unfortunately you can't - you will only have the direct fields for the records you are processing.  If you wish to follow relationships you will have to query the related objects via SOQL.

Shashikant SharmaShashikant Sharma

You trigger should be like this

trigger AddTitleToAttendee_BI on Attendee__c (Before Insert) {  
Set<ID> setContactIds = new Set<ID>();
MAP<Integer , ID> mapAtendeeIndex_ConID = new MAP<Integer ,ID>();
Integer i  = 0;
for(Attendee__c a : trigger.new){
        if(a.Contact__c != null)
        {
        setContactIds.add(a.Contact__c);
        mapAtendeeId_ConID.put( i , a.Contact__c);
        }
        i = i + 1;
        }

    Map<Id , Contact> mapContact =  new Map<Id , Contact>([Select Contacts_Title__c from Contact where Id in: setContactIds]);
     i = 0;
    for(Attendee__c a : trigger.new){
        if(mapAtendeeId_ConID.containsKey(i))
        {
        a.Title__c = mapContact.get(mapAtendeeId_ConID.get(i)).Contacts_Title__c;
        }
        i = i + 1;
    }
}

 

In trigger.new , trigger.old or trigger.newMap , trigger.oldMap all contain current object fields only not related object values, please use above trigger . let me know if any issue in it.

This was selected as the best answer
TheCustomCloudTheCustomCloud

Smart solution .  My solution was similar but from looking at yours I realized I may accidently associate a contact to the wrong title if more the attendee is referencing the same contact.

 

trigger AddTitleToAttendee_BI on Attendee__c (Before Insert) {  
    Set<Id> setContactIds = new Set<Id>();
    
    for(Attendee__c a : trigger.new){
        setContactIds.add(a.Contact__c);
    }
    Map<Id , Contact> mapContacts =  new Map<Id , Contact>([Select Contacts_Title__c from Contact where Id in: setContactIds]);

    for(Attendee__c at : trigger.new){
        if(contacts.containsKey(at.Contact__c)){
            at.Title__c = mapContacts.get(at.Contact__c).Contacts_Title__c;
        }
    }
}