You need to sign in to do that
Don't have an account?
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()); } } } } } }
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.
Thanks
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.