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
TheresaAndersonTheresaAnderson 

Looking for a trigger to create a task from a custom field on an account record

Can anyone share sample code to create a task based on a custom field on an Account record?  I believe I'm making my code way too difficult.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
raseshtcsraseshtcs
trigger CreateAlertTaskonAcct on Account (before update) {
  List<Task> ltask = new List<Task>();
	for(Account a: Trigger.new){
		if(a.Alert_Status__c=='High' && a.Alert_description__c != Trigger.oldMAp.get(a.Id).Alert_description__c){
			Task t = new Task();
		     t.WhatID=a.Id;
		     t.Subject='Red Alert';
		     t.Status='Completed';
		     t.Priority='Red';
		     t.Description=a.Alert_Description__c;
		     t.Project_Comments__c=a.Alert_Description__c;
		     t.Project_Status_Date__c=a.Alert_Timestamp__c;
		     t.OwnerId=a.OwnerId;
		     t.ActivityDate=system.today();
		     t.RecordTypeId='xxxxxxxxxxxxxxxxxxx';
		     ltask.add(t);	

		}
		else if(){	
		//Similar to above
		}
	
	}
	Insert ltask
}

 I am not 100% sure why it is creating 4 tasks. May be you can put a system.debug just before ltask.add() stmt and check what all tasks are getting added to the list. That way you can check what all is causing the trigger to create 4 tasks. The code snippet above shows the use of oldmap which returns the old object. It prevents the use of two for loops.

All Answers

raseshtcsraseshtcs
trigger AccountTrigger on Account (After Insert) {
  List<Task> ltask = new List<Task>();
  for(Account a: Trigger.new){
   if(a.field=='desired value'){
     Task t = new Task();
     t.WhatID=a.Id;
     //Add rest of the mandatory fields with specific values like above
    ltask.add(t);
   }
  }
  Insert ltask;
}

 a.field is the field which would drive the creation of the task. Hope it helps

TheresaAndersonTheresaAnderson

Thank you for the example - it's exactly what I needed.

 

Since ISCHANGED is not allowed in a trigger, what are my options?

raseshtcsraseshtcs

You will need to check the old and the new value of the field using Trigger.old and Trigger.new

TheresaAndersonTheresaAnderson

I have one more question -   the code is working, but the code is creating 4 tasks.

 

trigger CreateAlertTaskonAcct on Account (before update) {
  List<Task> ltask = new List<Task>();
  for(Account o: Trigger.old){
   for(Account a: Trigger.new){
    if(a.Alert_Status__c=='High' && o.Alert_description__c!=a.Alert_description__c){
     Task t = new Task();
     t.WhatID=a.Id;
     t.Subject='Red Alert';
     t.Status='Completed';
     t.Priority='Red';
     t.Description=a.Alert_Description__c;
     t.Project_Comments__c=a.Alert_Description__c;
     t.Project_Status_Date__c=a.Alert_Timestamp__c;
     t.OwnerId=a.OwnerId;
     t.ActivityDate=system.today();
     t.RecordTypeId='xxxxxxxxxxxxxxxxxxx';
     ltask.add(t);
   }
   Else if(a.Alert_Status__c=='Medium' && o.Alert_description__c!=a.Alert_description__c){
     Task t = new Task();
     t.WhatID=a.Id;
     t.Subject='Yellow Alert';
     t.Status='Completed';
     t.Priority='Yellow';
     t.Description=a.Alert_Description__c;
     t.Project_Comments__c=a.Alert_Description__c;
     t.Project_Status_Date__c=a.Alert_Timestamp__c;
     t.OwnerId=a.OwnerId;
     t.ActivityDate=system.today();
     t.RecordTypeId='xxxxxxxxxxxxxxxxxxx';
     ltask.add(t);
    }
  }
  }

  Insert ltask;
}

 

raseshtcsraseshtcs
trigger CreateAlertTaskonAcct on Account (before update) {
  List<Task> ltask = new List<Task>();
	for(Account a: Trigger.new){
		if(a.Alert_Status__c=='High' && a.Alert_description__c != Trigger.oldMAp.get(a.Id).Alert_description__c){
			Task t = new Task();
		     t.WhatID=a.Id;
		     t.Subject='Red Alert';
		     t.Status='Completed';
		     t.Priority='Red';
		     t.Description=a.Alert_Description__c;
		     t.Project_Comments__c=a.Alert_Description__c;
		     t.Project_Status_Date__c=a.Alert_Timestamp__c;
		     t.OwnerId=a.OwnerId;
		     t.ActivityDate=system.today();
		     t.RecordTypeId='xxxxxxxxxxxxxxxxxxx';
		     ltask.add(t);	

		}
		else if(){	
		//Similar to above
		}
	
	}
	Insert ltask
}

 I am not 100% sure why it is creating 4 tasks. May be you can put a system.debug just before ltask.add() stmt and check what all tasks are getting added to the list. That way you can check what all is causing the trigger to create 4 tasks. The code snippet above shows the use of oldmap which returns the old object. It prevents the use of two for loops.

This was selected as the best answer