1

Very new to coding and need a little help populating a lookup field.

I have object Account and custom object Order__c. On Account we have a Ship_to_Code__c which stores an ID. This ID is also part of the custom Order__c object, Ship_to_Code_del__c

Orders__c has a lookup to Account. When creating (or importing from dataloader) an Order__c record, I would like to auto populate the account lookup field with the Account name where there is a match on the ship to code IDs. My code below is running without errors, but not returning any account names in the lookup field. I suspect its something to do with line 16 and not correctly asking for the account id value:

trigger TestOrders on Order__c(before insert, before update, after insert) {
    set < string > setIds = new set < string > ();
    Map < String, Id > mapMatch = new Map < String, Id > ();
    for (Order__c Order: Trigger.New) {
        setIds.add(Order.Ship_To_Code_del__c);
    }
    for (Account acc: [Select Id, Ship_To_Code__c from Account where Ship_To_Code__c IN: setIds]) {
        mapMatch.put(acc.Ship_To_Code__c, acc.Id);
    }
    for (Order__c Order: Trigger.New) {
        if (mapMatch.containsKey(Order.Ship_To_Code_del__c)) {
            Order.SFDC_Account_Name__c = mapMatch.get(Order.Ship_To_Code_del__c);
        }
    }
}
  • could you add system.debug(mapMatch); & system.debug(trigger.new); after 2nd for you should see if data is valid – Ilya Lepesh May 12 '16 at 5:37
2

I cannot comment right Now, but I want to understand why you have used after insert event in this trigger. According to me this should only be on before insert and before update event as you are updating same object's field.

Update : I just checked, while you use after insert in the trigger it will give you exception(

System.FinalException: Record is read-only

) because you are trying to update new list in after insert event.

| improve this answer | |
0

I would suggest you to go with Before triggers since After triggers are of Read-only type. Also I would suggest you go not write your trigger logic inside the trigger. Instead use the Class and call it in your trigger.

One more thing you will need to take care of is that if your field Ship_To_Code_del__c AND Ship_To_Code__c are of text type and can have same values in different cases i.e upper/lower case. Then you will have to compare these fields in the case sensitive mode.

Below trigger will help you in achieving your requirement.

Trigger

Trigger TestOrders on Order (Before Insert , Before Update) 
{
    IF(Trigger.IsBefore)
    {
        IF(Trigger.IsInsert || Trigger.IsUpdate)
        {
            OrderTriggerHelper.UpdateAccount(Trigger.New);
        }
    }
}

Trigger Class

Public Class OrderTriggerHelper
{
    Public Static Void UpdateAccount(List<Order> OrderIds)
    {
        List<Account> AccList = [Select Id,Ship_To_Code__c from Account where Ship_To_Code__c != NULL];

        IF(AccList != NULL)
        {
            FOR(Account A : AccList)
            {
                FOR(Order O : OrderIds)
                {
                    //To check the Case-sensitivity.
                    String S1 = A.Ship_To_Code__c;
                    String S2 = O.Ship_to_code_del__c;
                    IF(S1.Equals(S2))
                    {
                        O.SFDC_Account_Name__c = A.Id;
                    }
                }
            }
        }
    }
}

Hope this helps you! In case you face any issues let us know.

| improve this answer | |
  • Appreciate your feedback, thanks. I have an error when trying to create the apex class: Entity is not org-accessible at line 1 column 8. The Order object is a custom one, API is Order__c – Mark May 13 '16 at 4:07

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.