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
Giancarlo AmatiGiancarlo Amati 

detect field1 update and update field 2

Dear All, 
in the contract object I have two fields. One is a text formula, which fetches the name from the related opportunity. The second one, is a contract text field. 
The problem I have is to detect when the value of the first field changes so that I can update the second one. I thought to a contract trigger:
where I want Customer_Contract_ID__c to get updated every time the value of Contract_Name_ref__c changes. Both are fields from the same contract object. 
For whatever reason I don't see any change.

Thank you for your help.
trigger CustomerContractIDHasChanged on Contract (before update) {

/* Fetch the records that have been udpated */
Map<Id, Contract> m_contract = new Map<Id, Contract> ([SELECT Id FROM Contract WHERE Id IN :Trigger.New]);

If (trigger.isUpdate) {

For (Contract c: Trigger.new) {
    //System.debug('Contract with ID: ', m_contract.size() );
    Contract old_c = Trigger.oldMap.get(c.Id);
    
    System.debug('GIANCAR Contract with Customer Contract ID: ' + m_contract.get(c.Id).Customer_Contract_ID__c );
    System.debug('GIANCAR Contract with Customer Contract Name: ' + m_contract.get(c.Id).Contract_Name_ref__c );

    /*Check if the value has changed, based on the OLD mapping*/
    If (old_c.Contract_Name_ref__c != c.Contract_Name_ref__c) { /*If the value has changed, we update the Customer Contract ID*/
    
        c.Customer_Contract_ID__c = c.Contract_Name_ref__c;
        System.debug('GIANCAR Contract with Customer Contract Name: ' + c.Customer_Contract_ID__c );
    }
}

}

}








 
Tad Aalgaard 3Tad Aalgaard 3
Detecting changes in formula field values using Apex code is not possible.  This is due to the fact that the forumula field is not actually data but instead is a calculated value.