+ Start a Discussion
grandak koygrandak koy 

how to reference lookup name field in apex class?

public static void checkDestination(Map<Id, order__c> newOrders, Map<Id, order__c> oldOrders){
        for(Id id : newOrders.keyset()){
            if(newOrders.get(id).Destination__c != oldOrders.get(id).Destination__c && newOrders.get(id).Destination__r.Name.contains(‘OverSeas’)){
                newOrders.get(id).Received__c =True;
            }
        }
    }
Simple class but when I use contains() in my lookup reference: destination__r.Name, it throws an error:
Unable to de-reference a null object

How can I write my class that if my destination lookup has the word “overseas” in it, it will set my orders received field to be true?
 
Shruti SShruti S
Could you please try this - 
public static void checkDestination(Map<Id, order__c> newOrders, Map<Id, order__c> oldOrders) {
    newOrders = new Map<Id, order__c>( 
        [ 
            SELECT  Destination__r.Name
                    ,Destination__c
            FROM    order__c
        ]
    );

    for(Id id : newOrders.keyset()){
        if(newOrders.get(id).Destination__c != oldOrders.get(id).Destination__c && newOrders.get(id).Destination__r.Name.contains('OverSeas')){
            newOrders.get(id).Received__c =True;
        }
    }
}
You need to re-query in order to obtain fields on the parent record. Feel free to add the other fields to the SOQL too.
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
I like to use a "relatedData" map so that I don't lose the records in the trigger:
public static void checkDestination( Map<Id,Order__c> newOrders, Map<Id,Order__c> oldOrders )
{
    Map<Id,Order__c> relatedDataMap = new Map<Id,Order__c>
    (   [   SELECT  Id, Destination__r.Name
            FROM    Order__c
            WHERE   Id IN :newOrders.keySet()
        ]
    );

    for ( Id id : newOrders.keyset() )
    {
        Order__c relatedData = relatedDataMap.get(id);
        if  (   newOrders.get(id).Destination__c != oldOrders.get(id).Destination__c
            &&  relatedData.Destination__r.Name.contains( ‘OverSeas’ )
            )
        {
            newOrders.get(id).Received__c = true;
        }
    }
}

 
Shruti SShruti S
+100! I like that idea of relatedDataMap. By the way I think you are using Book Quotes instead of Typewriter Quotes for the OverSeas word.
public static void checkDestination( Map<Id,Order__c> newOrders, Map<Id,Order__c> oldOrders )
{
    Map<Id,Order__c> relatedDataMap = new Map<Id,Order__c>
    (   [   SELECT  Id, Destination__r.Name
            FROM    Order__c
            WHERE   Id IN :newOrders.keySet()
        ]
    );

    for ( Id id : newOrders.keyset() )
    {
        Order__c relatedData = relatedDataMap.get(id);
        if  (   newOrders.get(id).Destination__c != oldOrders.get(id).Destination__c
            &&  relatedData.Destination__r.Name.contains( 'OverSeas' )
            )
        {
            newOrders.get(id).Received__c = true;
        }
    }
}
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Yes - copied from the original post.  Sharp eye!