You need to sign in to do that
Don't have an account?
Craig 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;
}
}
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;
}
}
All Answers
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.