+ Start a Discussion
SFDC_BigDogSFDC_BigDog 

Auto Populate a Lookup Field based on a field value

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.
Best Answer chosen by SFDC_BigDog
SFDC_BigDogSFDC_BigDog
trigger updatelookupfield on Owner__c (before update, before insert) {
    
  
    Set<String> shippingNumbers = new Set<String>();

    for (Owner__c collectNumFromOrder : Trigger.new) {
        shippingNumbers.add(collectNumFromOrder.Shipping_Customer_Number__c);
    }

    

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

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

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

    for (Owner__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;
        }
           
    }
    }

All Answers

Andy BoettcherAndy Boettcher
I would use a "trigger-ready" Visual Workflow to do the lookup/population on the Custom Object and call it from Lightning Process Builder.
SFDC_BigDogSFDC_BigDog
trigger updatelookupfield on Owner__c (before update, before insert) {
    
  
    Set<String> shippingNumbers = new Set<String>();

    for (Owner__c collectNumFromOrder : Trigger.new) {
        shippingNumbers.add(collectNumFromOrder.Shipping_Customer_Number__c);
    }

    

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

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

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

    for (Owner__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;
        }
           
    }
    }
This was selected as the best answer
tulasiram chtulasiram ch
Can we do same thing using Process builder if yes How? if we don't why?...Give me answer pls i am just learning...Happy learning
Singh 837Singh 837

Hi, Is the code bulkified, because i using the code, that is running for single record manual insertion but for multiple records it is failing with the below error...

"caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, "