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
Briana Kruse 2Briana Kruse 2 

First Trigger Help

I have my first trigger written as before insert and need to change it to be before insert, before update but I'm stuck! Any ideas?

trigger TaskCustomActivity on Task (before insert,  before update){
if (trigger.isBefore)
{
if(Trigger.isInsert)
{
for(Task.t = trigger.new){
t. custom_activity_type__c = Type;
insert cont;
}
}
else if (Trigger.IsUpdate)
{
Type = '(Update)';
Custom_Activity_Type__c = Trigger.new;
update cont;
}
}
}

Rashi Garg 12Rashi Garg 12
Hi Briana,

Can you please explain what is your actual requirement?. We don't have to write any insert statement in Trigger.isInsert and in same way Update DML in Trigger.isUpdate. It automatically do the same.
 
if (trigger.isBefore)
{
String Type ='';
if(Trigger.isInsert)
{
for(Task t = trigger.new){
t. custom_activity_type__c = Type;
}
}
else if (Trigger.IsUpdate)
{
Custom_Activity_Type__c = Type;
}
}
}

Try above code and on line 3 you can chage type value. Please mark this answer as best answer if it solves your issue.

Thanks,
Rashi
GauravGargGauravGarg
Hi Brianna,

First of all you need to define "cont" before inserting or updating it. It look like you are trying to update or insert Contact information based on Task object. 

Please provide us the actual requirement to further help you out on this.

Thanks,
Gaurav
Briana Kruse 2Briana Kruse 2
Its a simple need. We have a custom field on our task object called Custom Activity Type. Its a text field that was being populated by a WF that was copying the value of Type on the task object and pasting it. The only problem is when we do email to salesforce and a task is created, the WF isn't triggered. I worked with Salesforce support and they said the only way I can do it is to build a trigger. So the trigger should run before insert and before update and take the value from the standard task type field and paste it into the custom field. 
Arpit Jain7Arpit Jain7
Briana, Try below code and it should resolve your issue.

trigger TaskCustomActivity on Task (before insert,  before update)
{
   for(task t:trigger.new)
   {
      t. custom_activity_type__c = t.Type;
   }
}

Thanks.
Rashi Garg 12Rashi Garg 12
Hi Brianna,

Try following sample code.
trigger TaskCustomActivity on Task (before insert,  before update){

for(Task taskRec : Trigger.new){
taskRec.custom_activity_type__c = taskRec.Type;
}

}



Hope this solves your issue.

Thanks,
Rashi