You need to sign in to do that
Don't have an account?
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:
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;
}
Try this:
Hope this helps.:)
Thanks,
Devendra
All Answers
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
Hi Kiran,
I did try that previously, but I still get the same error message. Any other suggestions? Thanks.
Try this:
Hope this helps.:)
Thanks,
Devendra
Thanks for the updated code, but I still get the same error related to the Update opp; line. Any other suggestions?
Hi jneilan22,
My mistake.
The code should line should be update lstOpp;
Thanks,
Devendra
Can you please post your latest code and the error which you are receiving?
Thanks,
Devendra
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
Which are the fields from Opportunity are kept as required?
Please check that.
Thanks,
Devendra
The only fields required on the custom object are the Account Name and Related Opportunity Name, which are both pulled from the Opportunity record.
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!!
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