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
aditya prasadaditya prasad 

How to use lookup field in a trigger?

I need to insert ContactRole record for an opportunity while creating an opprtunity from some custom object.I have written a trigger i.e after insert on opportunity.

if (trigger.isinsert)
    {   
        oppty = trigger.new;

        for(opportunity Opt:oppty)
        {
    system.debug('Dealer'+Opt.Installed_Product__r.Dealer_1__c);
           if(Opt.Installed_Product__c !=null){
                system.debug('Method Entry');         OpportunityContactRole OCR1 = new OpportunityContactRole(ContactId=Opt.Installed_Product__r.Dealer_1__c, OpportunityId=Opt.Id,IsPrimary=TRUE);
                NewOppCRole.add(OCR1);
            }

But every time I get ContactId = null.So insertion failed since it is a mandatory field.
Is there any way to solve this issue?

Thanks in advance.
Best Answer chosen by aditya prasad
Suraj GharatSuraj Gharat
Hi Aditya,

I see you are setting "ContactId" by a field which is not present on opportunity itself, and that is the problem. Apex trigger does not provide data from relationships. Thus if you need "Dealer_1__c" from "Installed_Product__r", then you may do it in following two ways.
  1. Recommended way - Create a formula field on opportunity that stores "Dealer_1__c" from related "Installed_Product" object, and then you may use this formula field to set "ContactId" for "OpportunityContactRole" in above trigger.
  2. In above trigger, query the all required "Installed Products" parents and get its "Dealer_1__c" from query and set it "ContactId" for "OpportunityContactRole"
HTH,
Suraj

All Answers

Suraj GharatSuraj Gharat
Hi Aditya,

I see you are setting "ContactId" by a field which is not present on opportunity itself, and that is the problem. Apex trigger does not provide data from relationships. Thus if you need "Dealer_1__c" from "Installed_Product__r", then you may do it in following two ways.
  1. Recommended way - Create a formula field on opportunity that stores "Dealer_1__c" from related "Installed_Product" object, and then you may use this formula field to set "ContactId" for "OpportunityContactRole" in above trigger.
  2. In above trigger, query the all required "Installed Products" parents and get its "Dealer_1__c" from query and set it "ContactId" for "OpportunityContactRole"
HTH,
Suraj
This was selected as the best answer
AshlekhAshlekh
Hi,

Here is code you need to write for trigger.
if(trigger.isInsert)
{
	Set<id> opptyId = trigger.newMap.getKeySet();
	for(Opporutnity o :[select <mention here all fields which you are required > from opportunity where id in:opptyId])
	{
	  if(Opt.Installed_Product__c !=null)
      {
       system.debug('Method Entry');         
	   OpportunityContactRole OCR1 = new OpportunityContactRole(ContactId=Opt.Installed_Product__r.Dealer_1__c, 
					OpportunityId=Opt.Id,IsPrimary=TRUE);
       NewOppCRole.add(OCR1);
      }
	}
}

-Thanks
Ashlekh Gera
aditya prasadaditya prasad
Thanks Suraj
It solved my problem 

Aditya