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
Miles Sonnenfeld 2Miles 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);
        }
    }
}
Best Answer chosen by Miles Sonnenfeld 2
Miles Sonnenfeld 2Miles Sonnenfeld 2
Hey Paul, thank you for the quick responce and advice.  I was able to deploy the test class and the issue was that I needed to use the ID instead of Name for Deployment_Costing__c because it is a "master detail" feild.

            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

Paul S.Paul S.
For any IF block to be covered, you have to test both positive and negative use cases.  I think if you also test against an X6 record with no OEMPartDescription, you should see your coverage go up.
Miles Sonnenfeld 2Miles Sonnenfeld 2
Hey Paul, thank you for the quick responce and advice.  I was able to deploy the test class and the issue was that I needed to use the ID instead of Name for Deployment_Costing__c because it is a "master detail" feild.

            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);
This was selected as the best answer