• Steve Kucklinca
  • NEWBIE
  • 20 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 10
    Questions
  • 32
    Replies
My trigger is firing perfectly in sandbox. However when I added lines 17-19 to limit when to fire the trigger (bold text) my coverage dropped from 100% (12/12) down to 68% (11/16) How do I edit the class to increase my coverage back to 75% +?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
trigger MerchOppsRecords on Merchant_Application__c (after update){

    Set<Id> accountIds = new Set<Id>();
    Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
    for(Merchant_Application__c ma : Trigger.new){
        accountIds.add(ma.Account_Name__c); 
    }
    
  
    for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
        accountOpportunityMap.put(opp.AccountId,opp);
    }
    
    List<MerchOpps__c> mOps = new List<MerchOpps__c>();
     
    for(Merchant_Application__c ma : Trigger.new){
         Merchant_Application__c oldma = Trigger.oldMap.get(ma.Id);
         if (ma.Completed__c !=oldma.Completed__c) {
             if (ma.Completed__c == 'Completed')

        if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
            MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
            mo.Name = ma.Name + '_Notification';
            mOps.add(mo); 
        }
    }
   
    insert mOps;  
    
}
}

Related class to test coverage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
@IsTest
public class MerchOppsRecordsTest { 
 
    public static testmethod void MO() {

  Account a=new account();
  a.name='testaccount';
  a.website='www.test.com';
  a.Base_Rate_Program__c='2.9%';
  a.socpl__Billing_Country__c='USA';
  a.Company_Description__c='testing';
  a.Incumbent_Processer__c='Startup';
  a.Current_Integration_Type__c='Hosted Pages';
  a.Primary_Investor_s_VC_s_Name_s__c='Self funded';
  a.Approval_Stage__c='Approved';
  
  //put all other mandatory fields on account
  insert a;
  
  Contact c=new contact ();
  c.FirstName='Bruce';
  c.LastName='Wayne';
  c.AccountId=a.id;
  c.email='Bruce.Wayne@DarkKnight.com';
  
  insert c;
  
  Merchant_Application__c  ma=new Merchant_Application__c ();
  ma.name='testaccount-1';
  ma.Account_Name__c=a.id;
  ma.Merchant_Application_Status__c='Approved';
  ma.MA__c='null';
  ma.Principal_Name__c=c.id;
  ma.Completed__c='';
  
  //put all other mandatory fields on Merchant_Application__c object
  insert ma;
  
  Opportunity o=new opportunity ();
  o.name='opp1';
  o.accountId=a.id;
  o.stagename='Signed';
  o.closedate=date.parse('06/23/2014');
  o.Go_Live_Date__c=date.parse('07/16/2014');
  o.P_T_Date__c=date.parse('10/31/2014');
  o.Next_Step__c='test';
  o.type='New Business';
  o.leadsource='Get Started';
  
  insert o;
  
  MerchOpps__c mo=new MerchOpps__c ();
  mo.name='Work';
  mo.ChildofMA__c=ma.id;
  mo.ChildofOpp__c=o.id;
  
  insert mo;  
  
  ma = [select name from merchant_application__c where id = :ma.id];
  update ma;

  o = [select name from Opportunity where id =:o.id];
  update o;
}
}
this code is firing in the sandbox and has 100% code coverage there and I have validated successfully (not yet deployed) in my production org. However I want to know if it can be edited (and how) to fire only when a specified field Completed__c on Merchant_Application__c object is updated with the value 'Completed'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
trigger MerchOppsRecords on Merchant_Application__c (after update){

    Set<Id> accountIds = new Set<Id>();
    Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
    for(Merchant_Application__c ma : Trigger.new){
        accountIds.add(ma.Account_Name__c);
    }
   
 
    for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
        accountOpportunityMap.put(opp.AccountId,opp);
    }
   
    List<MerchOpps__c> mOps = new List<MerchOpps__c>();
    
    for(Merchant_Application__c ma : Trigger.new){
       
        if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
            MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
            mOps.add(mo);
        }
    }
  
    insert mOps; 
   
}
I have the below trigger executing beautifully with 100% code coverage. Unfortunately when it creates a new record in the sandbox the Name of the record is the SFDC ID string referenced in the URL of the record. Is there any way to edit the code so readable text populates there instead of the ID string? For example using the name of one of the Parent objects (or combination of both) to easily identify the record

