You need to sign in to do that
Don't have an account?

Trigger to Check for Objects With The Same Value in Custom Field
I'm trying to write a trigger that fills in a custom lookup field on a case by looking for a custom object with an exact match in a text field.
Basically, what I would like to do is when the custom object (custobj) is created or saved, and the text field (Package ID) has a value, find all cases with the same value in an equivalent custom field, and add the custom object to a lookup field on the case.
All attempts at writing this run into the same issue which is how to form the SOQL query to pull the related cases to update. The following query is obviously totally wrong, but hopefully it highlights where my problem is. What I think I need to do is pull all cases that have the Package_ID__c field which matches a Package_ID__c field on the custom object. Can anyone help clarify how to do this?
List<Case> cases = new List<Case>([SELECT Id, Package_ID__c FROM Case WHERE Package_ID__c = custobj.Package_ID__c);
Thanks!!
All Answers
Awesome! It was just a syntax thing; thanks very much! I've marked your answer as the solution, but that actually brings me to a follow-up question, though. I believe my code is poorly formed because it contains a SOQL query inside of a for loop that looks like it has the potential for disaster. Any advice on how to reform the code so that it's not quite so ugly?
Put all of the key fields into a set and then query for them. Then put them in a map according to the query. It will actually come to a little more code, but you won't hit governor limits.
Below should set up the code so you don't have to run the query in the loop.
Perfect - this is exactly what I needed to get this finished. Thanks very much for the help!