You need to sign in to do that
Don't have an account?
Steve Kucklinca
trigger code coverage for new record creation and naming of new record? ready to deploy
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 |
} |
For coverage just create a record in test method and then update that should cover both your contexts.
Could you please elaborate more on these queries:
"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?"
I am not sure what you are trying to state here.
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?