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
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....
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....)
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.
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
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
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
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.
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....
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....)
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
trigger UpdateConvert on Task (Before Insert){
for(Task t : trigger.new){
if(t.Subject == 'convert')
t.convert__c = true;
}
}
Thank you - I keep getting a variable doesnt exist error on the convert__c line but that is te name of the variable
sorry, it should be t.convert__c
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.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.
Im doing something wrong, because it wont save
When you say substitiute - where exactly should it be in here:
Try
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
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
Its ok - all figured out - thx