You need to sign in to do that
Don't have an account?
Steve Kucklinca
Help with fixing trigger to create a junction object record - keep getting errors Please Help!
I have the following code for a trigger in which I am trying to create aa junction object record when a field on one of the parent objects is updated Master Detail relation from Opportunity and Merchant_Application__c to junction object MerchOpps
What is beyong my level of skill, and thank you to the variety who have helped get me this far, is that this only incorporates 1 Master object as I get the error:
How do I incorporate both Master Objects?
Even were I to edit to this code
trigger MerchOppsList on Merchant_Application__c (after insert, after update){
List<MerchOpps__c> MO = new List<MerchOpps__c>();
for (Merchant_Application__c ma : Trigger.new) {
MerchOpps__c M = new
MerchOpps__c ( Account__c = ma.Account_Name__r.id , ChildofOpp__c = 'ASSIGN_OPPORTUNITY_ID', ChildofMA__c = ma.Id);
MO.add(M);
}
insert MO;}
I would get error indicating Assign_Opportunity_ID is invalid
where would I find this value to correct this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
trigger MerchOppsRecord on Merchant_Application__c ( after insert, after update){ for (Merchant_Application__c ma : Trigger.new) { if(ma.Completed__c != 'Completed '){ MerchOpps__c mo = new MerchOpps__c( ChildofMA__c = ma.Id ); insert mo; } } } |
What is beyong my level of skill, and thank you to the variety who have helped get me this far, is that this only incorporates 1 Master object as I get the error:
Apex script unhandled trigger exception by user/organization: 005A00000042sXs/00DK000000W47xw Source organization: 00DA0000000gi0U (null)
MerchOppsRecord: execution of AfterUpdate
caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity]: [Opportunity]
Trigger.MerchOppsRecord: line 13, column 1
How do I incorporate both Master Objects?
Even were I to edit to this code
trigger MerchOppsList on Merchant_Application__c (after insert, after update){
List<MerchOpps__c> MO = new List<MerchOpps__c>();
for (Merchant_Application__c ma : Trigger.new) {
MerchOpps__c M = new
MerchOpps__c ( Account__c = ma.Account_Name__r.id , ChildofOpp__c = 'ASSIGN_OPPORTUNITY_ID', ChildofMA__c = ma.Id);
MO.add(M);
}
insert MO;}
I would get error indicating Assign_Opportunity_ID is invalid
where would I find this value to correct this?
All Answers
"So you are trying to create a junction object record whenever either of the parent fields (in your case Opportunity & Merchant_Application__c) are updated?" This doesnt make sense to me.
A junction object by definition is the bridge between two objects to achieve a many-many relationship. For it to function, it must have both the parent ids present on it. In your case, to create MerchOpps__c record, you must have both Opportunity Id and Merchant_Application__c Id.
The trigger you wrote is on Merchant_Application__c, and you are not getting any Opportunity Id in that trigger. And you line of code
is trying to assign a String value to an Id field. which is not right. If you want your code to work, identify the right opportunity to which the record needs to be assigned.
But even before that, please make sure that your requirement is right
PS: I handwritten the code based on the above comments, if there are any compile errors or typos please correct them and run.