MerchOpps Name
a0aK0000004iSeQ
salesforce.com/a0aK0000004iSeQ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
trigger MerchOppsRecords on Merchant_Application__c (after update){

    Set<Id> accountIds = new Set<Id>();
    Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
    for(Merchant_Application__c ma : Trigger.new){
        accountIds.add(ma.Account_Name__c); 
    }
    
  
    for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
        accountOpportunityMap.put(opp.AccountId,opp);
    }
    
    List<MerchOpps__c> mOps = new List<MerchOpps__c>();
     
    for(Merchant_Application__c ma : Trigger.new){
        
        if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
            MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
            mOps.add(mo); 
        }
    }
   
    insert mOps;  
    
}
I get this error related to a Validation rule when I try to run a test on my Apex class (below) The fields related to the rule are bold. What am I doing wrong? If approval fields (Account and Merchant Application) are 'Approved' when Opportunity stage is 'Signed' why would I get the error from my validation when it meets criteria to not be an error?

Error Message System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please contact Risk team for validation that the Merchant Application has been Approved: [StageName]
 
Stack Trace Class.MerchOppsRecordsTest.MO: line 42, column 1


@IsTest
public class MerchOppsRecordsTest { 
 
    public static testmethod void MO() {

  Account a=new account();
  a.name='testaccount';
  a.website='www.test.com';
  a.Base_Rate_Program__c='2.9%';
  a.socpl__Billing_Country__c='USA';
  a.Company_Description__c='testing';
  a.Incumbent_Processer__c='Startup';
  a.Current_Integration_Type__c='Hosted Pages';
  a.Primary_Investor_s_VC_s_Name_s__c='Self funded';
  a.Approval_Stage__c='Approved';
  
  //put all other mandatory fields on account
  insert a;
  
    Merchant_Application__c  ma=new Merchant_Application__c ();
  ma.name='testaccount-1';
  ma.Account_Name__c=a.id;
  ma.Merchant_Application_Status__c='Approved';
  ma.MA__c='null';
  ma.Principal_First_Name__c='Bruce';
  ma.Principal_Last_Name__c='Wayne';
  ma.Completed__c='Completed';
  
  //put all other mandatory fields on Merchant_Application__c object
  insert ma;
  
  Opportunity o=new opportunity ();
  o.name='opp1';
  o.stagename='Signed';
  o.closedate=date.parse('06/23/2014');
  o.Go_Live_Date__c=date.parse('07/16/2014');
  o.P_T_Date__c=date.parse('10/31/2014');
  o.Next_Step__c='test';
  o.type='New Business';
  o.leadsource='Get Started';
  
  insert o;
  
  MerchOpps__c mo=new MerchOpps__c ();
  mo.name='Work';
  mo.ChildofMA__c=ma.id;
  mo.ChildofOpp__c=o.id;
  
  insert mo;  
  
  ma = [select name from merchant_application__c where id = :ma.id];
update ma;

  o = [select name from Opportunity where id =:o.id];
  update o;
}
}
Here is the trigger I am trying to get code coverage for. When I turn off validation rules I get 75%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
trigger MerchOppsRecords on Merchant_Application__c (after update){

    Set<Id> accountIds = new Set<Id>();
    Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
    for(Merchant_Application__c ma : Trigger.new){
        accountIds.add(ma.Account_Name__c); 
    }
    
  
    for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
        accountOpportunityMap.put(opp.AccountId,opp);
    }
    
    List<MerchOpps__c> mOps = new List<MerchOpps__c>();
     
    for(Merchant_Application__c ma : Trigger.new){
        
        if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
            MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
            mOps.add(mo); 
        }
    }
   
    insert mOps;  
    
}
I have the following trigger firing correctly and with community help I built a test class that I thought would apply. However when I check code coverage on the trigger it reads 0% (0/12) even though I ran the test for the class which passed (1/1) What am I missing? How do I edit class to get at least 75% coverage? I'm lost!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
trigger MerchOppsRecords on Merchant_Application__c (after update){

    Set<Id> accountIds = new Set<Id>();
    Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
    // Get the parent Account Ids from all the Merchant Application records,
    for(Merchant_Application__c ma : Trigger.new){
        accountIds.add(ma.Account_Name__c); // Assuming Account__c is the fields that maps to the Account Object
    }
    
    // Query the Opportunity using AccountId and build a map of AccountId to the Opportunity record
    // Here the assumption is that One account is tied to only one Opportunity. But if you have multiple opportunities for a single account, the logic must identify the right opportunity
    for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
        accountOpportunityMap.put(opp.AccountId,opp);
    }
    
    List<MerchOpps__c> mOps = new List<MerchOpps__c>();
    
    // Iterate again to build the Junction Object Records, 
    for(Merchant_Application__c ma : Trigger.new){
        
        if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
            MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
            mOps.add(mo); // Add the records to the list
        }
    }
    
    // Note that since this trigger is on both After Insert, After Update, and there is no validation to check if the child records already exists for that Opportunity & Merchant Application combination, it inserts child records everytime something gets changed on Merchant Application
    
    insert mOps; // Insert the junction object records. 
    
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@IsTest
public class MerchOppsRecordsTest { 
 
