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
SFDC_BigDogSFDC_BigDog 

Code Coverage 0% for Trigger

I have written a trigger on custom object Order_Item__c. This object has a custom field Shipping_Number__c. When the user enters value in Shipping_Number__c and clicks save, the contact lookup field will automatically populate with contact record which has same shipping number as the value entered by the user. 

All the contact records have the shipping customer number. Now custom object order item has a field called shipping customer number. When user enters the value in it, the trigger checks the contact record which has same number and populate the contact lookup field on order item object. The trigger is running as it is supposed to run. I am unable to achieve code coverage.

 

TRIGGER:

trigger updatelookupfield on Order_Item__c (before update, before insert)
 {


    Set<String> shippingNumbers = new Set<String>();

    for (Order_Item__c collectNumFromOrder : Trigger.new) {
        shippingNumbers.add(collectNumFromOrder.Shipping_Customer_Number__c);
    }



    List<Contact> contactList = [SELECT id, Shipping_customer_number__c FROM Contact WHERE Shipping_Customer_Number__c IN :shippingNumbers];

    Map<String, Contact> shippingNumToContactMap = new Map<String, Contact>();

    for (Contact c : contactList) {
        shippingNumToContactMap.put(c.Shipping_customer_number__c, c);
    }

    for (Order_Item__c o : Trigger.new) {

          if (o.Shipping_Customer_Number__c != null) {
            o.RSM_Shipping_Contact__c = shippingNumToContactMap.get(o.Shipping_Customer_Number__c).id;
        }
        else {
            o.RSM_Shipping_Contact__c = null;
        }

    }
    }

TEST CLASS

@isTest
public class testupdatelookupfield{

    Static testMethod void insertOrderItem()
    {   // create and insert the contact record - this part is correct
        contact c = new contact();
        c.Shipping_Customer_Number__c = '0987654';
        c.lastName = 'Surekha';

      insert c;


        // create an order item and insert it
        Order_Item__c op = new Order_Item__c();

        op.Name = 'Test Varun Order';
        op.Shipping_Customer_Number__c = '0987654';
        insert op;


        system.assertequals(op.RSM_Shipping_Contact__c,c.id);
    }
}
Best Answer chosen by SFDC_BigDog
SFDC_BigDogSFDC_BigDog
This is the correct test class for above trigger.
 
@isTest
public class updatelookupfieldTestclass
{


    @testSetup static void setup() 
      {
                
        contact c = new contact();
        c.Shipping_Customer_Number__c = '0987654';
        c.lastName = 'Surekha';
        insert c;
        
        
        Order_Item__c o = new Order_Item__c();
        o.name = 'ATT';
        o.Shipping_Customer_Number__c = '0987554';
        insert o;

      
 
     }
    Static testMethod void insertOrderItem()
    
      {   
        
        
        Order_Item__c OI=[select id,RSM_Shipping_Contact__c from Order_Item__c where Shipping_Customer_Number__c = '0987554'];
        system.assertequals(OI.RSM_Shipping_Contact__c, null);
      }
        
        Static testMethod void insertOrderItem_Null()
    
      {   
      
        Order_Item__c OI=[select id,RSM_Shipping_Contact__c from Order_Item__c where Shipping_Customer_Number__c = '0987554'];
        Contact c=[select id from contact where Shipping_Customer_Number__c = '0987654'];
       
        OI.Shipping_Customer_Number__c = '0987654';
        update OI;
        
        Order_Item__c OIU=[select id,RSM_Shipping_Contact__c from Order_Item__c where Id=:OI.id];
        system.assertequals(OIU.RSM_Shipping_Contact__c, c.Id);
      }

}

 

All Answers

NagaNaga (Salesforce Developers) 
Hi SFDC_bigdog,

Your RSM_Shipping_Contact__c value will not be updated until you declare a  DML, you have to get the value again after your Insert operation.

User-added image

Please let me know if this helps

Best Regards
Naga kiran
SFDC_BigDogSFDC_BigDog
This is the correct test class for above trigger.
 
@isTest
public class updatelookupfieldTestclass
{


    @testSetup static void setup() 
      {
                
        contact c = new contact();
        c.Shipping_Customer_Number__c = '0987654';
        c.lastName = 'Surekha';
        insert c;
        
        
        Order_Item__c o = new Order_Item__c();
        o.name = 'ATT';
        o.Shipping_Customer_Number__c = '0987554';
        insert o;

      
 
     }
    Static testMethod void insertOrderItem()
    
      {   
        
        
        Order_Item__c OI=[select id,RSM_Shipping_Contact__c from Order_Item__c where Shipping_Customer_Number__c = '0987554'];
        system.assertequals(OI.RSM_Shipping_Contact__c, null);
      }
        
        Static testMethod void insertOrderItem_Null()
    
      {   
      
        Order_Item__c OI=[select id,RSM_Shipping_Contact__c from Order_Item__c where Shipping_Customer_Number__c = '0987554'];
        Contact c=[select id from contact where Shipping_Customer_Number__c = '0987654'];
       
        OI.Shipping_Customer_Number__c = '0987654';
        update OI;
        
        Order_Item__c OIU=[select id,RSM_Shipping_Contact__c from Order_Item__c where Id=:OI.id];
        system.assertequals(OIU.RSM_Shipping_Contact__c, c.Id);
      }

}

 
This was selected as the best answer