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 

Error Message When Trying to Update Master Record from Child Record

Hello,

 

I created a custom object called Contract_Summary__c that is the Detail record in a Master-Detail relationship with the Opportunity record.  I also created a simple custom button on the Opportunity record that creates the Contract_Summary__c record and populates a few of the fields with values from the Opportunity.  I created the trigger below to write back the Contract_Effective_Date__c and the Contract_Expiration_Date__c back to the Opportunity when the Contract_Summary__c record is created or edited.  However, when I try to save the new record I receive the following error:

 

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger ContractUpdates caused an unexpected exception, contact your administrator: ContractUpdates: execution of BeforeInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 0067000000MEERtAAP; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity Identifier]: [Opportunity Identifier]: Trigger.ContractUpdates: line 19, column 1

 

My trigger is below:

 

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

System.debug('Trigger@@@@@@@@@@@@@@');

    list<Opportunity> opp = new list<Opportunity>();    

    FOR(Contract_Summary__c con :trigger.new) {
    
    Opportunity opp1 = [SELECT Id
    FROM Opportunity
    WHERE Id =:con.Related_Opportunity__c];
    System.debug('@@@@@@@@@@@@@@'+con );
    
    IF (con.Current_Effective_Date__c!=null) {
    
        opp1.Start_Date__c = con.Current_Effective_Date__c;
        opp1.End_Date__c = con.Current_Expiration_Date__c;
    update opp1;
    }
    }
    IF(opp.size()>0)
    Update opp;

}

Best Answer chosen by Admin (Salesforce Developers) 
Devendra@SFDCDevendra@SFDC

 

Try this:

 

trigger ContractUpdates on Contract_Summary__c (before insert, before update) 
{
//Update Opportunity fields from values in Contract Summary object       
    
    list<Opportunity> lstOpp = new list<Opportunity>();  
    set<Id> Ids = new Set <Id>();           
    for (Contract_Summary__c con: Trigger.new) {             
        Ids.add(con.Related_Opportunity__c); 
    }
    
    Opportunity opp = [SELECT Id, Candidate_Description__c,Start_Date__c,End_Date__c FROM Opprtunity WHERE Id IN :Ids])  
    
    if(trigger.isInsert || trigger.isUpdate)
    {   
        for(Contract_Summary__c objC : trigger.new) 
        {
            IF (con.Current_Effective_Date__c!=null) 
            {
                opp.Start_Date__c = con.Current_Effective_Date__c;
                opp.End_Date__c = con.Current_Expiration_Date__c;
                lstOpp.add(opp);
            } 
        }
   }
   IF(lstOpp.size()>0)
         Update opp;
}

 

Hope this helps.:)

 

Thanks,

Devendra

All Answers

kiranmutturukiranmutturu

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

System.debug('Trigger@@@@@@@@@@@@@@');

    list<Opportunity> opp = new list<Opportunity>();    

    FOR(Contract_Summary__c con :trigger.new) {
    
    Opportunity opp1 = [SELECT Id
    FROM Opportunity
    WHERE Id =:con.Related_Opportunity__c];
    System.debug('@@@@@@@@@@@@@@'+con );
    
    IF (con.Current_Effective_Date__c!=null) {
    
        opp1.Start_Date__c = con.Current_Effective_Date__c;
        opp1.End_Date__c = con.Current_Expiration_Date__c;
    update opp1;
    }
    }
    IF(opp.size()>0)
    Update opp;

} 

remove the red color code block

jneilan22jneilan22

Hi Kiran,

 

I did try that previously, but I still get the same error message.  Any other suggestions?  Thanks.

Devendra@SFDCDevendra@SFDC

 

Try this:

 

trigger ContractUpdates on Contract_Summary__c (before insert, before update) 
{
//Update Opportunity fields from values in Contract Summary object       
    
    list<Opportunity> lstOpp = new list<Opportunity>();  
    set<Id> Ids = new Set <Id>();           
    for (Contract_Summary__c con: Trigger.new) {             
        Ids.add(con.Related_Opportunity__c); 
    }
    
    Opportunity opp = [SELECT Id, Candidate_Description__c,Start_Date__c,End_Date__c FROM Opprtunity WHERE Id IN :Ids])  
    
    if(trigger.isInsert || trigger.isUpdate)
    {   
        for(Contract_Summary__c objC : trigger.new) 
        {
            IF (con.Current_Effective_Date__c!=null) 
            {
                opp.Start_Date__c = con.Current_Effective_Date__c;
                opp.End_Date__c = con.Current_Expiration_Date__c;
                lstOpp.add(opp);
            } 
        }
   }
   IF(lstOpp.size()>0)
         Update opp;
}

 

Hope this helps.:)

 

Thanks,

Devendra

This was selected as the best answer
jneilan22jneilan22

Thanks for the updated code, but I still get the same error related to the Update opp; line.  Any other suggestions?

Devendra@SFDCDevendra@SFDC

Hi jneilan22,

 

My mistake.

 

The code should line should be update lstOpp;

 

Thanks,

Devendra


jneilan22jneilan22

Hi Devendra,

 

Thanks, but that still doesn't remove the error.

Devendra@SFDCDevendra@SFDC

 

Can you please post your latest code and the error which you are receiving?

 

Thanks,

Devendra

jneilan22jneilan22

Hi Devendra,

 

My code is here:

 

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)
        {
            IF (objC.Current_Effective_Date__c!=null)
            {

            opp1.Start_Date__c = objC.Current_Effective_Date__c;
            opp1.End_Date__c = objC.Current_Expiration_Date__c;
            Opp.add(opp1);
            }
        }
     }
   IF(opp.size()>0)
         Update opp;
}

 

 

 

And the error I am receiving is here:

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ContractUpdates caused an unexpected exception, contact your administrator: ContractUpdates: execution of BeforeInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 0067000000MEERtAAP; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity Identifier]: [Opportunity Identifier]: Trigger.ContractUpdates: line 29, column 1

 

 

Devendra@SFDCDevendra@SFDC

 

Which are the fields from Opportunity are kept as required?

Please check that.

 

Thanks,

Devendra

jneilan22jneilan22

The only fields required on the custom object are the Account Name and Related Opportunity Name, which are both pulled from the Opportunity record.

jneilan22jneilan22

Devendra,

 

I found the issue.  As you said there was a mandatory field on the Opportunity that was not being populated.  It was not on the page layouts and was created by my predecessor.  Thanks for you assistance, the code you posted works!!

 

 

Devendra@SFDCDevendra@SFDC

 

Hi jneilan22,

 

I am glad, the given solution has worked for you..:)

 

If the solution is working then please Mark the solution, as others can be benefited.

 

Thanks,

Devendra