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
SFDC_DevloperSFDC_Devloper 

Hoe to get relationship values in Trigger Please help me urgent

Hi Below code I am getting null values;
 
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

 
lakslaks

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.
AshlekhAshlekh
Hi,

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.
Pritam ShekhawatPritam Shekhawat
trigger CreatePhase on Project__c (after insert) {
 
  List<Phase__c> newOpps = new List<Phase__c>();
  List<Lead__c>  newLead = new List<Lead__c>();
  for (Project__c acc : Trigger.new) {
    Phase__c opp = new Phase__c();
    opp.Name               =  'Phase-1';
    opp.Start_Date__c      = Date.today(); 
 //query the DB for the Parent Object Fields   
List<Lead__c> accList = [SELECT Rate__c,Type__c,Description__c,Hours_Per_Week__c,Duration__c,Fixed_Budget__c from Lead__c WHERE Id = :acc.pritam__Lead__c];
Lead__c lead = accList.get(0);
//used Parent object Field For update Child Object Fields
      opp.pritam__Description__c = lead.pritam__Description__c;
    opp.pritam__Duration__c = lead.pritam__Duration__c;
    opp.pritam__Hours_Per_Week__c = lead.pritam__Hours_Per_Week__c;
    opp.pritam__Rate__c = lead.pritam__Rate__c;
    opp.pritam__Type__c = lead.pritam__Type__c;
    opp.pritam__Price__c =lead.pritam__Fixed_Budget__c;

opp.Project__c = acc.Id; // Use the trigger record's ID
    newOpps.add(opp);
      
  
  }
  insert newOpps;
}

Checkout this example In this trigger i used relationship field .  If u have any doubt then let me know.
Thanx
Pritam Shekhawat
SFDC_DevloperSFDC_Devloper
trigger CompareValues on Account__c (before insert) 
{

   Set<id> triggerIds = Trigger.newMap.keySet(); //NUll POINTER EXCEPTION

List<Account> listWithParentData = [select <whichever fields you need, including parent fields> from Account where id in :triggerIds];
   
    for(Account appObj:listWithParentData )
    {
    
      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');
  
        }
        
    
    }

I am getting this error :
Set<id> triggerIds = Trigger.newMap.keySet(); //NUll POINTER EXCEPTION

 
lakslaks

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.
Sfdc CloudSfdc Cloud
Hi
Try Below code
trigger CompareValues on Account__c (before insert) 
{

   Set<id> triggerIds = new Set<id>();
   for(Account acc:trigger.new)
   {
	triggerIds.add(acc);
	
   }

List<Account> listWithParentData = [select <whichever fields you need, including parent fields> from Account where id in :triggerIds];
   
    for(Account appObj:listWithParentData )
    {
    
      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');
  
        }
        
    
    }

Thanks:)
John PipkinJohn Pipkin
In before insert triggers the record's ID has not yet been set (hence the name "before" insert), so you cannot access it. This should work:
 
trigger CompareValues on Account__c (before insert)
{
    for(Account appObj:Trigger.new)
    {
      If(appObj.Sales_Name__c != null)
        {
            salesNameIds.add(appObj.Sales_Name__c);
        }
    }

    Map<ID,String> codeMap = new Map<ID,String>();
    Map<ID,SalesNameObject> salesNameMap = new Map<ID,SalesNameObject>();

    if(!salesNameIds.isEmpty()){
    	salesNameMap = new Map<ID,SalesNameObject>([Select Id, Salesman_Status__c, City_Code__r.Code__c from SalesNameObject 
    												where Id in :salesNameIds]);
    	for(SalesNameObject sno: salesNameMap.values()){
    		codeMap.put(sno.Id,sno.City_Code__r.Code__c);
    	}
    }

    for(Account appObj :Trigger.new){
    	String code = codeMap.get(appObj.Sales_Name__c);
    	SalesNameObject refSalesName = salesNameMap.get(appObj.Sales_Name__c);

    	if(refSalesName != null){
    		if(appObj.City_Code__c != code || refSalesName.Salesman_Status__c == 'Inactive')
    			appObj.addError('City Trigger Error');
    	}
    }
}

 
SFDC_DevloperSFDC_Devloper

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
John PipkinJohn Pipkin
You can use before update as well, but I would suggest adding criteria to the update portion of the code so the logic will only run when it should re-evaluate versus running on every update.
kalyan chakravarthikalyan chakravarthi
Hi, How to write a trigger of update event for non related objects. i have two objects i.e., Contact and Candidate. if i update fields in Contact and it should get update the corresponding fields in Candiate also. please help me.