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
Craig GroveCraig Grove 

Assistance with test class

I've created a trigger to create child objects when a lookup field is changed on the parent object.  Instance: Parent Object =Laptop__c; Child object=Laptop_Update__c; Field=Location__c(Account). I was able to create and deploy successfully a trigger and class based upon creating a new Laptop and triggering the creatiion of the laptop updates, but when attempting to create a similar trigger and class based on information update on Laptop__c I ran into issues. Any assistance is appreciated.

Trigger:

trigger CreateIHSLaptopUpdate2 on Laptop__c (after Update) {
    List<IHS_Laptop_Updates__c> Updates = new List<IHS_Laptop_Updates__c>();
  
    //For each Laptop processed by the trigger, add a new
    //Update record for the specified user.
    //Note that Trigger.New is a list of all new Laptops
    //That are being created.
  
    for (Laptop__c updatedLaptop: Trigger.New) {
        if(updatedLaptop.Location__c !='0017000000bSTpd')
        if(updatedLaptop.Location__c !='0017000000YRiPy'){
            Updates.add(new IHS_Laptop_Updates__c(
                Type__c = 'AEP Software',
                Version__c = '2.36B ALI',
                IHS_Laptop__c = updatedLaptop.Id));
            Updates.add(new IHS_Laptop_Updates__c(
                Type__c = 'IHS Auto Software Update',
                Version__c = '1.55',
                IHS_Laptop__c = updatedLaptop.Id));
            Updates.add(new IHS_Laptop_Updates__c(
                Type__c = 'Central Internet Site',
                Version__c = 'N/A',
                IHS_Laptop__c = updatedLaptop.Id));
            Updates.add(new IHS_Laptop_Updates__c(
                Type__c = 'Manual',
                Version__c = '2.32',
                IHS_Laptop__c = updatedLaptop.Id));
        }
    }

    insert Updates;
}

Test Class:

@IsTest
private class TestTrigger3 {

    static testmethod void testtrigger() {
   
        //Add Account
        Account A = new Account();
            A.Name = 'Test';
           
        insert A;
   
        //Add Laptop
        Laptop__c L = new Laptop__c();
            L.Name = 'Test';
       
        insert L;
       
        //Update Laptop
            L.Location__c = a.id;
        update L;
    }
}
Best Answer chosen by Craig Grove
Dominic Blythe 18Dominic Blythe 18
Hi Craig. A Custom setting is a like a Custom Object, but it has special properties that make it the ideal place to keep application settings like "which accounts automatically need stuff added to their laptops". In the Salesforce UI, go to Setup > Develop > Custom Settings to create and manage them. Full information is here: Custom Settings Overview - Salesforce.com Then in the Trigger you can retrieve your custom settings, check if your Account Id is in the list, and do your trigger thing. An alternative approach is to flag the account itself with - a new checkbox for "Laptops need extra things" - or maybe a picklist "Account extra things type" or some other way of identifying the Accounts in question. Maybe you even already have that information on the Account? If the information is there as a (new or existing) field on the Account, you can create a Cross-Object Formula on the Laptop object to bring the info into the trigger. Let's say you have the field Account.Laptop_Extras__c as a checkbox. You can add a formula field Laptop__c.Laptop_Extras__c where the formula is Location__r.Laptop_Extras__c This will pull the Laptop Extras from the Location Account onto your Laptop record. Then your code simply has to do for (Laptop__c updatedLaptop: Trigger.New) { if (updatedLaptop.Laptop_Extras__c) { ... do stuff... Sorry if I'm preaching to the choir, but you said you just started with Apex so I'm being basic. Another comment - it looks like your trigger is going to add more and more IHS_Laptop_Updates every time the Laptop gets updated. Should you check if they exist already before adding more?

All Answers

Dominic Blythe 18Dominic Blythe 18

HI Craig, what issues did you run into? 

As an aside, you don't necessarily need 2 triggers. You could run the same one on both events:

trigger CreateIHSLaptopUpdate2 on Laptop__c (after Insert, after Update)

 

You might also have an issue with code coverage, because you've hardcoded the IDs of the Accounts which are relevant as a Location. And your test code is probably not going to create Account with that exact ID. You might consider creating a Custom Setting for Accounts that are relevant for the trigger, or adding a flag to the Account record itself. 

Craig GroveCraig Grove
Thank you sir! I'm very new with apex. This is the second code I've successfully implemented. I did get this to work by adding an ID of an existing Account from production.  I am not sure how to create a class without hardcoded IDs, which I too believe is the issue with another code I am working on. What do you mean by create a custom setting for accounts relevant to the trigger or flag? Also, I did remove my first code as the new code would have created duplicate records after the second was implemented. 
Dominic Blythe 18Dominic Blythe 18
Hi Craig. A Custom setting is a like a Custom Object, but it has special properties that make it the ideal place to keep application settings like "which accounts automatically need stuff added to their laptops". In the Salesforce UI, go to Setup > Develop > Custom Settings to create and manage them. Full information is here: Custom Settings Overview - Salesforce.com Then in the Trigger you can retrieve your custom settings, check if your Account Id is in the list, and do your trigger thing. An alternative approach is to flag the account itself with - a new checkbox for "Laptops need extra things" - or maybe a picklist "Account extra things type" or some other way of identifying the Accounts in question. Maybe you even already have that information on the Account? If the information is there as a (new or existing) field on the Account, you can create a Cross-Object Formula on the Laptop object to bring the info into the trigger. Let's say you have the field Account.Laptop_Extras__c as a checkbox. You can add a formula field Laptop__c.Laptop_Extras__c where the formula is Location__r.Laptop_Extras__c This will pull the Laptop Extras from the Location Account onto your Laptop record. Then your code simply has to do for (Laptop__c updatedLaptop: Trigger.New) { if (updatedLaptop.Laptop_Extras__c) { ... do stuff... Sorry if I'm preaching to the choir, but you said you just started with Apex so I'm being basic. Another comment - it looks like your trigger is going to add more and more IHS_Laptop_Updates every time the Laptop gets updated. Should you check if they exist already before adding more?
This was selected as the best answer
Craig GroveCraig Grove
Thanks Dominic! Definitely not preaching to the choir :^) I am basic... I'm going to review and attempt your suggestions. I'll reply when I resolve or hit a wall. Thanks again!!
Craig GroveCraig Grove
Thanks again Dominic. Your suggestion led me in the right direction, or at least one that had worked for me. I don't believe hard coding ids is the best solution, but I did create a test class record type for my laptops without any restrictions applied to the record type.  I still hard coded the laptop location id, but was able to pass the class in production. I will coast with this until I find a more proper solution. I'm heading to the ADM 231 class to get some background on this.