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
Jim MontgomeryJim Montgomery 

Trigger to insert lookup id on opportunity to another object

I have a custom object BI Retention that I need to link to opportunities.
Basically, if the DTN, month and year match, I want to insert the BI.ID into the BI lookup field on the opportunity.
Here is what I have so far, but getting error invalid foriegn key relationship BI_Retention_Quota.Retention_month__c.

trigger BI_Retention_Quota on Opportunity (before insert, before update) {   
  List<BI_Retention_Quota__c > BI = [select Retention_Month__c,Retention_DTN__C from BI_Retention_Quota__c ];
{
    if(Trigger.isBefore)
{

 if (Trigger.isInsert || Trigger.isUpdate)
{
  for(Opportunity  Opp : trigger.new)      

 {
     for (BI_Retention_Quota__c b : BI)
     {
         if (b.retention_month__c.month = opp.retention_month__c.month && b.retention_month__c.year    = opp.retention_month__c.year && b.retention_dtn__c = opp.renewal_rep_dtn__c)
         {         
                        opp.BI_Retention_Quota__c = b.Id;
         }   
                                     

           }

       }

    }

}

}
}
 
Medhya MahajanMedhya Mahajan
Hi,

Change your IF condition like this :

Use __r instead of __c:

if (b.retention_month__r.month = opp.retention_month__r.month && b.retention_month__r.year    = opp.retention_month__r.year && b.retention_dtn__c = opp.renewal_rep_dtn__c)

Please mark as solved if this helps.

Regards
Medhya Mahajan
 
Jim MontgomeryJim Montgomery
I still get the same error, but it just has __r instead of __c.