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 

Locking Stage Field on Opportunity

On my opportunity page, I have a child object called Sponsorship Offer. When a Sponsorship offer is related to an opportunity, I want to prevent the Stage field from being change on the Opportunity page, but be controlled via a custom stage field on the custom object. Is this even possible through a trigger?
viruSviruS
Opportunity stage should no allow to update if any Sponsorship Offer record associated .. right.  We can write trigger on opportunity update for that. not sure what do you wants  ViA Custome field and Custom Object?
 
Ramanjot SidhuRamanjot Sidhu
So once a sponsorship offer record is associated with an opportunity, the field stage on the opportunity should only be controlled via the field stage on the custom object called sponsorship offer. The stage field on the related sponsorship offer should be the conrolling field for the opportunity stage field.  We still want to be able to make updates to the opportunity stage, but only through the related sponsorship offer controlling field named"stage".
 
Ramanjot SidhuRamanjot Sidhu

Vivek, on this developer forum, assissted me and wrote this trigger:

 

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');
            }
        }
      }
     }

 

which works perfectly fine, but I still want to be able to edit the opportunity stage field, from the related sponsorship offer field. I cannot due this, as this trigger locks the field completed once a sponsorship offer is added.