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
jneilan22jneilan22 

Reset Field Values to Empty After Child Object Deleted

Hello,

 

I have a custom object called Contract Summary__c that has a Master-Detail relationship with the Opportunity object.  I have a trigger that updates the some of the fields on the Opportunity with the fields from the Contract_Summary__c object after it is saved by a user.  What I need to do now to reset those fields on the Opportunity to be empty in the event that the child object is deleted.  My trigger below works fine for the insert and update portions, but does not do anything once the Contract_Summary__c record is deleted.  There are no errors, it just does not affect the Opportuinity.  Any help would be appreciated.  Thanks.

 

 

 

trigger ContractUpdates on Contract_Summary__c (before insert, before update)
{
//Update Opportunity fields from values in Contract Summary object

    list<Opportunity> opp = new list<Opportunity>();    
    set<Id> Ids = new Set <Id>();
    FOR(Contract_Summary__c con :trigger.new) {
        Ids.add(con.Related_Opportunity__c);
    }
    
    Opportunity opp1 = [SELECT Id, Start_Date__c, End_Date__c
    FROM Opportunity
    WHERE Id IN :Ids];

    if(trigger.isInsert || trigger.isUpdate)
    {      
        for(Contract_Summary__c objC : trigger.new)
        {
            opp1.Start_Date__c = objC.Current_Effective_Date__c;
            opp1.End_Date__c = objC.Current_Expiration_Date__c;
            opp1.Current_Term_Months__c = objC.Current_Term_Months__c;
            opp1.Auto_Renew__c = objC.Auto_Renew_Provision__c;
            opp1.Term_for_Convenience__c = objC.Term_for_Convenience__c;
            opp1.Portal_SLAs__c = objC.Portal_SLAs__c;
            opp1.Coaching_Performance_Guarantees__c = objC.Coaching_Performance_Guarantees__c;
            opp1.Delegated_Entity_Agreement__c = objC.Delegated_Entity_Agreement__c;
            opp1.Special_Terms__c = objC.Special_Terms__c;
            Opp.add(opp1);
        }
     }
    IF(trigger.isDelete)
    {      
        for(Contract_Summary__c objC : trigger.new)
        {

            opp1.Start_Date__c = null;
            opp1.End_Date__c = null;
            opp1.Current_Term_Months__c = null;
            opp1.Auto_Renew__c = FALSE;
            opp1.Term_for_Convenience__c = FALSE;
            opp1.Portal_SLAs__c = FALSE;
            opp1.Coaching_Performance_Guarantees__c = FALSE;
            opp1.Delegated_Entity_Agreement__c = FALSE;
            opp1.Special_Terms__c = FALSE;
            Opp.add(opp1);
        }
     }
    IF(opp.size()>0)
    Update opp;
}

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Your trigger is only configured to fire after insert and update:

 

trigger ContractUpdates on Contract_Summary__c (before insert, before update)

 You'll need to change this to:

 

trigger ContractUpdates on Contract_Summary__c (before insert, before update, before delete)

(assuming you want to take action before the record is deleted from the database)

All Answers

bob_buzzardbob_buzzard

Your trigger is only configured to fire after insert and update:

 

trigger ContractUpdates on Contract_Summary__c (before insert, before update)

 You'll need to change this to:

 

trigger ContractUpdates on Contract_Summary__c (before insert, before update, before delete)

(assuming you want to take action before the record is deleted from the database)

This was selected as the best answer
jneilan22jneilan22

Hi Bob,

 

I knew there would be something simple missing.  Thanks for that!  I did have to move a few things around and change the isDelete section to trigger.old so, for anyone that might need it, below is the new code that works!

 

 

trigger ContractUpdates on Contract_Summary__c (before insert, before update, before delete)
{
//Update Opportunity fields from values in Contract Summary object

    list<Opportunity> opp = new list<Opportunity>();    
    set<Id> Ids = new Set <Id>();

    if(trigger.isInsert || trigger.isUpdate)
        {
    FOR(Contract_Summary__c con :trigger.new) {
        Ids.add(con.Related_Opportunity__c);
    }
    
    Opportunity opp1 = [SELECT Id, Start_Date__c, End_Date__c
    FROM Opportunity
    WHERE Id IN :Ids];

        for(Contract_Summary__c objC : trigger.new)
        {
            opp1.Start_Date__c = objC.Current_Effective_Date__c;
            opp1.End_Date__c = objC.Current_Expiration_Date__c;
            opp1.Current_Term_Months__c = objC.Current_Term_Months__c;
            opp1.Auto_Renew__c = objC.Auto_Renew_Provision__c;
            opp1.Term_for_Convenience__c = objC.Term_for_Convenience__c;
            opp1.Portal_SLAs__c = objC.Portal_SLAs__c;
            opp1.Coaching_Performance_Guarantees__c = objC.Coaching_Performance_Guarantees__c;
            opp1.Off_Shore_Resource_Restiction__c = objC.Off_Shore_Resource_Restiction__c;
            opp1.Delegated_Entity_Agreement__c = objC.Delegated_Entity_Agreement__c;
            opp1.Special_Terms__c = objC.Special_Terms__c;
            Opp.add(opp1);
        }
     }
    IF(trigger.isDelete)
    {      
    FOR(Contract_Summary__c con :trigger.old) {
        Ids.add(con.Related_Opportunity__c);
    }
    
    Opportunity opp1 = [SELECT Id, Start_Date__c, End_Date__c
    FROM Opportunity
    WHERE Id IN :Ids];

        for(Contract_Summary__c objC : trigger.old)
        {
            opp1.Start_Date__c = null;
            opp1.End_Date__c = null;
            opp1.Current_Term_Months__c = null;
            opp1.Auto_Renew__c = FALSE;
            opp1.Term_for_Convenience__c = FALSE;
            opp1.Portal_SLAs__c = FALSE;
            opp1.Coaching_Performance_Guarantees__c = FALSE;
            opp1.Off_Shore_Resource_Restiction__c = FALSE;
            opp1.Delegated_Entity_Agreement__c = FALSE;
            opp1.Special_Terms__c = FALSE;
            Opp.add(opp1);
        }
     }
    IF(opp.size()>0)
    Update opp;
}