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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

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
Best Answer chosen by Developer.mikie.Apex.Student
Vinit_KumarVinit_Kumar
I see there are few issues with your code which I am listing below :-

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

Vinit_KumarVinit_Kumar
Your code looks just fine except that you are hard coding the RecordTypeId which is not advised instead you should query it and use in your code something like below :-

<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>
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

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?

Vinit_KumarVinit_Kumar
Oh my bad...

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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

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

Vinit_KumarVinit_Kumar
I see there are few issues with your code which I am listing below :-

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>
This was selected as the best answer
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
I think I figured it out!! it needed update at the end!!!

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!
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Thank you soooo much Vinit. I will get to work on combining the bulkified code. You are a legend mate!!
Vinit_KumarVinit_Kumar
No worries Happy to help :)
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
I am sooo happy, I have spent the better part of last week trying to figure out this trigger. You are a legend mate!!. I Will just quickly run the finished product by you so that I can get the all clear from someone alot more  in the know how than myself.

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

}
Vinit_KumarVinit_Kumar
Yes looks good :)
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Thank you Vinit!