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
SF DEVSF DEV 

Null value when retrieving values in trigger

For(contract__c con:trigger.new){

con.Owner__c = con.Opportunity__r.Account.createdBy.ID;
con.Region__c= con.Opportunity__r.Account.Region__c;
}

 

con.Opportunity__r.Account.createdBy.ID,con.Opportunity__r.Account.Region__c these values are  NULL ,  How to pass values into Contract when they are related other opportunity and opportunity related to contract.

SRKSRK
You need to query the related records it won't come like that
SF DEVSF DEV

But i want to bulkify my code!!!!!! 

 

 

  For(contract__c con:trigger.new){     
        for(Opportunity o:oppall){
            if(Con.Opportunity__c == o.ID ){
                acc = [Select Id,createdBy.ID,Country_Code__c,ClientID__c,Region__c,Station__c,Country__c From account where Id =:o.AccountId];
               
                   con.Owner__c = acc.createdBy.ID;
                   con.Region__c=acc.Region__c;
                   con.Country_Code__c=acc.Country_Code__c;
                   con.Station__c=acc.Station__c;
                   con.Country__c=acc.Country__c; 
                
            }
        }

SRKSRK
Try something like this
set<id> OppIDset = new set<Id>();
For(contract__c con:trigger.new)
{
OppIDset .add(con.Opportunity__c);
}
Select id,name,Account.Id,Account.createdBy.ID,Account.Country_Code__c,Account.ClientID__c,Account.Region__c,Account.Station__c,Account.Country__c From Opportunity where Id in: OppIDset