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.
Best Answer
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:
Basically what you have to do is: