You need to sign in to do that
Don't have an account?
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); } }
All Answers
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.
Please let me know if this helps
Best Regards
Naga kiran