You need to sign in to do that
Don't have an account?
Hoe to get relationship values in Trigger Please help me urgent
Hi Below code I am getting null values;
below fields i am getting nullvalues..
appObj.Sales_Name__r.City_Code__r.Code__c
appObj.Sales_Name__r.Salesman_Status__c
Thansk in advance
trigger CompareValues on Account__c (before insert) { for(Account appObj:Trigger.new) { If(appObj.City_Code__c != appObj.Sales_Name__r.City_Code__r.Code__c || appObj.Sales_Name__r.Salesman_Status__c=='Inactive') { appObj.addError('City Trigger Error'); } }
below fields i am getting nullvalues..
appObj.Sales_Name__r.City_Code__r.Code__c
appObj.Sales_Name__r.Salesman_Status__c
Thansk in advance
Hi Rockz,
You are facing the above problem because trigger.new does not include parent object data.
You will have to query the DB for the same.
One way to do it will be to get the set of IDs of records that are being used by the trigger, and query them again to fetch the related parent data as well.
Set<id> triggerIds = Trigger.newMap.keyset();
List<Account> listWithParentData = [select <whichever fields you need, including parent fields> from Account where id in :triggerIds];
And use the list above.
Regards,
Lakshmi.
Yes, In trigger trigger.new and triggger.old always give the current object field value not the info about parent mean above level. Also not include the infr of child.
Checkout this example In this trigger i used relationship field . If u have any doubt then let me know.
Thanx
Pritam Shekhawat
I am getting this error :
Set<id> triggerIds = Trigger.newMap.keySet(); //NUll POINTER EXCEPTION
Sorry, for 'before insert' Trigger.newMap will return null.
On update or after insert trigger.newMap.keySet() will return Id of records to be processed.
In this case you can explicitly query the related object for the required field.
Try Below code
Thanks:)
Hi Sfdc Cloud /John Pipkin,
Thanks for your responce ..As per your code can I use before insert,before update as well tell me?
Thanks in advance