0

TRIGGER DESCRIPTION: We have a custom object "OrderItem__C". This custom object has a lookup field to standard contact object. It also has a field called "Shipping_customer_number__c".

We have this same field(Shipping_customer_number__c) in contact object too. Now when the user enters a shipping customer number in the order item object, based on the value in the shipping customer number field it should auto populate the contact lookup field.

This is because each contact record has an unique shipping customer number.

I am new in writing test classes and I am unable to write test class for my trigger. I am posting the code. Any insights would be 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) {
        shippingNumber.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()
    {
        contact c = new contact();
        c.Shipping_Customer_Number__c = '0987654';
        c.FirstName = 'Surekha rani';
        c.LastName = 'Penna';


            insert c;




        Order_Item__c op = new Order_Item__c();

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


            list <contact> shipnum = [select id from contact where Shipping_Customer_Number__c ='0987654' ];
            System.assertEquals(1,shipnum.size());
            map <string,id> shipnummap = new map(string,id);

            for(contact ct: shipnum)
            {
                shipnummap.put(ct.Shipping_Customer_Number__c ,ct);
            }
            list <Order_Item__c > oc = [Select id,RSM_Shipping_Contact__c ,Shipping_Customer_Number__c from Order_Item__c  ];

    }
}
4
  • 1
    What specific problem are you running into?
    – Jenny B
    Commented Jun 3, 2015 at 19:16
  • My trigger is working fine. I am stuck at writing test class. So my problem is I am unable to write test class. I have written this far. I am looking for help in finishing this. Thank you. Commented Jun 3, 2015 at 19:21
  • Your trigger object is Owner__c but the unit test does not insert this. Looks like you've got some cleanup perhaps to reference the correct object, looks like you are using Order_Item__c in the unit test instead. Commented Jun 3, 2015 at 19:28
  • Sorry, I did correct the errors. Commented Jun 3, 2015 at 19:31

1 Answer 1

2

I see a couple problems with your test case. First, you're blindly eating any error that happens when creating the test contact record:

try{
    insert c;
    }catch (Exception e){
    System.debug("Error occcured");
    }

This is really bad! If an exception were to occur and you didn't catch it, your test would fail, but you'd know immediately that there was a problem. In this case, should a failure occur while inserting the contact, you'll never know about it because the problem will be logged and the test will happily keep running, oblivious to the fact the contact really needs to have been inserted for the rest of the test to succeed. You should extract the insert statement and throw out the rest of the try/catch block.

(Note that try/catch exists for a reason and you should totally use it where appropriate! Just don't use it to hide errors in test cases which you'd really rather see.)

Next, you're incorrectly creating a map:

map <string,id> shipnummap = new map(string,id);

On a stylistic level, “map” is a type name, and it's Apex convention to capitalize type names. Secondly, the bit with the less-than/greater-than signs is also part of the type definition so it shouldn't be space-separated from the rest of the type name.

But your big problem is that you're not instantiating the map properly. A “map” data structure is one that lets you look up a value associated with another. (A “map” is also called a “dictionary” or an “associative array” in other languages.) In this case, you're trying to build a map that lets you retrieve a contact based on the “Shipping Customer Number” field. That means the map holds two different types of data: the String shipping customer number is the key type (the type of data we're using to perform the lookup), and the Contact contact record is the value type (the type of data we're looking up). When we instantiate the Map object, we need to provide those type definitions in the less-than/greater-than pairs:

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

You don't need to pass any arguments to the Map constructor to create an empty map; you certainly don't need to pass the names of the key/value types as arguments.

I suspect that once you fix these problems, you'll be able to figure out what you need to do to complete the text case based on code coverage levels. As Mike Katulka comments on your original question, you'll at least need to test the insertion of an Owner__c object.

3
  • I have edited the question and changed to order item from owner object. Now I have inserted both order item and contact in the test class. I am stuck in figuring out on how to finish the test class. :( Commented Jun 3, 2015 at 19:45
  • I'm not really sure we can help you with that. Without knowing what the trigger is supposed to do, what the objects are supposed to hold, and in turn, what the test is supposed to test, all we can help you with are technical and Apex language issues. The responsibilities of and operations performed by the test are entirely up to you. I suggest getting the test to a state where it can run, then consulting the code coverage results to see what else you need to write test code to cover.
    – Mr. DOS
    Commented Jun 3, 2015 at 19:47
  • Sorry for the trouble. I did add the description of the trigger and edited it. Commented Jun 3, 2015 at 19:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .