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
zimmerpkzimmerpk 

Update fields on Tasks via Workflow not on Workflow Layout - Code Inside

So I have always been bothered that if you want to create tasks via workflow, you could only set certain predefined fields.  There would always have to be some custom apex trigger running to update other fields.  I wanted to come up with a standard  way to solve this.  My thought was to use the Description/Comments field on the task to drive other fields to update.

 

So when using the workflow "Create a Task" screen you would add a special string to the end of the comments such as:

 

"My real comment is this part of the message. >>Type=Meeting::Custom_Field__c=Custom Value"

 

My apex trigger will take everything after the ">>" then split it based on the "::" and update the proper fields.  This code will be a huge time saver for me so I thought I would share it.

This is my first code share so feedback is appreciated.

 

trigger WFTaskCreationExtend on Task (before insert) {
	
for(Task t:Trigger.new){
	List<string> parsedesc = t.Description.substringAfter('>>').split('::');
	t.description=t.Description.substringBefore('>>');
	if(parsedesc[0].length()>0){
		for(string p:parsedesc){
			system.debug('Update expression: '+p);
			try{
				t.put(p.substringBefore('='), p.substringAfter('='));
			}
			catch (System.SObjectException e) {
				if(e.getMessage().contains('Decimal')){
					t.put(p.substringBefore('='), decimal.valueof(p.substringAfter('=')));
				}
				else{
					System.debug('Field Error: '+e.getMessage());
				}
			}
		}
	}
}
}

 

 

 

tommytxtommytx

Wow! That looks like a neat piece of code... I will study it in detail, as I am slurping up everything trying to learn as fast a possible and this looks like several tidbits I can learn from.  Wow Again... I just noticed this is only your second post and you are writing code like a pro... were you a born apex writer... or is it just natural for you.... I did not learn Java first so its a real challenge for me.


Once I play with it I will definitely give you some feed back.
Thank you very much for sharing.

tommytxtommytx
I was able to get it working great in the sandbox.... any chance you might share the test code with us... if its written yet?
Thanks
zimmerpkzimmerpk

glad it worked for you.  no test code yet.  also you should check to make sure that Description is not null before parsing it.  If you dont the code errors.