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
Ramanjot SidhuRamanjot Sidhu 

How to map a field from child object to parent object using a trigger

Trigger Not Working

I have two objects: Opportunity (Parent),  Sponsorship Offer(Line_item__c) (Child)
On the Opportunity object there is a standardpicklist field called "Stage (StageName), and there is a custom field on the Sponsorship Offer page called Stage (Stage__c) with the same picklist. When an individual goes on to the Opportunity page, and tries to change the Stage, if the value is not equal to the Sponsorship Offer field "Stage", then I want to produce an error.. My trigger is not working. Any help would be greatly appreciated as I am new to apex.

 

Compile Error: Variable does not exist: o.StageName at line 7 column 8

trigger UpdateStage on Opportunity (before update) {
    List<Line_item__c> opportunitywithsponsorshipoffer = [SELECT Opportunity__r.id, Opportunity__r.StageName 
    from Line_item__c where Line_item__c.Opportunity__c <> Null];
   
    for (Opportunity o: opportunitywithsponsorshipoffer);
       for(Line_item__c li : o.li){
       o.StageName = li.Stage__c;
    if(o.StageName <> null){//If the opportunity stage name is not the same as the Sponsorship Offer stage name present error message 
         o.StageName = Line_item__c.Stage__c;
     } else {
      o.adderror ('Cannot track Stage on Opportunity page, please refer to the related Sponsorship Offer page');
     } //close for loop
     }// close for if statement
     }// close trigger
Best Answer chosen by Ramanjot Sidhu
Vivek DeshmaneVivek Deshmane
Hi,
Please try below code and it should work.
trigger UpdateStage on Opportunity (before update) {
     List<Line_item__c> opportunitywithsponsorshipoffer = [SELECT Opportunity__r.id, Opportunity__r.StageName 
        from Line_item__c where Line_item__c.Opportunity__c IN :Trigger.NewMap.keyset()];
      
      if( opportunitywithsponsorshipoffer !=null && !opportunitywithsponsorshipoffer.isEmpty())
      {
        for(Line_item__c liRecord : opportunitywithsponsorshipoffer)
        {
            if(Trigger.newMap.get(liRecord.Opportunity__r.id)!=null && Trigger.newMap.get(liRecord.Opportunity__r.id).StageName != liRecord.Opportunity__r.StageName )
            {
              Trigger.newMap.get(liRecord.Opportunity__r.id).adderror('Cannot track Stage on Opportunity page, please refer to the related Sponsorship Offer page');
            }
        }
      }
     }
Best Regards,
-Vivek

All Answers

Vivek DeshmaneVivek Deshmane
Hi,
Could you please try below code and let me know if it works.
trigger UpdateStage on Opportunity (before update) {
     List<Line_item__c> opportunitywithsponsorshipoffer = [SELECT Opportunity__r.id, Opportunity__r.StageName 
        from Line_item__c where Line_item__c.Opportunity__c IN :Trigger.NewMap.keyset();
      
      if( opportunitywithsponsorshipoffer !=null && !opportunitywithsponsorshipoffer.isEmpty())
      {
        for(Line_item__c liRecord : opportunitywithsponsorshipoffer)
        {
            if(Trigger.newMap(liRecord.Opportunity__r.id)!=null && Trigger.newMap(liRecord.Opportunity__r.id).StageName != liRecord.Opportunity__r.StageName )
            {
              Trigger.newMap(liRecord.Opportunity__r.id).adderror('Cannot track Stage on Opportunity page, please refer to the related Sponsorship Offer page');
            }
        }
      }
     }
Best Regard,
-Vivek
Ramanjot SidhuRamanjot Sidhu
Hi Vivek,

Thanks for the quick reply. I get this error :
Error: Compile Error: unexpected token: ';' at line 3 column 87
Vivek DeshmaneVivek Deshmane
Hi,
Please try below code and it should work.
trigger UpdateStage on Opportunity (before update) {
     List<Line_item__c> opportunitywithsponsorshipoffer = [SELECT Opportunity__r.id, Opportunity__r.StageName 
        from Line_item__c where Line_item__c.Opportunity__c IN :Trigger.NewMap.keyset()];
      
      if( opportunitywithsponsorshipoffer !=null && !opportunitywithsponsorshipoffer.isEmpty())
      {
        for(Line_item__c liRecord : opportunitywithsponsorshipoffer)
        {
            if(Trigger.newMap(liRecord.Opportunity__r.id)!=null && Trigger.newMap(liRecord.Opportunity__r.id).StageName != liRecord.Opportunity__r.StageName )
            {
              Trigger.newMap(liRecord.Opportunity__r.id).adderror('Cannot track Stage on Opportunity page, please refer to the related Sponsorship Offer page');
            }
        }
      }
     }
Mark the best answer if it helps you.
Best Regards,
-Vivek
Ramanjot SidhuRamanjot Sidhu
Hi Vivek,

New error produced:
Variable does not exist: Trigger at line 11 column 15
 
Vivek DeshmaneVivek Deshmane
Hi,
Please try below code and it should work.
trigger UpdateStage on Opportunity (before update) {
     List<Line_item__c> opportunitywithsponsorshipoffer = [SELECT Opportunity__r.id, Opportunity__r.StageName 
        from Line_item__c where Line_item__c.Opportunity__c IN :Trigger.NewMap.keyset()];
      
      if( opportunitywithsponsorshipoffer !=null && !opportunitywithsponsorshipoffer.isEmpty())
      {
        for(Line_item__c liRecord : opportunitywithsponsorshipoffer)
        {
            if(Trigger.newMap.get(liRecord.Opportunity__r.id)!=null && Trigger.newMap.get(liRecord.Opportunity__r.id).StageName != liRecord.Opportunity__r.StageName )
            {
              Trigger.newMap.get(liRecord.Opportunity__r.id).adderror('Cannot track Stage on Opportunity page, please refer to the related Sponsorship Offer page');
            }
        }
      }
     }
Best Regards,
-Vivek
This was selected as the best answer
Ramanjot SidhuRamanjot Sidhu
Hi so althought the rule works to lock the opportunity stage field, once a sponsorship offer is added, the problem is now I cannot change the Opportunity field Stage, from the Sponsorship offer page. I still want to be able to change the Stage field on the Opportunity page, but I want to make the controlling field the Related Sponsorship offer custom field "Stage".