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
Jesse LeeJesse Lee 

Apex Trigger Help - Update related custom object record with Case Id when a new Case is created


I have a custom object called "Application" and I'm trying to use a trigger to update a field on the Application record with a Case record Id when a case is created. More context....

There is an Application Number automatically created and added to a field on the Application object called "Application Number" (Application.Application_Number__c). The Case object also has a custom field called "Application Number" (Case.Application_Number__c). 

When a new Application record is created, Process Builder is invoked to create a new Case record. Process Builder takes the Application Number from my App object record and inserts it into the corresponding field on the Case record its creating. That's currently the only linked factor. 

Here's what I'm trying to accomplish with this trigger and need help with: 
I want this trigger to fire when the Case record is created, query my Application object records and look for the record with the matching Application Number (remember this is a stored value on both objects). Once it finds the record with the matching Application Number, I want to update the "Case_Id__c" field on my Application record with the actual Case record that was just created. This is so that I can relate them.

Example:
1)    Object: Application__c
          - Field: Application_Number__c
          - Field: Case_Id__c
2)  Object: Case
          - Field: Application_Number__c

I need a SOQL query to find the match via the application number, then simply update the field with the case Id.

My problem is right now I can't get this trigger to update the field and insert the Case Id. What am I doing wrong? I'm sure this is going to be a facepalm moment for me. Any help is appreciated!
 
trigger CaseRelateToApp on Case (after insert) // trigger when a new Case is created
{
    for (Case c : Trigger.new) 
    {
        String applicationNumber = c.Application_Number__c; //field on the Case holding the Application #
        String caseRecordId = c.Id;  // Case SFDC record Id

        List<Application__c> appRecord = [SELECT Id,Case_Id__c,Application_Number__c FROM Application__c WHERE Application_Number__c = :applicationNumber];  //query the Application object records and look for the matching Application Number that's stored on both object records in the field called "Application_Number__c"
       
        for(Application__c d: appRecord)
    	{
            d.Case_Id__c = caseRecordId;  //update the Case Id field on the Application object with the actual Case record Id that caused this trigger to execute.
        }
    }
}

 
Best Answer chosen by Jesse Lee
Dushyant srivastava 8Dushyant srivastava 8
Also, it would be great if the code is bulkified. Please refer to the code written below: 
trigger CaseRelateToApp on Case (after insert)
{
	caseTriggerHandler.onAfterInsert(List<Case> lstCase);
}

public class caseTriggerHandler {
	public static void onAfterInsert(List<Case> lstCase)
    {
        linkCaseWithApp(lstCase);
    }
    
	public static void linkCaseWithApp(List<Case> lstCase)
	{
		Map<String , String> mapAppWithCase = new Map<String , String>();
		for (Case objCase : lstCase) 
		{
			if(objCase.Application_Number__c != Null)
				mapAppWithCase.put(objCase.Application_Number__c, objCase.Id);
		}
		
		List<Application__c> appRecord = new List<Application__c>();
		for(Application__c objApplication: [SELECT Id,Case_Id__c,Application_Number__c FROM Application__c WHERE Application_Number__c IN: mapAppWithCase.keySet()])
		{
			objApplication.Case_Id__c = mapAppWithCase.get(objApplication.Application_Number__c);
			appRecord.add(objApplication);
		}
		
		if(appRecord.size() > 0)
			update appRecord;
	}
}

All Answers

Dushyant srivastava 8Dushyant srivastava 8
Hi Jesse,
DML is missing.
Dushyant srivastava 8Dushyant srivastava 8
Also, it would be great if the code is bulkified. Please refer to the code written below: 
trigger CaseRelateToApp on Case (after insert)
{
	caseTriggerHandler.onAfterInsert(List<Case> lstCase);
}

public class caseTriggerHandler {
	public static void onAfterInsert(List<Case> lstCase)
    {
        linkCaseWithApp(lstCase);
    }
    
	public static void linkCaseWithApp(List<Case> lstCase)
	{
		Map<String , String> mapAppWithCase = new Map<String , String>();
		for (Case objCase : lstCase) 
		{
			if(objCase.Application_Number__c != Null)
				mapAppWithCase.put(objCase.Application_Number__c, objCase.Id);
		}
		
		List<Application__c> appRecord = new List<Application__c>();
		for(Application__c objApplication: [SELECT Id,Case_Id__c,Application_Number__c FROM Application__c WHERE Application_Number__c IN: mapAppWithCase.keySet()])
		{
			objApplication.Case_Id__c = mapAppWithCase.get(objApplication.Application_Number__c);
			appRecord.add(objApplication);
		}
		
		if(appRecord.size() > 0)
			update appRecord;
	}
}
This was selected as the best answer
Jesse LeeJesse Lee
Hi Dushyant, Thank you so very much! I tried your second segment of code, but it didn't work unfortunately. My code worked once I added the DML "update" part at the end. I marked your second answer as the "Best" because it is a better practice doing it that way. Thank you for both of your responses! Have a great day!