You need to sign in to do that
Don't have an account?
Bulkify Task Field update trigger and update associated custom object record
Hey there,
I have a trigger. This trigger works fine although i have read in many places that it is in ones best interest to bulkify trigger to prevent it from affecting your system in the long run. i was hoping that I may be able to get a hand in bulkifying my trigger.
I was also hoping someone could assist me in adding some extra functionality to the trigger. The activity with subject 'Upload Authoirty form' 'will awlays be related to an account, I was wondering how I could go about adding lines which will update a picklist field inside a custom object (account status) which is in a one to one relationship with account (account being the master)?
Any advised steps I should take as well would be appreciated. E.G. Adding fields to lock in one to one relationship, changing picklist to formula field, etc. I currently have a trigger which creates the Account status record and associates it with the account upon Account record creation.
This is my trigger:
Trigger updateFields on Task(before insert){
for (Task Tas: trigger.new){
if (Tas.Subject == 'Upload Authority Form') {
Tas.RecordTypeID = '012N00000008eow';
Tas.Type = 'Document Upload';
Tas.Status = 'Awaiting Form';
}
}
}
Thank you so much ina advance for any help that you may be able to provide me.
Kind regards,
Michael
I have a trigger. This trigger works fine although i have read in many places that it is in ones best interest to bulkify trigger to prevent it from affecting your system in the long run. i was hoping that I may be able to get a hand in bulkifying my trigger.
I was also hoping someone could assist me in adding some extra functionality to the trigger. The activity with subject 'Upload Authoirty form' 'will awlays be related to an account, I was wondering how I could go about adding lines which will update a picklist field inside a custom object (account status) which is in a one to one relationship with account (account being the master)?
Any advised steps I should take as well would be appreciated. E.G. Adding fields to lock in one to one relationship, changing picklist to formula field, etc. I currently have a trigger which creates the Account status record and associates it with the account upon Account record creation.
This is my trigger:
Trigger updateFields on Task(before insert){
for (Task Tas: trigger.new){
if (Tas.Subject == 'Upload Authority Form') {
Tas.RecordTypeID = '012N00000008eow';
Tas.Type = 'Document Upload';
Tas.Status = 'Awaiting Form';
}
}
}
Thank you so much ina advance for any help that you may be able to provide me.
Kind regards,
Michael
1.) You are running yoor SOQL inside for loop which is not as per the best practices of salesforce
2.) Also,you are using the AccSt[0].Account_Status__c which means if there is any bulk operation it would only update the first record of the list.Hence,your Trigger is not bulkified.
3.) Also,the syntax of SOQL which you are using is not correct.
I have modified your code.
<pre>
trigger SetAccountStatus1 on Task (after insert) {
List<Account_Status__c> AccSt = new List<Account_Status__c>();
List<Account_Status__c> updatedAccSt = new List<Account_Status__c>();
List<Id> accIds = new List<Id>();
for (Task tas: trigger.new) {
if(tas.WhatId != null && Tas.Subject == 'Upload Authority Form') {
accIds.add(tas.whatId);
}
}
AccSt = [Select Id,Account_Status__c from Account_Status__c WHERE Account__c in :accIds ];
for(Account_Status__c a : AccSt)
{
a.Account_Status__c = 'Service Decision Yes: Awaiting Authority Form';
updatedAccSt.add(a);
}
if(updatedAccSt.siza()>0)
{
update updatedAccSt;
}
}
</pre>
All Answers
<pre>
Trigger updateFields on Task(before insert){
Id rtId = [select id from RecordType where DeveloperName ='<Name of RecordType>' and SobjectType='Account']; //Assuming that the the object is Account
for (Task Tas: trigger.new){
if (Tas.Subject == 'Upload Authority Form') {
Tas.RecordTypeID = rtId;
Tas.Type = 'Document Upload';
Tas.Status = 'Awaiting Form';
}
}
}
</pre>
hEY THERE,
I tried to use your mehtod and I received this error:
Error: Compile Error: Illegal assignment from LIST<RecordType> to Id at line 3 column 1.
I was wondering if you also knew how I may update that field in the account status object?
Try the below code
<pre>
Trigger updateFields on Task(before insert){
RecordType rtId = [select id from RecordType where DeveloperName ='<Name of RecordType>' and SobjectType='Account']; //Assuming that the the object is Account
for (Task Tas: trigger.new){
if (Tas.Subject == 'Upload Authority Form') {
Tas.RecordTypeID = rtId.id;
Tas.Type = 'Document Upload';
Tas.Status = 'Awaiting Form';
}
}
}
</pre>
You can associate your Account record based on WhatId as Task and Account have a standard relationship field called WhatId
Thank you for your reply:
I got it working by this:
Trigger updateFields on Task(before insert){
RecordType RecType = [Select Id From RecordType Where SobjectType = 'Task' and DeveloperName = 'Upload_Authority_Form'];
for (Task Tas: trigger.new){
if (Tas.Subject == 'Upload Authority Form') {
Tas.RecordTypeID = RecType.id;
Tas.Type = 'Document Upload';
Tas.Status = 'Awaiting Form';
}
}
}
I attempted the other trigger, it saves....but it does not work...I planned to make two separate triggers and then combine them. I was hoping you could give me your feedback on this trigger now that the other one is working-
trigger SetAccountStatus1 on Task (after insert) {
List<Account_Status__c> AccSt = new List<Account_Status__c>();
for (Task tas: trigger.new) {
if(tas.WhatId != null && Tas.Subject == 'Upload Authority Form') {
AccSt = [Select Id,Account_Status__c from Account_Status__c WHERE Account_Status__c.Account__c = :tas.WhatId ORDER BY Id LIMIT 1];
AccSt[0].Account_Status__c = 'Service Decision Yes: Awaiting Authority Form';
}
}
}
Thank you so much for your help
1.) You are running yoor SOQL inside for loop which is not as per the best practices of salesforce
2.) Also,you are using the AccSt[0].Account_Status__c which means if there is any bulk operation it would only update the first record of the list.Hence,your Trigger is not bulkified.
3.) Also,the syntax of SOQL which you are using is not correct.
I have modified your code.
<pre>
trigger SetAccountStatus1 on Task (after insert) {
List<Account_Status__c> AccSt = new List<Account_Status__c>();
List<Account_Status__c> updatedAccSt = new List<Account_Status__c>();
List<Id> accIds = new List<Id>();
for (Task tas: trigger.new) {
if(tas.WhatId != null && Tas.Subject == 'Upload Authority Form') {
accIds.add(tas.whatId);
}
}
AccSt = [Select Id,Account_Status__c from Account_Status__c WHERE Account__c in :accIds ];
for(Account_Status__c a : AccSt)
{
a.Account_Status__c = 'Service Decision Yes: Awaiting Authority Form';
updatedAccSt.add(a);
}
if(updatedAccSt.siza()>0)
{
update updatedAccSt;
}
}
</pre>
Vinit, in your professional opinion, Is my second trigger bulkified enough? Can you think of how I may better it?
This is the final trigger and it works? Can i get your opinion on it:
trigger ServiceDecisionYesSet on Task (before insert) {
List<Account_Status__c> AccSt = new List<Account_Status__c>();
RecordType RecType = [Select Id From RecordType Where SobjectType = 'Task' and DeveloperName = 'Upload_Authority_Form'];
for (Task tas: trigger.new) {
if(tas.WhatId != null && Tas.Subject == 'Upload Authority Form') {
AccSt = [Select Id,Account_Status__c from Account_Status__c WHERE Account_Status__c.Account__c = :tas.WhatId ORDER BY Id LIMIT 1];
AccSt[0].Account_Status__c = 'Service Decision Yes: Awaiting Authority Form';
Tas.RecordTypeID = RecType.id;
Tas.Type = 'Document Upload';
Tas.Status = 'Awaiting Form';}
}
update AccSt;
}
Thank you for all your help mate!
Finished - hopefully well enough bulkified trigger -
trigger ServiceDecisionYesSet on Task (before insert) {
List<Account_Status__c> AccSt = new List<Account_Status__c>();
List<Account_Status__c> updatedAccSt = new List<Account_Status__c>();
List<Id> accIds = new List<Id>();
RecordType RecType = [Select Id From RecordType Where SobjectType = 'Task' and DeveloperName = 'Upload_Authority_Form'];
for (Task tas: trigger.new) {
if(tas.WhatId != null && Tas.Subject == 'Upload Authority Form') {
Tas.RecordTypeID = RecType.id;
Tas.Type = 'Document Upload';
Tas.Status = 'Awaiting Form';
accIds.add(tas.whatId);
}
}
AccSt = [Select Id,Account_Status__c from Account_Status__c WHERE Account__c in :accIds];
for(Account_Status__c a : AccSt)
{
a.Account_Status__c = 'Service Decision Yes: Awaiting Authority Form';
updatedAccSt.add(a);
}
if(updatedAccSt.size()>0)
{
update updatedAccSt;
}
}