You need to sign in to do that
Don't have an account?
Apex Trigger to update lookup field, need help
Alright, I have very little experience coding with Apex (or any other programming language for that matter), however I am generally alright at modifying code. However, there is an area I am having trouble with, and even though I know a lot of people have had a very similar problem, I cannot seem to fix this.
I have a custom object created, that I am trying to link to the Lead object, using a lookup field. I have it set up perfectly manually, however I want to automatic this process using Apex, I get an error message.
Lead Object Fields:
PostalCode
ACZipCode API Name:ACZipCode__c Type: Lookup(ACZipCode)
Custom Object ACZipCode fields API Name: ACZipCode__c
ZipCode
API Name:ZipCode__c
Type:Text(5)
Apex Trigger needing work:
trigger GetZipCode on Lead (after insert, after update) {
for (Lead a : Trigger.new) {a.ACZipCode__c = a.PostalCode; }}
Error message:
Review all error messages below to correct your data.
Apex trigger GetZipCode caused an unexpected exception, contact your administrator: GetZipCode: execution of AfterUpdate caused by: System.StringException: Invalid id: 43225: Trigger.GetZipCode: line 2, column 29
I believe what it is doing is passing the raw zipcode number though, when it really needs the ACZipCode ID number. I just don't know how to fix this. Again, everything works perfectly if I disable the Apex trigger, and just manually type in the 5 digit zipcode (I'm guessing it does the lookup behind the scenes).
Any help would be appreciated, thanks!
If you look at this statement -
a.ACZipCode__c = a.PostalCode;
PostalCode is of type String. ACZipCode__c is a lookup to a custom object. So when the assignment happens, the right hand side is a five digit zip code. The assignment will only work if you assign a valid ID to zipcode object.
You will need to get a SOQL query on the Zipcode object. This query will use PostalCode as an input and will get the object id. This id can then be assigned to a.ACZipCode__c.
Thanks, I figured as much. The problem is with my limited knowledge of Apex, I'm not exactly sure how to code this.
Can you offer any additional pointers?
Can anyone review this code, and help me see if there are any glaring vulnerabilities for failure, or room for improvement? I'm very new to coding (do my ugly variable names make it obvious? :P ), so even though this works, and I've tested it via manual entry, manual changing, and mass uploading via the data loader, I'm still not 100% ready to push towards production..
Key (Since some things changed from original post)
Lead Object
left5zip__c = custom field, basically if len(PostalCode >4) then it's the left 5 digits
mikezip__c = custom lookup field to the ZipCodeTable__c object
ZipCode Object
ZipCodeID__c = 5 digit zip code
ZipCode ID a0YQ0000002vg4 is ID for the record on the Zipcode Object for unknown ZipCodes
ZipCode ID a0YQ0000002wKD1 in my test class is the ID for the record on the Zipcode Object for Zip Code 98026. Would be better if this wasn't hard coded...but I got tired.
This is the quick test class I wrote, and it passes with 100% coverage, but obviously that doesn't mean everything.
Hi, Well, there are a few things I see....
First, it's hard to understand what you're trying to do without Comments. Comments are important. :-)
Second, hard coding ID's is bad. An Id that you've hardcoded and works perfectly in a Sandbox won't work in production. And, it's just bad form.
Third the "special record" in the ZipCodeTable__c object for "unknown zip code" is a bad practice. Why not just leave milezip__c as null if there is no match?
This is what I'd have written:
Your test case only tests the update condition, not the insert condition. And then there's update from a case where there was an existing mikezip__c value and when there isn't.
And, what if an array of Leads has been updated or inserted. What about cases where there *is* a matching code in the ZipCodeTable, and when there isn't.
So, there's a bit more to test. Best, Steve.
Steve,
Awesome! Thanks for the feedback. I'm sure it's painfully obvious how new to coding I am, so I love the feedback. I totally forgot about ID's changing from Sandbox to Production....I knew it was bad form...but now I understand how it's really really bad form. :)
Will definitely take a look at this and start to wrap my head around it. The good thing is I definitely error on the safe side with regards to pushing anything through to production.
Hi, and, it just occurred to me that in the update case, you don't need to bother looking at the lead unless the value for the Zip in trigger.old is different from the one in trigger.new. So, you should check that in the If statement before bothering to add that zipcode to the set.
if (trigger.isUpdate and l.zipxxx == trigger.oldmap.get(l.id).zipxxx) continue;
Best, Steve.