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
Gaurish Gopal GoelGaurish Gopal Goel 

Accessing the Parents field in an After Insert Trigger

I am writing a trigger on Contract. When a contract is inserted an opportunity should be inserted under the same Account. Opportunity's name should be the Account's name.

 

I am trying to access the account's fields in this way:

 

For(Contract con:Trigger.new)

{

Opportunity opp = new Opportunity;

opp.name = con.Account.name;

opp.accountid = con.accountid;

insert opp;

}

 

But con.Account.name always return Null value. How should i accomplish this task and Why can't I access the account's name in this way ?

Best Answer chosen by Admin (Salesforce Developers) 
Gaurish Gopal GoelGaurish Gopal Goel

I tried it like this:

 

For(Contract con: [select id,account.name,accountid from contract where id in:Trigger.new])

{

//Code

}

 

It worked fine. But Trigger.new is also a list of Contract, so why we cannot access Account's fields from Trigger.new ?

All Answers

Vinita_SFDCVinita_SFDC

Hello,

 

Try this:

 

for(Contract con : [Select Id, aacount.name, account.id from Contract] : Trigger.new)

 

{


....

code...

 

{

Gaurish Gopal GoelGaurish Gopal Goel

I tried it like this:

 

For(Contract con: [select id,account.name,accountid from contract where id in:Trigger.new])

{

//Code

}

 

It worked fine. But Trigger.new is also a list of Contract, so why we cannot access Account's fields from Trigger.new ?

This was selected as the best answer
Dhaval PanchalDhaval Panchal
We cannot access relationship fields in trigger. you can get only id value. You have to query for other relationship fields.
Gaurish Gopal GoelGaurish Gopal Goel

Ok thanks dapanchal.

Vinita_SFDCVinita_SFDC

Hello,

 

We can refer related fields, that is relationship fields in trigger. For code snippets please refer following links:

 

http://blog.wdcigroup.net/tag/salesforce-apex-trigger/

 

http://salesforce.stackexchange.com/questions/15244/access-field-on-lookup-relationship-in-trigger

Dhaval PanchalDhaval Panchal
I tried to retrieve related field values using trigger.newMap, but it is not working for me.
Vinita_SFDCVinita_SFDC

Please share your code.

Dhaval PanchalDhaval Panchal

Hi Vinita,

 

Thanks for reply.

 

Below is my code. It is just for testing purpose. But in past couple of time I have queried to get related field value in trigger. If this work then it would be great.

 

trigger getAccountContactNames on Contact (before insert, before update) {
    for(Contact con:trigger.New){
        if(con.AccountId <> null){
            con.Account_Contact_Names__c = Trigger.newMap.get(con.Id).Account.Contact_Name__c;
//con.Account_Contact_Names__c = con.Account.Contact_Name__c; } } }

 

Dhaval PanchalDhaval Panchal

If I use below code then it works fine (as per your example given in link). But in this case still we have to query for related field values.

 

trigger getAccountContactNames on Contact (before insert, before update) {
    Map<Id, Contact> mapAcc = new Map<Id, Contact>([Select Id, Account.Contact_Name__c From Contact Where Id in:Trigger.New]);
    for(Contact con:trigger.New){
        if(con.AccountId <> null){
            //con.Account_Contact_Names__c = Trigger.newMap.get(con.Id).Account.Contact_Name__c;
            //con.Account_Contact_Names__c = con.Account.Contact_Name__c;
            con.Account_Contact_Names__c = mapAcc.get(con.Id).Account.Contact_Name__c; //Works fine
        }
    }
}