You need to sign in to do that
Don't have an account?
Justin George
Help to Bulkify Trigger
Hello, I have written a trigger that autmatically creates a Task record when a custom object record is created. The Owner of the Task is located through a string of related lookups. Currently, the trigger works, but it is not bulkified. I have attempted to bulkify the trigger, but I got lost is the logic and I could not find a similar piece of sample code to reference. Can someone point me in the right direction or give me some guidance.
trigger AutoCreateTask on Investigation__c (after insert) { List<Task> tasks = new List<Task>(); for (Investigation__c newInv: Trigger.New) { Investigation__c investigation = [Select Id, RecordType.Name, Driver__c, Employee__c from Investigation__c where Id =: newInv.Id]; if (investigation.RecordType.Name == 'Auto Investigation') { Contact driver = [select Supervisor__c FROM Contact where Id =: investigation.Driver__c]; Tasks.add(new Task( OwnerId = driver.Supervisor__c, WhatId = newInv.Id, Subject = 'Please complete driver investigaiton', ActivityDate = System.today().addDays(14), Type = 'Investigation' )); } if (investigation.RecordType.Name == 'Injury Investigations') { Contact employee = [select Supervisor__c FROM Contact where Id =: investigation.Employee__c]; Tasks.add(new Task( OwnerId = employee.Supervisor__c, WhatId = newInv.Id, Subject = 'Please complete injury investigaiton', ActivityDate = System.today().addDays(2), Type = 'Investigation' )); } } insert tasks; }
While bulkfying any trigger always think through the data which you need while running inside Trigger.new
We can see that you need data from Contact object and you have contact id as investigation_driver__c or investigation__c.employee__c which making this common and it can come out from Trigger.new loop.
so fix this issue we will follow below approach
1. Get all the contact id first from Trigger.new
2. SOQL contact object and prepare a dataset
3. Loop over Trigger.new and fill data from dataset created in step 2
Go though this code and try to understand logic with inline comments.
Let me know if you see any issue
Thanks,
Himanshu
Salesforce Certified Developer, Administrator, Service Cloud Consultant
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
All Answers
While bulkfying any trigger always think through the data which you need while running inside Trigger.new
We can see that you need data from Contact object and you have contact id as investigation_driver__c or investigation__c.employee__c which making this common and it can come out from Trigger.new loop.
so fix this issue we will follow below approach
1. Get all the contact id first from Trigger.new
2. SOQL contact object and prepare a dataset
3. Loop over Trigger.new and fill data from dataset created in step 2
Go though this code and try to understand logic with inline comments.
Let me know if you see any issue
Thanks,
Himanshu
Salesforce Certified Developer, Administrator, Service Cloud Consultant
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Map<id,Contact> mapConatact = new Map<id,Contact>([select Supervisor__c FROM Contact where Id : insetContact])
The map is storing the Contact.Id and the Supervisor__c
Map is the collection of key value pair. so when we define it can contain id as key and any string as value, we can initialize this map using and when we want to use it we can use following syntax.
and it will output USA
in the similiar fashion we can create a map of any type where key will be unique.
So in your case I have created a map of id,Contact sObject in following way
which we can initialize in following way
above syntax has a small performance issue, if there are 100+ contact above code execution will happen 100 times which is not optimized way to do this so SFDC provides single statement initialization which automatically create map of id,sObject which i did in your code as shown below
so instead of story only Supervisor__c in map as a value I am storing whole Contact object as value which I can access by passing contact id in following way
There are different way to write this code but I have written this code consideirng all best practice of trigger so that you can understand trigger concepts.
Makes sense?
Thanks,
Himanshu
I made some changes to the code so that it would save (clean up variable names, etc....). The trigger saves, but I can't get it to fire. I double checked the record type names and those are correct. Can you take a look at the code to see why it is not creating a task?
replace following line with
It should resolve your problem.
Thanks,
Himanshu