    public static testmethod void Test() {

  Account a=new account();
  a.name='testaccount';
  a.website='www.test.com';
  a.Base_Rate_Program__c='2.9%';
  a.socpl__Billing_Country__c='USA';
  a.Company_Description__c='testing';
  
  
  //put all other mandatory fields on account
  insert a;
  
  Opportunity o=new opportunity ();
  o.name='opp1';
  o.stagename='Signed';
  o.closedate=date.parse('06/23/2014');
  o.Go_Live_Date__c=date.parse('07/16/2014');
  o.P_T_Date__c=date.parse('10/31/2014');
  o.Next_Step__c='test';
  o.type='New Business';
  
  
  insert o;
   
  Merchant_Application__c  ma=new Merchant_Application__c ();
  ma.name='testaccount-1';
  ma.Account_Name__c=a.id;
  ma.Merchant_Application_Status__c='Open';
  ma.MA__c='null';
  ma.Principal_First_Name__c='Bruce';
  ma.Principal_Last_Name__c='Wayne';
  //put all other mandatory fields on Merchant_Application__c object
  insert ma;
  
  MerchOpps__c mo=new MerchOpps__c ();
  mo.name='Work';
  mo.ChildofMA__c=ma.id;
  mo.ChildofOpp__c=o.id;
  
  insert mo;  
}
}
I understand writing the trigger - but having a sales background as an SFDC admin and I am not a coding engineer in any sense. What would be the test class to create code coverage for this trigger that fires successfully in the sandbox?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
trigger MerchOppsRecords on Merchant_Application__c (after update){

    Set<Id> accountIds = new Set<Id>();
    Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
    // Get the parent Account Ids from all the Merchant Application records,
    for(Merchant_Application__c ma : Trigger.new){
        accountIds.add(ma.Account_Name__c); // Assuming Account__c is the fields that maps to the Account Object
    }
    
    // Query the Opportunity using AccountId and build a map of AccountId to the Opportunity record
    // Here the assumption is that One account is tied to only one Opportunity. But if you have multiple opportunities for a single account, the logic must identify the right opportunity
    for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
        accountOpportunityMap.put(opp.AccountId,opp);
    }
    
    List<MerchOpps__c> mOps = new List<MerchOpps__c>();
    
    // Iterate again to build the Junction Object Records, 
    for(Merchant_Application__c ma : Trigger.new){
        
        if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
            MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
            mOps.add(mo); // Add the records to the list
        }
    }
    
    // Note that since this trigger is on both After Insert, After Update, and there is no validation to check if the child records already exists for that Opportunity & Merchant Application combination, it inserts child records everytime something gets changed on Merchant Application
    
    insert mOps; // Insert the junction object records. 
    
}

 

