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
BrandiTBrandiT 

Trigger to update child records on Opportunity - Compile Error: Field is not writeable

I'm trying to write a trigger that updates the child records on an Opportunity when a formula field - Contract Status - shows the value Cancelled/Recalled.  I'm not trying to update the Contract Status field, I just referencing it so that the trigger only fires when it shows that one value.

 

Here's the trigger and error message.  I'm at a loss as to how to fix this.

 

trigger CancelSuppHistory on Opportunity (before update) {

If(Trigger.isUpdate){
    
     Set<ID> ids = new Set<ID>();
     list<Opportunity> updOpps =
     [SELECT Id,
                 (Select Id, opportunity__c, active_media_plan__c, active_art__c
                  from Media_Plan_History__r ) FROM Opportunity
                 WHERE Id in :ids];        
                
                 List<Media_Plan_History__c> SHToUpdate = new List<Media_Plan_History__c>();

         //Then loop through each parent object in 'updated parent
         for (Opportunity Opp : updOpps)
         {
             if(opp.contract_status__c = 'Cancelled/Recalled')
            
                //and loop thru each kid in the child set}
               for(Media_Plan_History__c sh : Opp.Media_Plan_History__r)
               {
                         ///update logic from above
                          if(sh.active_media_plan__c = TRUE)

{
       sh.active_media_plan__c =  FALSE;
      shToUpdate.add(sh);
}
                }
        }
       if( !shToUpdate.isEmpty)
       {
            update shToUpdate;
       }
}


}

 

 

Error: Compile Error: Field is not writeable: Opportunity.Contract_Status__c at line 17 column 17

 

steve456steve456

Contract_status is a picklist.........make that change

BrandiTBrandiT

I'm not sure what you mean.  Contract_Status__c field on the opportunity is actually a formula field, which is why it wouldn't be writable, but I'm not trying to change it with the trigger.

cashworthcashworth

On line 17 you have:

if(opp.contract_status__c = 'Cancelled/Recalled')

 

Change it to:

if(opp.contract_status__c == 'Cancelled/Recalled')

 

Problem is you only used a single = which attempting to set the field to a value not evaluate it agains a string or expression.