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
Sam AlexSam Alex 

get Account Name from Contact Object

Hi,

I want to get the Account.Name from the Account object from the Contact (before insert) Trigger. 

1. I fill Contact details
2. I select the Account

Then I fire my trigger. My trigger is for Contact object. 

I have the Contact object details

Contact[] cont = Trigger.new;

 

I tired to access the Account.Name related to this Contact by

cont.Account.Name
But it returns NULL.

Please Help me.

Thanks in advance
Best Answer chosen by Sam Alex
ManjunathManjunath
Hi,

Basically if you want the account associated with the contact. Then you can do this

Contact[] cont =[Select name, Account.Name from contact where id in: Trigger.new];

After that use EX : cont[0].Account.Name

Regards,
Manjunath 

All Answers

ManjunathManjunath
Hi,

You will not have option to access lookup fileds value from the Trigger.new.
You need to query to get that.


Regards,
Manjunath

 
Sam AlexSam Alex
Hi Manjunath, Thanks for your response.

Ohh... OK But, with what values I can query it? Because at the moment I only have details of the Contact object. User can select any Account when adding the Contact record. How can I specifically select the Account that the user selects?

Could you please help me on this as well? Thankyou.
ManjunathManjunath
Hi,

Basically if you want the account associated with the contact. Then you can do this

Contact[] cont =[Select name, Account.Name from contact where id in: Trigger.new];

After that use EX : cont[0].Account.Name

Regards,
Manjunath 
This was selected as the best answer
Sam AlexSam Alex

Hi Manjunath. Thank you for response.

IT IS WORKING with your code.

Thank you again for helping me.