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 trying different ways. Any insights can be helpful for us.

Thank you.

  • Reddy, what is your question? Post the code you have developed Commented Jun 3, 2015 at 14:45
  • I am unable to decide. Is there any particular link /code which can guide me in developing trigger for this. For auto populating lookup field based on a value in a field. Commented Jun 3, 2015 at 14:48

2 Answers 2


Just create a trigger on OrderItem__c. Since the shipping customer number is unique, when an OrderItem__c record is updated, you can query for the Contact whose Shipping Customer Number matches the new value of the OrderItem__c, and set the Contact lookup on OrderItem__c to match that queried Contact.

You can find information on developing triggers here.

I didn't attempt to compile this and there might be a more elegant way to write it, but it's bulkified and is generally what the logic should be:

trigger SetContactOnOrderItem on OrderItem__c (before update, before insert) {
    Set<Integer> shippingNumbers = new Set<Integer>();

    for (OrderItem__c collectNumFromOrder : Trigger.new) {

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

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

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

    for (OrderItem__c o : Trigger.new) {
        if (o.Shipping_Customer_Number__c != null) {
            o.Contact__c = shippingNumToContactMap.get(o.Shipping_Customer_Number__c).id;
        else {
            o.Contact__c = null;
  • Hey Brian, Thanks Buddy it is working. Also if there isn't any shipping number in contact it is populating my name as contact. Can you guide me on how to prevent that? Commented Jun 3, 2015 at 15:49
  • 1
    It could be a lot of things, like I said I didn't compile it. For instance, I see that I wrote WHERE Shipping_Customer_Number__c IN shippingNumbers instead of WHERE Shipping_Customer_Number__c IN :shippingNumbers Commented Jun 3, 2015 at 15:53
  • 1
    @ReddyPenna the trigger shouldn't have even compiled without the : in front of shippingNumbers in that query, are you sure you saved the trigger correctly? Commented Jun 3, 2015 at 15:55
  • 1
    @ReddyPenna I updated the last for loop to add that logic, if you have any more follow up issues please post it in a different question :P good luck man Commented Jun 3, 2015 at 16:06
  • 1
    Thanks Brian. I am new to this forum. Next time I will do that. Commented Jun 3, 2015 at 16:10

Before you go too far down the custom code path, consider the Process Builder.

You can create a Process Builder Process on the Order Item object.

The evaluation criteria would be something like NOT( ISBLANK( Order_Item__r.Shipping_Customer_Number__c ))

The action would be to Update Record(s) and select Order_Item__r.Contact__c as the object to update.

You need to consider what happens when a Contact already has a Shipping Customer Number. Are future Order Items populated with that number? I'm assuming that only the first time an Order Item is created that this number is populated.

What previously required not a small amount of code can be built in less than 5 minutes.

  • Hey Ryan, All the contacts has this shipping customer number. Now when a user enters this number in field in order items object, it should auto populate the contact lookup field in order items object. i.e. based on this number it should populate the contact look field. Commented Jun 3, 2015 at 15:22

You must log in to answer this question.

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