1

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. The trigger is working fine. I am unable to get code coverage. The coverage is 0%. Any insights will be lot helpful.

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);
    }
}
  • 1
    Where are you seeing that number? – Adrian Larson Jun 3 '15 at 23:16
  • Hey Adrian Which number? Shipping customer number? Code coverage number? – SFDC_BigDog Jun 3 '15 at 23:17
  • @sfdc_bigdog code coverage number – Rao Jun 3 '15 at 23:17
  • 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. – SFDC_BigDog Jun 3 '15 at 23:19
  • Before the code coverage, I am getting an error as assertion failed. My test class is wrong somewhere and I am unable to find it. But trigger is fine. Any insights on test class for this trigger would be helpful to me. – SFDC_BigDog Jun 3 '15 at 23:21
2

My advice is to use System.debug('My debug message: ' + myObject.field__c), instead of using system.assertequals, for right now. This will allow your Test class to pass, giving you code coverage, and will also give you the value of the fields, allowing you to assert their equality yourself and see if the logic behaves as you think it does.

Run your test in the Developer Console. After it has finished, click "Logs" in the bottom left corner of the screen. You should see your test classes last run. Double-click it. Click the checkbox "Debug Only" and you will see your System.debug statements only.

0

Your RSM_Shipping_Contact__c value will not be updated until you issue DML, so you need to get the value again after your Insert operation.

Try something like this:

system.assertequals(c.id, [select RSM_Shipping_Contact__c from Order_Item__c where Shipping_Customer_Number__c = '0987654'].RSM_Shipping_Contact__c);

  • Yeah you only get coverage from passing test classes... – Adrian Larson Jun 3 '15 at 23:19
  • @AdrianLarson But unable to pass the test class. I am new in developing and finding it difficult in writing test class for my trigger – SFDC_BigDog Jun 3 '15 at 23:22
  • I am getting this error System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Order_Item__c.Shipping_Customer_Number__c – SFDC_BigDog Jun 3 '15 at 23:41
  • Sorry, my mistake :) It should be system.assertequals([select RSM_Shipping_Contact__c from Order_Item__c where Shipping_Customer_Number__c = '0987654'].RSM_Shipping_Contact__c , c.id); – dzh Jun 3 '15 at 23:43
  • @dzh I am getting this error still System.AssertException: Assertion Failed: Expected: null, Actual: 003190000059cv1AAA – SFDC_BigDog Jun 3 '15 at 23:49
0

I would recommend you to use the Test.startTest() system method before you create the Order_Item__c record. Contact could be outside (before), as a pre-requisite setup test data. Trigger is based on the Order_Item__c object and you could use Test.StartTest() to show when your test actually begins

0

This is exactly the test class for my trigger should be. I am trying to get help from this community and instead people are redirecting me somewhere else.

Test Class:

@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);
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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