+ Start a Discussion
harshal patil 007harshal patil 007 

Hello, how to pass lookup field in if condition (How to check lookup field in apex)??

harshal patil 007harshal patil 007
 i want to check lookup field in 3rd cond like (if status__c='open')
                         object name :SalesHeader__c //Lookp field on EFT_Transaction_Status__c object
                         and field name:Status__c 
                         how to write it please let me know


public class Eftclass {

    public static void eft(List<EFT_Transaction_Status__c>newlist)
    {
        for(EFT_Transaction_Status__c ca:newlist)
        {
            
       if(ca.Method_Name__c ='Credit Card Address Verify' && ca.Transaction_Status__c= 'Declined' && Help me for this condition)
Naveen KNNaveen KN
Basically lookup fields stores id in the objects. consider you have field A in Object A. Field A is lookup from Object B., In that case, Field A stores the id in the object A. 

lookup fields always store the id reference to the parent record. 

Regards,
Naveen
harshal patil 007harshal patil 007
So, how can i use status field in if condition 
 if(ca.Method_Name__c ='Credit Card Address Verify' && ca.Transaction_Status__c= 'Declined' && Help me for this condition)
Naveen KNNaveen KN
Hi Harshal,

Sorry for the delay here. we have to use the relationship name to get the lookup field values. 

Example: select name, Authority_Group_Name__r.name__c from account where id='0017F00000JsK5dQAF'

in this case, Authority_Group__c and Account are two objects, where i have created Authority_Group_Name__c lookup field in the account object. change __c to __r and have your field to the relationship.fieldname__c format 

in your case relationship.status__c  in the if condition, make sure you are getting this field value in the query. Try this and let me know the outcome.

Regards,
Naveen K N
 
Harshal Patil 15Harshal Patil 15
Thank you.