I lack experience in creating classes and test records so all instruction and assistance creating this class I would appreciate (there is no related class that would currently cover this code)

Also, when the new record is created the name on that record is the SFDC ID string as I mentioned - this would be an example a0aK0000004iSek and I would prefer a more conventional naming structure aligned with the Account name or Opportunity name ('Opp 1' for example)

I have a field on Merchant_Application "Completed__c" and I would only want this trigger to fire if 'Completed' was the value entered in the field. Is this possible?

I have the following trigger working in my sandbox (much thanks to Kartik and others for your help) I am wondering how do I get code coverage as I thought I read for record creation a test class was not necessary? Also how do I change the naming convention of the newly created record as I think it pulls line 22 ChildofMA ma.Id as the name and I would prefer either using the opportunity name or Merchant_Application name. And finally, is it possible to limit the record creation based on a set value of a field from Merchant_Application__c object?

01 trigger MerchOppsRecord on Merchant_Application__c ( after insert, after update){
02  
03     Set<Id> accountIds = new Set<Id>();
04     Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
05     // Get the parent Account Ids from all the Merchant Application records,
06     for(Merchant_Application__c ma : Trigger.new){
07         accountIds.add(ma.Account__c); // Assuming Account__c is the fields that maps to the Account Object
08     }
09     
10     // Query the Opportunity using AccountId and build a map of AccountId to the Opportunity record
11     // Here the assumption is that One account is tied to only one Opportunity. But if you have multiple opportunities for a single account, the logic must identify the right opportunity
12     for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
13         accountOpportunityMap.put(opp.AccountId,opp);
14     }
15     
16     List<MerchOpps__c> mOps = new List<MerchOpps__c>();
17     
18     // Iterate again to build the Junction Object Records,
19     for(Merchant_Application__c ma : Trigger.new){
20         
21         if(accountOpportunityMap.containsKey(ma.Account__c)){
22             MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account__c), ChildofMA__c = ma.Id);
23             mOps.add(mo); // Add the records to the list
24         }
25     }
26      
27     // Note that since this trigger is on both After Insert, After Update, and there is no validation to check if the child records already exists for that Opportunity & Merchant Application combination, it inserts child records everytime something gets changed on Merchant Application
28     
29     insert mOps; // Insert the junction object records.
30     
31 }
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

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?
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
I am relatively new to Apex triggers and classes. I have 2 Custom Objects Merchant Applications__c and MerchOpps__c what I need is to create a new MerchOpps__c record when a field value on Merchant Applications__c is met. As the only other trigger I ever deployed was code I lifted from another admin and I don't have ability to take the Developer course, I am hoping some of you will pity me and help me with this trigger and class. Basically, if field "Envelope Status" = Completed on Merchant Applications__c, I need a MerchOpps__c record created using Account Name for the related Lookup field.

I figure it may be similar to this example I found here. But I still wouldn't be able to determine the Class I would have to write.

