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
Lauren StuparLauren Stupar 

Do I need to use an Apex Trigger to update a case field when record in custom object is created?

I'm not familiar with Apex Triggers but I think I have a use case that requires one. Our org has a cutom object called Surveys. When a new Survey record is created, I want to update a field on an existing Case. I tried using a Workflow Rule, but the Case fields were not available in the Workflow Action 'New field update'. I'm guessing it's not the right kind of relationship. We're using Classic interface and in the process of migrating to Lightning.

The Survey has a field called Date Survey Completed. I want it to update a date field called QI Date, on a related Case. Surveys and Cases have a lookup relationship. 

Before I take the time to learn about and attempt to write an Apex Trigger, I wanted to ask: Does this sound like a situation that calls for a trigger or is there a simpler process to address this? and, if so, does anyone have an example of a similar code I could use as a starting point?

Any thoughts would be appreciated.
Mandalapu BrahmanaiduMandalapu Brahmanaidu
Hi

 Please find the code
 
Trigger SurveryTrigger on Survey__c(after insert)
{
	Map<Id,Survey__c> mapsurvey=new Map<Id,Survey__c>();
	for(Survey__c ObjSurevey:Trigger.new)
	{
		if(ObjSurevey.Case__c!=null)
		{
			mapsurvey.put(ObjSurevey.Case__c);
			//ObjSurevey.lookupfield
		}
	}
	if(mapsurvey.size()>0)
	{
		List<Case> caselist=[select Id,Qi__Date__c from Case where Id in:mapsurvey.keyset()];
		for(Case ObjCase:caselist)
		{
			ObjCase.Qi__Date__c=System.today();
		}
		if(caselist.size()>0 && !caselist.isEmpty())
		{
			update caselist;
		}
	}
	
}
If you find it's helpful then please mark it as best answer.It would be helpful to others.
Thanks&Regards
Brahmanaidu
https://sfdcscenarios.blogspot.com/
$hwet@$hwet@
Hi Laurean,

It can be achieved through Process builder as well. 
https://trailhead.salesforce.com/en/content/learn/modules/business_process_automation/process_builder
https://help.salesforce.com/articleView?id=process_overview.htm&type=5
 
Lauren StuparLauren Stupar
Thank you both for the thoughtful replies. I am going to spend some time working on this next week and will let you know how it turns out.