function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

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);


Best Answer chosen by Admin (Salesforce Developers) 
List<Case> cases = [SELECT Id, Package_ID__c FROM Case WHERE Package_ID__c = :custobj.Package_Id__c];


All Answers

List<Case> cases = [SELECT Id, Package_ID__c FROM Case WHERE Package_ID__c = :custobj.Package_Id__c];


This was selected as the best answer

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?


trigger CaseCustomobjAssociation on Customobj__c (before insert, before update) {
    for(Customobj__c custobj:{
        if (custobj.Package_ID__c != Null)  {
            List<Case> cases = new List<Case>([SELECT Id,Package_ID__c,AccountId FROM Case WHERE Package_ID__c = :custobj.Package_ID__c]);
            List<Case> caseUpdateList = new List<Case>();
    	    for(Case c: cases) {
    		c.Custobj__c = Custobj.Id;
c.AccountId = custobj.Account__c; caseUpdateList.add(c); } if(caseUpdateList.size() > 0) { update caseUpdateList; } } } }



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.

Set<String> pkIds = new Set<String>();
for (Customobj__c cust:

Map<String, Case> caseMap = new Map<String, Case>();
for (Case aCase: [SELECT Package_Id__c, AccountId FROM Case WHERE Id IN :pkIds])
  caseMap.put(aCase.Package_Id__c, aCase):



Perfect - this is exactly what I needed to get this finished.  Thanks very much for the help!