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
TempleHoffTempleHoff 

How do you generate test data for a trigger that includes D&B Company records?

I've writen a trigger so that when I import an Account record from Data.com or clean one the trigger will use the Parent DUNS on the associated D&B Company record to see if I already have the parent in my org.  If I do, then it sets the parent automatically.  The trigger works great and is bulkified.  But now I'm stuck.  How do I generate mock D&B Company records in a Test Method to test against?  Do I have to 'see all data' in the TestMethod test from a known real D&B Company record?
Best Answer chosen by TempleHoff
TempleHoffTempleHoff
So, I figured out a clunky way to work around not being able to generate test D&B Company records.  I have a formula fields on Account that use to point to the parent DUNS numbers on the D&B Company record, ( DandbCompany.ParentOrHqDunsNumber ).  Of course, in a test method when I create a test account, this formula will be null because there is no D&B Company record associated.  So, what I did was to add a custom text field to Account called  testingField.  I actually add one of these to all my objects.  It comes in handy to trigger mass updates.  Then I change the Formula to include an IF statement so that it only reflects the parent DUNS from the D&B Company record if testingField != 'AccountTriggerTest'.  If it is = AccountTriggerTest then make the formula reflect the Account Name.  Then in my test method when I generate my test Accounts I set testingField = AccountTriggerTest and set the name of the test Account to be the same as the test DUNS  I am using (the DUNS of the test Parent).  Its a cheesy work-around that adds code to my formula field that is only useful during test execution.  But it works.

All Answers

Blake TanonBlake Tanon
post your trigger and test method please
TempleHoffTempleHoff
So, I figured out a clunky way to work around not being able to generate test D&B Company records.  I have a formula fields on Account that use to point to the parent DUNS numbers on the D&B Company record, ( DandbCompany.ParentOrHqDunsNumber ).  Of course, in a test method when I create a test account, this formula will be null because there is no D&B Company record associated.  So, what I did was to add a custom text field to Account called  testingField.  I actually add one of these to all my objects.  It comes in handy to trigger mass updates.  Then I change the Formula to include an IF statement so that it only reflects the parent DUNS from the D&B Company record if testingField != 'AccountTriggerTest'.  If it is = AccountTriggerTest then make the formula reflect the Account Name.  Then in my test method when I generate my test Accounts I set testingField = AccountTriggerTest and set the name of the test Account to be the same as the test DUNS  I am using (the DUNS of the test Parent).  Its a cheesy work-around that adds code to my formula field that is only useful during test execution.  But it works.
This was selected as the best answer
Lisa KattawarLisa Kattawar
We want to do the same thing in our org.

I'm not a developer (and we don't have a SF developer).  Are you willing to share the code for the trigger and test class you wrote?
TempleHoffTempleHoff
Here the core chunk.  It requires a custom Account field called D_B_Parent_DUNS__c  as a formula like: DandbCompany.ParentOrHqDunsNumber.   This code is not bullet proof.  It won't cover all scenarios.  

trigger AccountMain on Account (before insert, before update, after insert, after update) {

//Set Parent based on D&B Record
    if((trigger.isInsert || trigger.isUpdate) && trigger.isBefore){
        Map<String,string> ParentDUN_Acc_Map = new Map<String,string>{};
        List<String> ParentDUN_List = new List<String>();
       
        //cycle through new or updated accounts accounts, determine candidates, record their ParentDUN to the list and them to the ParentDUN to Acc ID Map
        for(Account Acc : trigger.new)
            {
            if(Acc.D_B_Parent_DUNS__c != null && Acc.D_B_Parent_DUNS__c != Acc.DunsNumber && Acc.Parent == null) {
                ParentDUN_List.Add(Acc.D_B_Parent_DUNS__c);
                string myid = Acc.id;
                ParentDUN_Acc_Map.put(Acc.D_B_Parent_DUNS__c, myid);
            }
         }  
        //Find all the accounts to be used as parents
        List<Account> AccList = [Select DunsNumber, Id From Account where DunsNumber In : ParentDUN_List];
       
        //Create a map of parents to be used
        Map<String, String> ParentFoundDUN_ID_Map = new Map<String, String>{};      
        for(Account Accs : AccList )
            {
                 ParentFoundDUN_ID_Map.put(Accs.DunsNumber, Accs.id);
            }

        // cycle through the new list and make any parent inserts needed
        for(Account AccU : trigger.new)
            {
            String Uparent = ParentFoundDUN_ID_Map.get(accU.D_B_Parent_DUNS__c);
            if(uParent != null) {AccU.parentid = Uparent;}
            }

    }
}
Lisa KattawarLisa Kattawar
Thanks for posting!!  I completely understand this may not cover all cases, but it will be a great place for us to start!