• zimmerpk
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies

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());
				}
			}
		}
	}
}
}

 

 

 

I am writing my first controller test and running into an issue.  The controller is used to drive a custom keystroke in the service console.

 

Here is the controller:

public class ShortcutListenerController {

// Lead id for lead to be closed
    public String selectedObjectId {get; set;} 
    public String closeReason {get; set;}

 // Close Lead
    public PageReference closeLead() {
    	system.debug('The Lead Id is :'+ selectedObjectId);
    	system.debug('The Reason is :'+ closeReason);
        Lead selectedLead = [SELECT id, status, Lead_Status_If_Closed_Specify__c FROM Lead WHERE id =:selectedObjectId LIMIT 1];
        selectedLead.status = 'Closed';
        selectedLead.Lead_Status_If_Closed_Specify__c=closeReason;
        update selectedLead;
        return null;
    }
}

 

 

Here is my test script:

@isTest
public class ShortcutControllerTest {

	ShortcutListenerController controller = new ShortcutListenerController();
	
    public static testMethod void shortcuttest() {
        // create lead
         Lead tl = new Lead(
            LastName = '123456', Company= '654321', Phone='1233213222'); 
        insert tl;  
      system.debug('Lead ID ='+tl.id);
      
      ApexPages.currentPage().getParameters().put('selectedObjectId', tl.id);
      ApexPages.currentPage().getParameters().put('closeReason', 'Test');
      
      PageReference pr = controller.closeLead();
      
      Lead t2 =[SELECT id, status, Lead_Status_If_Closed_Specify__c FROM Lead WHERE id =:tl.id];
      
      system.debug('Close Reason ='+t2.Lead_Status_If_Closed_Specify__c);
      system.debug('Status ='+t2.status);
    }
}

 

Why am I getting the error "Method does not exist of incorrect signature: controller.closeLead();

 

Thanks in advance

Phil

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());
				}
			}
		}
	}
}
}