trigger CreateClass on Opportunity (after insert , before update)
02 {
03     if(!myStaticClass.flag)
04     {
05         List<CS__c> listCS = new List<CS__c>();
06         
07         for(Opportunity o : trigger.new)
08         {
09             if(Trigger.isUpdate)
10             {
11                 if(o.StageName == 'Closed Won' && o.StageName != trigger.oldMap.get(o.Id).StageName)
12                 {
13                   listCS.add(new CS__c(name = 'Addendum '+ o.Name + ' Closed' , Opportunity__c = o.id));
14                 }
15             }
16             if(Trigger.isInsert)
17             {
18                 if(o.StageName == 'Closed Won')
19                 {
20                   listCS.add(new CS__c(name = 'Addendum '+ o.Name + ' Closed' , Opportunity__c = o.id));
21                 }
22             }
23         }
24     
25         if(listCS.size() > 0)
26         {
27             insert listCS;
28         }
29         myStaticClass.flag = true ;
30     }
31 }
this code is firing in the sandbox and has 100% code coverage there and I have validated successfully (not yet deployed) in my production org. However I want to know if it can be edited (and how) to fire only when a specified field Completed__c on Merchant_Application__c object is updated with the value 'Completed'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
trigger MerchOppsRecords on Merchant_Application__c (after update){

    Set<Id> accountIds = new Set<Id>();
    Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
    for(Merchant_Application__c ma : Trigger.new){
        accountIds.add(ma.Account_Name__c);
    }
   
 
    for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
        accountOpportunityMap.put(opp.AccountId,opp);
    }
   
    List<MerchOpps__c> mOps = new List<MerchOpps__c>();
    
    for(Merchant_Application__c ma : Trigger.new){
       
        if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
            MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
            mOps.add(mo);
        }
    }
  
    insert mOps; 
   
}
I have the below trigger executing beautifully with 100% code coverage. Unfortunately when it creates a new record in the sandbox the Name of the record is the SFDC ID string referenced in the URL of the record. Is there any way to edit the code so readable text populates there instead of the ID string? For example using the name of one of the Parent objects (or combination of both) to easily identify the record

MerchOpps Name
a0aK0000004iSeQ
salesforce.com/a0aK0000004iSeQ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
trigger MerchOppsRecords on Merchant_Application__c (after update){

    Set<Id> accountIds = new Set<Id>();
    Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
    for(Merchant_Application__c ma : Trigger.new){
        accountIds.add(ma.Account_Name__c); 
    }
    
  
    for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
        accountOpportunityMap.put(opp.AccountId,opp);
    }
    
    List<MerchOpps__c> mOps = new List<MerchOpps__c>();
     
    for(Merchant_Application__c ma : Trigger.new){
        
        if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
            MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
            mOps.add(mo); 
        }
    }
   
    insert mOps;  
    
}
I get this error related to a Validation rule when I try to run a test on my Apex class (below) The fields related to the rule are bold. What am I doing wrong? If approval fields (Account and Merchant Application) are 'Approved' when Opportunity stage is 'Signed' why would I get the error from my validation when it meets criteria to not be an error?

Error Message System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please contact Risk team for validation that the Merchant Application has been Approved: [StageName]
 
Stack Trace Class.MerchOppsRecordsTest.MO: line 42, column 1


@IsTest
public class MerchOppsRecordsTest { 
 
