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
ArikArik 

APEX Code to do field update

Help - need apex - not workflows to do a field update on tasks....

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

Also,

 

the original code I provided must be run in a before insert trigger not an after. If it must be in an after trigger, further modifications will be needed

All Answers

Starz26Starz26

Need more info on use case as a simple field update should be performed by a workflow unless there are circumstances that do not allow it.

ArikArik

It cant be done in workflow because the task is created by a workflow and sf prevents a workflow (field update) from being triggered from a task created from a workflow.  Therefore, I need an apex trigger to do a field update and I am just not that smart....

Starz26Starz26

ok....

 

Field to be updated and criteria for the update?

 

You may need to write an insert trigger....

 

Depending on your criteria, you may be able to incorporate the update in the workflow (i.e different workflows based off your criteria to create different type of tasks with different field values....)

 

 

 

 

ArikArik

The field to be updated is a cehckbox called "convert"  and it needs to me marked true.  the criteria is if the subjuct of the task says "converted"

 

I am so grateful for any help u can give

Starz26Starz26

trigger UpdateConvert on Task (Before Insert){

 

for(Task t : trigger.new){

 

 if(t.Subject == 'convert')

       t.convert__c = true;

 

}

 

 

}

ArikArik

Thank you - I keep getting a variable doesnt exist error on the convert__c  line but that is te name of the variable

Starz26Starz26

sorry, it should be t.convert__c

ArikArik

It works by iteself, but i get this error:

 

"Attempt to de-reference a null object" when run alongside this trigger.  The problem is the line that begins task ta1=trigger.old[0];

 

trigger CreateEvent on Task (after insert,after update)
{
task ta=trigger.new[0];
if(ta.CreateEvent__c==true)
{
task ta1=trigger.old[0];
Datetime startDateTime=Datetime.newInstance(ta1.activitydate, Time.newInstance(1, 0, 0, 0));
Datetime endDateTime=Datetime.newInstance(ta1.activitydate, Time.newInstance(12, 0, 0, 0));
Event e=new event(OwnerId = ta1.ownerid,Subject = ta1.Subject,StartDateTime = startDateTime,EndDatetime = endDateTime,WhoId = ta1.whoid,WhatId = ta1.Whatid);
insert e;
task t=new task(id=ta1.id);
delete t;
}
}
Starz26Starz26

trigger.old cannot be used in an Insert trigger

 

place the trigger.old part in an if

 

if(!trigger.isInsert){

 

  trigger.old stuff

 

}

 

There is a lot more wrong with the trigger than this though. It will work but it will not handle bulk records. If I have time tomorrow to rewrite for you I will.

ArikArik

Im doing something wrong, because it wont save

ArikArik

When you say substitiute - where exactly should it be in here:

 

trigger CreateEvent on Task (after insert,after update)
{
task ta=trigger.new[0];
if(ta.CreateEvent__c==true)
{
task ta1=trigger.old[0];
Datetime startDateTime=Datetime.newInstance(ta1.activitydate, Time.newInstance(1, 0, 0, 0));
Datetime endDateTime=Datetime.newInstance(ta1.activitydate, Time.newInstance(12, 0, 0, 0));
Event e=new event(OwnerId = ta1.ownerid,Subject = ta1.Subject,StartDateTime = startDateTime,EndDatetime = endDateTime,WhoId = ta1.whoid,WhatId = ta1.Whatid);
insert e;
task t=new task(id=ta1.id);
delete t;
}
}

I have tried everywhere and it wont save
Starz26Starz26

Try

 

trigger CreateEvent on Task (after insert,after update)
{

//Perform the code for the update trigger here 
//
if(!trigger.isInsert){
  
  for(task ta : trigger.new){

    if(ta.CreateEvent__c==true){
       task ta1=trigger.oldMap.get(ta.id);
         Datetime startDateTime=Datetime.newInstance(ta1.activitydate,   
             Time.newInstance(1, 0, 0, 0));
          Datetime endDateTime=Datetime.newInstance(ta1.activitydate, 
             Time.newInstance(12, 0, 0, 0));
         
          Event e=new event(OwnerId = ta1.ownerid,Subject = 
             ta1.Subject,StartDateTime = startDateTime,EndDatetime = 
             endDateTime,WhoId = ta1.whoid,WhatId = ta1.Whatid);
          insert e;
//not sure what you are doing in these next two lines
task t=new task(id=ta1.id);
delete t;
    }
  }
}else{

   //put your code for the insert trigger here

}
}

 

If you provide a bit more information as to what you want to do on insert vs on update I can refine the trigger for you even more

Starz26Starz26

Also,

 

the original code I provided must be run in a before insert trigger not an after. If it must be in an after trigger, further modifications will be needed

This was selected as the best answer
ArikArik

Its ok - all figured out - thx