0

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 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;


    list <contact> conlist = [Select id,name from contact where Shipping_Customer_Number__c ='0987654'];
    list<Order_Item__c> orderlist = [Select id,RSM_shipping_contact__c,Shipping_Customer_Number__c from Order_Item__c ];

       for(Order_Item__c o: orderlist)
       {
           for(contact ct:conlist)
           {
           o.RSM_Shipping_Contact__c = ct.id;
           }
            System.assertEquals('Surekha', o.RSM_Shipping_Contact__c);
       }

}
}
  • 1
    isn't this a dup of salesforce.stackexchange.com/questions/78625/…? – cropredy Jun 4 '15 at 1:00
  • It is always better to crate the Order_Item__c record within Test.startTest() to denote when your test actually begins. Is your assertion fails? what do you mean by unable to achieve code coverage. – Tuan Abdeen Jun 4 '15 at 1:39
  • Yes my assertion fails – SFDC_BigDog Jun 4 '15 at 1:42
  • I assume your expected value is 'Surekha' and actual value is a SF contact Id - when the assertion runs – Tuan Abdeen Jun 4 '15 at 1:46
  • But it is showing null instead of id – SFDC_BigDog Jun 4 '15 at 2:23
0

Shipping_customer_number__c is this field of contact need to know RSM_Shipping_Contact__c this field is belongs to order item object

Not the answer you're looking for? Browse other questions tagged or ask your own question.