1

I've got a trigger that helps copy some stuff over when a lead is cloned trough a visual workflow. Even though it works properly when testing it in the flow, the plugin fails when trying to unit test the plugin.

The problem is that when invoking the plugin from the test class the inputParamaters don't seem to pass over to the plugin. I confirmed there are actually ID's being put in inputParamater map, but when the plugin tries to query for the parameters they always return null.

(String)request.inputParameters.get('sourceLeadID'); //null

I've tried running as different user, tried 'new' data and existing by using SeeAllData=true, and tried converting the IDs to string first, which also didn't work. I'm thinking that I'm overlooking some simple thing but I ran out of ideas as to where my mistake lies. Any help is much appreciated.

(For some reason I can't get the code formatting right, sorry for that.)

My test code:

@isTest(SeeAllData=true) class testLeadClonedHelper {
    static testMethod void testCloneHelperTest() {

        Lead testLead = ([SELECT Id, Huidige_Eigenaar__c, Laatst_geopend__c, VCSW_id__c,
                           is_Actief__c, Laatst_geopend_door__c, SBI__c,
                           SBI_Nevenactiviteit_A__c, SBI_Nevenactiviteit_B__c,
                           CloneLeadFrom_ID__c FROM Lead LIMIT 1]);
        //Lead testLead = new Lead(FirstName='test12', LastName='foe22', Company='testX22', is_Actief__c=false);                 
        Lead newLead = new Lead(FirstName='test', LastName='foe', Company='testX', is_Actief__c=false);
        //insert testLead;
        insert newLead;

        LeadClonedHelper myPlugin = new LeadClonedHelper();
        Map<String, Object> inputParams = new Map<String, Object>();   
        Map<String, String> outputParams = new Map<String, String>();

        system.debug('sourceLeadId' + testLead.Id);
        system.debug('clonedLeadId' + newLead.Id);
        inputParams.put('sourceLeadId', string.valueOf(testLead.Id));
        inputParams.put('clonedLeadId', string.valueOf(testLead.Id));

        for (Object ip: inputParams.values()) {
            system.debug(ip);
        }

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = myPlugin.invoke(request);

        System.assert(newLead.is_Actief__c);
    }

}

my plugin:

// Converts a lead as a step in a Visual Workflow process.
global class LeadClonedHelper implements Process.Plugin {
    // This method runs when called by a flow's Apex plug-in element.
    global Process.PluginResult invoke(
        Process.PluginRequest request) {

        // Set up variables to store input parameters from 
        // the flow.
        String sourceLeadID = (String)request.inputParameters.get('sourceLeadID');
        String clonedLeadID = (String)request.inputParameters.get('clonedLeadID');


        // Convert the lead by passing it to a helper method.
        Map<String,Object> result = new Map<String,Object>();
        system.debug('===invoke=sourceLeadId' + sourceLeadID);
        system.debug('===invoke=clonedLeadId' + clonedLeadID);   
        result = convertLead(sourceLeadID, clonedLeadID);

        return new Process.PluginResult(result); 
    }

    // This method describes the plug-in and its inputs from
    // and outputs to the flow.
    // Implementing this method adds the class to the 
    // Cloud Flow Designer palette.
    global Process.PluginDescribeResult describe() {
        // Set up plugin metadata
        Process.PluginDescribeResult result = new 
            Process.PluginDescribeResult();
        result.description = 
            'Kopieert SBI en andere voor de' +
            'gebruiker read-only velden naar' +
            'de nieuwe gekloonde lead.';
        result.tag = 'Lead Helpers';


        result.inputParameters = new 
            List<Process.PluginDescribeResult.InputParameter>{
            // Source Lead ID (mandatory)
            new Process.PluginDescribeResult.InputParameter(
                'sourceLeadID', 
                Process.PluginDescribeResult.ParameterType.String, 
                true),
            // Account Id (mandatory)
            new Process.PluginDescribeResult.InputParameter(
                'clonedLeadID', 
                Process.PluginDescribeResult.ParameterType.String, 
                true)
        };

        return result;
    }


    public Map<String,String> convertLead (
                               String sourceLeadID,
                               String clonedLeadID
        ) {
        Map<String,String> result = new Map<String,String>();
        system.debug('====sourceLeadId' + sourceLeadID);
        system.debug('====clonedLeadId' + clonedLeadID);                       
        if (sourceLeadID == null) throw new ConvertLeadPluginException(
            'source Lead Id cannot be null');

        if (clonedLeadID == null) throw new ConvertLeadPluginException(
            'cloned Lead Id cannot be null');

        // Query for source and cloned lead
        Lead sourceLead = [Select Id, Huidige_Eigenaar__c, Laatst_geopend__c, VCSW_id__c,
                           is_Actief__c, Laatst_geopend_door__c, SBI__c,
                           SBI_Nevenactiviteit_A__c, SBI_Nevenactiviteit_B__c,
                           CloneLeadFrom_ID__c From Lead where Id = :sourceLeadID];
        Lead clonedLead = [Select Id, Huidige_Eigenaar__c, Laatst_geopend__c, VCSW_id__c,
                           is_Actief__c, Laatst_geopend_door__c, SBI__c,
                           SBI_Nevenactiviteit_A__c, SBI_Nevenactiviteit_B__c, 
                           CloneLeadFrom_ID__c From Lead where Id = :clonedLeadID];    

        // Check if both exist    
        if (sourceLead != null && clonedLead != null) {

            clonedLead.Huidige_Eigenaar__c = sourceLead.Huidige_Eigenaar__c;
            clonedLead.Laatst_geopend__c = sourceLead.Laatst_geopend__c;
            clonedLead.is_Actief__c = true;
            clonedLead.Laatst_geopend_door__c = sourceLead.Laatst_geopend_door__c;
            clonedLead.CloneLeadFrom_ID__c = sourceLead.Id;
            clonedLead.VCSW_id__c = sourceLead.VCSW_id__c;
            clonedLead.SBI__c = sourceLead.SBI__c;
            clonedLead.SBI_Nevenactiviteit_A__c = sourceLead.SBI_Nevenactiviteit_A__c;
            clonedLead.SBI_Nevenactiviteit_B__c = sourceLead.SBI_Nevenactiviteit_B__c;

            update clonedLead;
            system.debug('===================================================== leadclonehlper');
            result.put(clonedLead.Id, sourceLead.Id);


            } 
        else { 
            throw new ConvertLeadPluginException(
                'No leads found with Id : "' + sourceLeadId + '"' + ' or "' + clonedLeadId + '"');        }
        return result;
    }

    // Utility exception class
    class ConvertLeadPluginException extends Exception {}
}
  • Don't worry about the formatting too much: we'll help you out with that. In the meantime, we'll take a look and see if we can help you out. – sfdcfox Jun 25 '16 at 2:12
  • try to use test.startTest and Test.stopTest and run the code inside the block. – Tushar Sharma Jun 25 '16 at 7:08
  • Here's a tip on formatting code. When you paste code into a post. Select it, then click on the pre-formatted text icon {} and it will move each line over by 4 spaces which will automatically format your code for you. That should help you with posts in the future. All code needs to be spaced over by at least 4 spaces to appear with the blue background as formatted code. – crmprogdev Jun 25 '16 at 12:53
  • @crmprogdev Thx, i tried by typing 4 spaces and then paste code but that works better indeed i see. – Jaap V. Jun 25 '16 at 18:50
  • 1
    Solved it by going for, the much easier, invocablemethod annotation. I would however still like to understand what's wrong with my initial code so i won't mark it as an answer, for future reference(as i can't post my new code) -> [link]andyinthecloud.com/category/invocable-methods – Jaap V. Jun 25 '16 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.