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
Steve KucklincaSteve Kucklinca 

Trigger to create a junction object record when text field value on 1 Parent is met

I have a junction object MerchOpps connected to both Opprtunity object and a custom object Merchant Application. When the Completed__c field (text) on the Merchant Application object reads 'Completed' I want to trigger a new record on the junction object MerchOpps. I had great help from th community where I was able to save the trigger here but it was not firing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
trigger createMerchOpps on Merchant_Application__c (after insert, after update)
{
    LIst<MerchOpps__c> MerchOppsList = new List<MerchOpps__c>();
    for(Merchant_Application__c Merchant_Application : Trigger.new)
    {
        // check status (ApI name check in your system for envelope status)
            if(Merchant_Application.Completed__c == 'Completed ')
        {
            // Please check api name of account on application and merchopps custom object. and also assign values in all required fields in below statment.
            MerchOppsList.add(new MerchOpps__c( Account__c = Merchant_Application.Account_Name__c, Name = 'TestOpp'));
        }
    }
    if (MerchOppsList.size() > 0)
    {
        insert MerchOppsList ;
    }

}

I then found this trigger below  through the answers page here which I edited and was able to save but caused this error

Error:Apex trigger MerchOppsList caused an unexpected exception, contact your administrator: MerchOppsList: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity, Merchant Application]: [Opportunity, Merchant Application]: Trigger.MerchOppsList: line 10, column 1

4
5
6
7
8
9
10
trigger MerchOppsList on Merchant_Application__c ( 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)
        ;       
MO.add(M);   
}   
insert MO;}

How do I merge these triggers to create the junction object record when the field value is met? I do not have much APEX experience so I am lost. Please help
Grazitti TeamGrazitti Team
Hi Steve Kucklinca, 

As you have created a Mater Detail Relationship Between  (MerchOpps and Opportunity object) and (Merchant Application. and MerchOpps) to make MerchOpps as a junction object.
and Master Detail Relationship create a Required Look up field on the Detail Object. So you need to assign the values to the these required fields in order to create a Record of  MerchOpps__c Object. 

So make following correction at line 7 of above code
MerchOpps__c        (   Account__c = ma.Account_Name__r.id , Opportunity__c = 'ASSIGN_OPPORTUNITY_ID', Merchant Application __c = ma.Id);


let us know if you have any question .
please don't Forget to Mark this as your best answer if it works fine for you

Regards,
Grazitti Team
Web: www.grazitti.com


Steve KucklincaSteve Kucklinca
Here is the error received when I try to update the Merchant Application
Apex trigger MerchOppsList caused an unexpected exception, contact your administrator: MerchOppsList: execution of AfterUpdate caused by: System.StringException: Invalid id: ASSIGN_OPPORTUNITY_ID: Trigger.MerchOppsList: line 5, column 1

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;}

where would I find this value to correct this?