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 

What would be the Apex Class for this trigger?

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?

@anilbathula@@anilbathula@
Hi Steve,

try this test class,hope it works for you.

@IsTest
public class MerchOppsRecords _Test { 
 
    public static testmethod void Test() {

  Account a=new account();
  a.name='testaccount';
  //put all other mandatory fields on account
  insert a;

  Merchant_Application__c  mp=new Merchant_Application__c ();
  mp.name='mnane';
  mp.Account_Name__c=a.id;
  //put all other mandatory fields on Merchant_Application__c object
  insert mp;

}
}

Thanks
Anil.B
Steve KucklincaSteve Kucklinca
Anil

Thanks for your help.

Here is the test class I built from your instructions. Unfortunately when I check code coverage on the trigger it is still 0% with (0/12) next to it (assuming methods)  What could I be missing?

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