    public static testmethod void MO() {

  Account a=new account();
  a.name='testaccount';
  a.website='www.test.com';
  a.Base_Rate_Program__c='2.9%';
  a.socpl__Billing_Country__c='USA';
  a.Company_Description__c='testing';
  a.Incumbent_Processer__c='Startup';
  a.Current_Integration_Type__c='Hosted Pages';
  a.Primary_Investor_s_VC_s_Name_s__c='Self funded';
  a.Approval_Stage__c='Approved';
  
  //put all other mandatory fields on account
  insert a;
  
    Merchant_Application__c  ma=new Merchant_Application__c ();
  ma.name='testaccount-1';
  ma.Account_Name__c=a.id;
  ma.Merchant_Application_Status__c='Approved';
  ma.MA__c='null';
  ma.Principal_First_Name__c='Bruce';
  ma.Principal_Last_Name__c='Wayne';
  ma.Completed__c='Completed';
  
  //put all other mandatory fields on Merchant_Application__c object
  insert ma;
  
  Opportunity o=new opportunity ();
  o.name='opp1';
  o.stagename='Signed';
  o.closedate=date.parse('06/23/2014');
  o.Go_Live_Date__c=date.parse('07/16/2014');
  o.P_T_Date__c=date.parse('10/31/2014');
  o.Next_Step__c='test';
  o.type='New Business';
  o.leadsource='Get Started';
  
  insert o;
  
  MerchOpps__c mo=new MerchOpps__c ();
  mo.name='Work';
  mo.ChildofMA__c=ma.id;
  mo.ChildofOpp__c=o.id;
  
  insert mo;  
  
  ma = [select name from merchant_application__c where id = :ma.id];
update ma;

  o = [select name from Opportunity where id =:o.id];
  update o;
}
}
Here is the trigger I am trying to get code coverage for. When I turn off validation rules I get 75%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
trigger MerchOppsRecords on Merchant_Application__c (after update){

    Set<Id> accountIds = new Set<Id>();
    Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
    for(Merchant_Application__c ma : Trigger.new){
        accountIds.add(ma.Account_Name__c); 
    }
    
  
    for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
        accountOpportunityMap.put(opp.AccountId,opp);
    }
    
    List<MerchOpps__c> mOps = new List<MerchOpps__c>();
     
    for(Merchant_Application__c ma : Trigger.new){
        
        if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
            MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
            mOps.add(mo); 
        }
    }
   
    insert mOps;  
    
}
I have the following trigger firing correctly and with community help I built a test class that I thought would apply. However when I check code coverage on the trigger it reads 0% (0/12) even though I ran the test for the class which passed (1/1) What am I missing? How do I edit class to get at least 75% coverage? I'm lost!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
trigger MerchOppsRecords on Merchant_Application__c (after update){

    Set<Id> accountIds = new Set<Id>();
    Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
    // Get the parent Account Ids from all the Merchant Application records,
    for(Merchant_Application__c ma : Trigger.new){
        accountIds.add(ma.Account_Name__c); // Assuming Account__c is the fields that maps to the Account Object
    }
    
    // Query the Opportunity using AccountId and build a map of AccountId to the Opportunity record
    // Here the assumption is that One account is tied to only one Opportunity. But if you have multiple opportunities for a single account, the logic must identify the right opportunity
    for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
        accountOpportunityMap.put(opp.AccountId,opp);
    }
    
    List<MerchOpps__c> mOps = new List<MerchOpps__c>();
    
    // Iterate again to build the Junction Object Records, 
    for(Merchant_Application__c ma : Trigger.new){
        
        if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
            MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
            mOps.add(mo); // Add the records to the list
        }
    }
    
    // Note that since this trigger is on both After Insert, After Update, and there is no validation to check if the child records already exists for that Opportunity & Merchant Application combination, it inserts child records everytime something gets changed on Merchant Application
    
    insert mOps; // Insert the junction object records. 
    
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@IsTest
public class MerchOppsRecordsTest { 
 
