You need to sign in to do that
Don't have an account?
Miles Sonnenfeld 2
Help writing Test Class for Lookup Trigger
Hello,
I am trying to write a Test Class for a APEX trigger that looks up a value based on a picklist value. The APEX trigger works, however I can't deploy it because I can't get higher than 50%. Can anyone help me write the additional code for my test class for the following code?
>>>>>>>>>>>>>>>>APEX Trigger>>>>>>>>>>>>>>>>>
trigger UpdatePrice on X6_Remote_Unit__c (before insert, before update){
Set<String> locationSet = new Set<String>();
Map<String,Decimal> MapPrice = new Map<String,Decimal>();
for (X6_Remote_Unit__c RU: trigger.new){
if(!String.isBlank(RU.X1_OEMPartDescription__c)){
locationSet.add(RU.X1_OEMPartDescription__c);
}
}
if(!locationSet.isEmpty()){
for(Deployment_Part_Price_List__c PartPrice : [SELECT Name, Price__c FROM Deployment_Part_Price_List__c WHERE Name IN :locationSet]){
MapPrice.put(PartPrice.Name, PartPrice.Price__c);
//Expecting there will be one record for each location, otherwise this map will have last Data_Center_Location__c Id
}
}
for (X6_Remote_Unit__c RU: trigger.new){
if(!String.isBlank(RU.X1_OEMPartDescription__c) && MapPrice != null && MapPrice.get(RU.X1_OEMPartDescription__c) != null){
RU.X1_Total_Price__c = MapPrice.get(RU.X1_OEMPartDescription__c);
}
else{
RU.X1_OEMPartDescription__c = null;
}
}
}
>>>>>>>>>>>>>>>>TEST CLASS>>>>>>>>>>>>>>>>>
@isTest
private class TestClassUpdatePrice{
static testMethod void validateUpdatePrice(){
User u = [SELECT name, Id FROM user LIMIT 1];
system.runAs(u){
Deployment_Costing__c DC=new Deployment_Costing__c(Name='APEX Test');
insert DC;
system.debug('----->>End Adding Deployment Cost'+DC.name);
Deployment_Costing__c DCRead=[SELECT Name FROM Deployment_Costing__c WHERE name='APEX Test'];
system.debug('----->>End Reading Deployment Cost'+DC.name);
Deployment_Part_Price_List__c DPPL=new Deployment_Part_Price_List__c(Name='A', Price__c=999);
insert DPPL;
system.debug('----->>End Adding Price List'+DPPL.name);
Deployment_Part_Price_List__c DPPLRead=[SELECT Name FROM Deployment_Part_Price_List__c WHERE name='A'];
system.debug('----->>End Reading Price List'+DC.name);
X6_Remote_Unit__c RU=new X6_Remote_Unit__c(Name='APEX TEST:APEX Test', X1_OEMPartDescription__c='A',Deployment_Costing__c='APEX TEST');
insert RU;
system.debug('----->>End Adding RU'+RU.name);
X6_Remote_Unit__c RURead=[SELECT Name FROM X6_Remote_Unit__c WHERE name='APEX TEST:APEX Test'];
system.debug('----->>End Reading RU'+DC.name);
}
}
}
I am trying to write a Test Class for a APEX trigger that looks up a value based on a picklist value. The APEX trigger works, however I can't deploy it because I can't get higher than 50%. Can anyone help me write the additional code for my test class for the following code?
>>>>>>>>>>>>>>>>APEX Trigger>>>>>>>>>>>>>>>>>
trigger UpdatePrice on X6_Remote_Unit__c (before insert, before update){
Set<String> locationSet = new Set<String>();
Map<String,Decimal> MapPrice = new Map<String,Decimal>();
for (X6_Remote_Unit__c RU: trigger.new){
if(!String.isBlank(RU.X1_OEMPartDescription__c)){
locationSet.add(RU.X1_OEMPartDescription__c);
}
}
if(!locationSet.isEmpty()){
for(Deployment_Part_Price_List__c PartPrice : [SELECT Name, Price__c FROM Deployment_Part_Price_List__c WHERE Name IN :locationSet]){
MapPrice.put(PartPrice.Name, PartPrice.Price__c);
//Expecting there will be one record for each location, otherwise this map will have last Data_Center_Location__c Id
}
}
for (X6_Remote_Unit__c RU: trigger.new){
if(!String.isBlank(RU.X1_OEMPartDescription__c) && MapPrice != null && MapPrice.get(RU.X1_OEMPartDescription__c) != null){
RU.X1_Total_Price__c = MapPrice.get(RU.X1_OEMPartDescription__c);
}
else{
RU.X1_OEMPartDescription__c = null;
}
}
}
>>>>>>>>>>>>>>>>TEST CLASS>>>>>>>>>>>>>>>>>
@isTest
private class TestClassUpdatePrice{
static testMethod void validateUpdatePrice(){
User u = [SELECT name, Id FROM user LIMIT 1];
system.runAs(u){
Deployment_Costing__c DC=new Deployment_Costing__c(Name='APEX Test');
insert DC;
system.debug('----->>End Adding Deployment Cost'+DC.name);
Deployment_Costing__c DCRead=[SELECT Name FROM Deployment_Costing__c WHERE name='APEX Test'];
system.debug('----->>End Reading Deployment Cost'+DC.name);
Deployment_Part_Price_List__c DPPL=new Deployment_Part_Price_List__c(Name='A', Price__c=999);
insert DPPL;
system.debug('----->>End Adding Price List'+DPPL.name);
Deployment_Part_Price_List__c DPPLRead=[SELECT Name FROM Deployment_Part_Price_List__c WHERE name='A'];
system.debug('----->>End Reading Price List'+DC.name);
X6_Remote_Unit__c RU=new X6_Remote_Unit__c(Name='APEX TEST:APEX Test', X1_OEMPartDescription__c='A',Deployment_Costing__c='APEX TEST');
insert RU;
system.debug('----->>End Adding RU'+RU.name);
X6_Remote_Unit__c RURead=[SELECT Name FROM X6_Remote_Unit__c WHERE name='APEX TEST:APEX Test'];
system.debug('----->>End Reading RU'+DC.name);
}
}
}
X6_Remote_Unit__c RU=new X6_Remote_Unit__c(Name='APEX TEST:APEX Test', Deployment_Costing__c='a044100000E6NcR', X1_OEMPartDescription__c='A');
insert RU;
system.debug('----->>End Adding RU'+RU.name);
X6_Remote_Unit__c RURead=[SELECT Name FROM X6_Remote_Unit__c WHERE name='APEX TEST:APEX Test'];
system.debug('----->>End Reading RU'+DC.name);
All Answers
X6_Remote_Unit__c RU=new X6_Remote_Unit__c(Name='APEX TEST:APEX Test', Deployment_Costing__c='a044100000E6NcR', X1_OEMPartDescription__c='A');
insert RU;
system.debug('----->>End Adding RU'+RU.name);
X6_Remote_Unit__c RURead=[SELECT Name FROM X6_Remote_Unit__c WHERE name='APEX TEST:APEX Test'];
system.debug('----->>End Reading RU'+DC.name);