0
trigger Wholesale_State on Account (after insert, after update) 
{
    for (Account a : Trigger.new) 
    {
        if (a.Wholesale_State__c == null)
        {
            a.Wholesale_State__c = a.Account_State__c; 
        }
        else if (a.Account_State__c == null) 
        {
            a.Account_State__c = a.Wholesale_State__c;
        }
    }
}

Error:Apex trigger Wholesale_State caused an unexpected exception, contact your administrator: Wholesale_State: execution of AfterUpdate caused by: System.StringException: Invalid id: Texas: Trigger.Wholesale_State: line 7, column 1

I need to use the Name instead of the ID to populate the look up field, from what I researched. I don't understand how to do that though. Any help would be greatly appreciated.

2
  • Are both fields lookups to a State__c object? Apr 7 '15 at 16:37
  • Account_State__c is a picklist and Wholesale_State__c is the lookup field.
    – natenate
    Apr 7 '15 at 18:08
3

When setting lookup fields programmatically with Apex, you need to reference the id of the "looked-up" object, not the name. The following code is untested, so there may be some syntactical errors, but intentionally readable - here's the general idea of what you want to do.

When designing a trigger like this, think backwards from your desired end result. In this case:

  1. You know that you will need a data structure in which you can access the Wholesale_State__c id (to assign the lookup) where the Name of that object matches the Picklist value...a Map<{picklist value}, {id of the Wholesale_State__c}> will work.
  2. To create that, you need to know the Wholesale_State__c records that you will need, i.e. the records whose Names match the values of the Picklist on Accounts for which this trigger will be firing.
  3. You already know those Accounts, because Trigger.new is a List of them!

Basically what you have to do is:

trigger Wholesale_State on Account (after insert, after update) {
   // set to hold the string values of the picklist that are used in the affected Accounts
   Set<String> picklistNameSet = new Set<String>();

   // add each of the picklist values, removing duplicates
   for (Account a : trigger.new) {
      picklistNameSet.add(a.Account_State__c);
   }

   // List of actual Wholesale_State__c records that we'll use to make our map
   List<Wholesale_State__c> ourWholesales = [SELECT id, Name FROM Wholesale_State__c WHERE Name IN :picklistNameSet];

   // Map that we'll use to access Wholesale_State__c Name/id pairs
   Map<String, id> wholesaleMap = new Map<String, id>();

   for (Wholesale_State__c w : ourWholesales) {
      wholesaleMap.put(w.Name, w.id);
   }

   for (Account a : Trigger.new) 
   {
      if (a.Wholesale_State__c == null) {
         a.Wholesale_State__c = wholesaleMap.get(a.Account_State__c);
      }
      else if (a.Account_State__c == null) {
         a.Account_State__c = a.Wholesale_State__r.Name;
      }
   }
}
1
  • It may be worth noting this code relies on the name field being unique or else it will have wonderfully non-deterministic results (although from the question this seems like a sane assumption, just worth calling out) Apr 8 '15 at 4:59
1

A lookup field needs to be set with the record Id of the related object, not it's display name.

While the ui lookup dialog can make it seem like the name is the important part that's not the case at a data or api level.

You have a couple options to make this work, the most common of which is using soql (read up on bulkification before you deploy to production though!) to fetch the ids of the corresponding records for the names selected.

You could also use a list custom setting to hold a mapping of names to record ids, but unless there's good reason to do so this just adds complexity.

0

trigger Wholesale_State on Account (after insert, after update) { for (Account a : Trigger.new) { if (a.Wholesale_State__c == null) { a.Wholesale_State__c = a.Account_State__r.Name; } else if (a.Account_State__c == null) { a.Account_State__c = a.Wholesale_State__c; } } }

If this will not work than see the relation field name nad put that instead of Account_State__r.

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.