    public static testmethod void Test() {

  Account a=new account();
  a.name='testaccount';
  a.website='www.test.com';
  a.Base_Rate_Program__c='2.9%';
  a.socpl__Billing_Country__c='USA';
  a.Company_Description__c='testing';
  
  
  //put all other mandatory fields on account
  insert a;
  
  Opportunity o=new opportunity ();
  o.name='opp1';
  o.stagename='Signed';
  o.closedate=date.parse('06/23/2014');
  o.Go_Live_Date__c=date.parse('07/16/2014');
  o.P_T_Date__c=date.parse('10/31/2014');
  o.Next_Step__c='test';
  o.type='New Business';
  
  
  insert o;
   
  Merchant_Application__c  ma=new Merchant_Application__c ();
  ma.name='testaccount-1';
  ma.Account_Name__c=a.id;
  ma.Merchant_Application_Status__c='Open';
  ma.MA__c='null';
  ma.Principal_First_Name__c='Bruce';
  ma.Principal_Last_Name__c='Wayne';
  //put all other mandatory fields on Merchant_Application__c object
  insert ma;
  
  MerchOpps__c mo=new MerchOpps__c ();
  mo.name='Work';
  mo.ChildofMA__c=ma.id;
  mo.ChildofOpp__c=o.id;
  
  insert mo;  
}
}
I understand writing the trigger - but having a sales background as an SFDC admin and I am not a coding engineer in any sense. What would be the test class to create code coverage for this trigger that fires successfully in the sandbox?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
trigger MerchOppsRecords on Merchant_Application__c (after update){

    Set<Id> accountIds = new Set<Id>();
    Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
    // Get the parent Account Ids from all the Merchant Application records,
    for(Merchant_Application__c ma : Trigger.new){
        accountIds.add(ma.Account_Name__c); // Assuming Account__c is the fields that maps to the Account Object
    }
    
    // Query the Opportunity using AccountId and build a map of AccountId to the Opportunity record
    // Here the assumption is that One account is tied to only one Opportunity. But if you have multiple opportunities for a single account, the logic must identify the right opportunity
    for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
        accountOpportunityMap.put(opp.AccountId,opp);
    }
    
    List<MerchOpps__c> mOps = new List<MerchOpps__c>();
    
    // Iterate again to build the Junction Object Records, 
    for(Merchant_Application__c ma : Trigger.new){
        
        if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
            MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
            mOps.add(mo); // Add the records to the list
        }
    }
    
    // Note that since this trigger is on both After Insert, After Update, and there is no validation to check if the child records already exists for that Opportunity & Merchant Application combination, it inserts child records everytime something gets changed on Merchant Application
    
    insert mOps; // Insert the junction object records. 
    
}

 

I lack experience in creating classes and test records so all instruction and assistance creating this class I would appreciate (there is no related class that would currently cover this code)

Also, when the new record is created the name on that record is the SFDC ID string as I mentioned - this would be an example a0aK0000004iSek and I would prefer a more conventional naming structure aligned with the Account name or Opportunity name ('Opp 1' for example)

I have a field on Merchant_Application "Completed__c" and I would only want this trigger to fire if 'Completed' was the value entered in the field. Is this possible?

I have the following trigger working in my sandbox (much thanks to Kartik and others for your help) I am wondering how do I get code coverage as I thought I read for record creation a test class was not necessary? Also how do I change the naming convention of the newly created record as I think it pulls line 22 ChildofMA ma.Id as the name and I would prefer either using the opportunity name or Merchant_Application name. And finally, is it possible to limit the record creation based on a set value of a field from Merchant_Application__c object?

01 trigger MerchOppsRecord on Merchant_Application__c ( after insert, after update){
02  
03     Set<Id> accountIds = new Set<Id>();
04     Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
05     // Get the parent Account Ids from all the Merchant Application records,
06     for(Merchant_Application__c ma : Trigger.new){
07         accountIds.add(ma.Account__c); // Assuming Account__c is the fields that maps to the Account Object
08     }
09     
10     // Query the Opportunity using AccountId and build a map of AccountId to the Opportunity record
11     // Here the assumption is that One account is tied to only one Opportunity. But if you have multiple opportunities for a single account, the logic must identify the right opportunity
12     for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
13         accountOpportunityMap.put(opp.AccountId,opp);
14     }
15     
16     List<MerchOpps__c> mOps = new List<MerchOpps__c>();
17     
18     // Iterate again to build the Junction Object Records,
19     for(Merchant_Application__c ma : Trigger.new){
20         
21         if(accountOpportunityMap.containsKey(ma.Account__c)){
22             MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account__c), ChildofMA__c = ma.Id);
23             mOps.add(mo); // Add the records to the list
24         }
25     }
26      
27     // Note that since this trigger is on both After Insert, After Update, and there is no validation to check if the child records already exists for that Opportunity & Merchant Application combination, it inserts child records everytime something gets changed on Merchant Application
28     
29     insert mOps; // Insert the junction object records.
30     
31 }
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

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?