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
Matthias RivertiMatthias Riverti 

How to make a invocable method have a return value

Hello,

I have this method that works fine: 
Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Lead> Leads)
    {
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
        System.debug('Lead Converted Status' + CLeadStatus.MasterLabel);
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(Lead currentlead: Leads){
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead.Id);                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                if (currentlead.Account__c != null) {
                    System.debug('Lead Account' + currentlead.Account__c);
                    Leadconvert.setAccountId(currentlead.Account__c);
                }
            	MassLeadconvert.add(Leadconvert);
        }
        System.debug('List Convert' + MassLeadconvert);
        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        	System.debug('Result' + lcr);
        }
    }
}

But I want to add a return value.
The value would be the variabe "lcr" which is List<Database.LeadConvertResult>. It could also be a list of strings or opportunity.

When I'm trying this :
Public class AutoConvertLeads
{
    @InvocableMethod
    public static List<Database.LeadConvertResult> LeadAssign(List<Lead> Leads)
    {
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
        System.debug('Lead Converted Status' + CLeadStatus.MasterLabel);
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(Lead currentlead: Leads){
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead.Id);                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                if (currentlead.Account__c != null) {
                    System.debug('Lead Account' + currentlead.Account__c);
                    Leadconvert.setAccountId(currentlead.Account__c);
                }
            	MassLeadconvert.add(Leadconvert);
        }
        System.debug('List Convert' + MassLeadconvert);
        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        	System.debug('Result' + lcr);
            return lcr;
        }
    }
}

I'm having the following error : InvocableMethod methods do not support return type of List<Database.LeadConvertResult>
I have tested  all kind of value like string etc but none of those are accepted.

Do you have any idea ?

Thanks,
Matthias
Best Answer chosen by Matthias Riverti
vishal-negandhivishal-negandhi

Hi Matthias, 

If you go through this Salesforce documentation (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm), it says

If the return type is not Null, the data type returned by the method must be one of the following:
A list of a primitive data type or a list of lists of a primitive data type – the generic Object type is not supported.
A list of an sObject type or a list of lists of an sObject type.
A list of the generic sObject type (List<sObject>) or a list of lists of the generic sObject type (List<List<sObject>>).
A list of a user-defined type, containing variables of the supported types above or user-defined Apex types, with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure that your class contains at least one member variable with the invocable variable annotation.

You're trying to return a list of Database.LeadConvertResult and that's why you get the error. It needs to be a list of a primitive data type, so try with a list of ID or list of String, in the same link there's an example where a list of ID is returned, you can have a look at that for reference.

 

Hope this helps.

All Answers

AbhishekAbhishek (Salesforce Developers) 
Check the suggestions here,

https://salesforce.stackexchange.com/questions/83944/accessing-the-return-value-of-an-invocablemethod-in-process-builder


For further reference check this too,

https://developer.salesforce.com/forums/?id=9060G000000BhQJQA0


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
vishal-negandhivishal-negandhi

Hi Matthias, 

If you go through this Salesforce documentation (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm), it says

If the return type is not Null, the data type returned by the method must be one of the following:
A list of a primitive data type or a list of lists of a primitive data type – the generic Object type is not supported.
A list of an sObject type or a list of lists of an sObject type.
A list of the generic sObject type (List<sObject>) or a list of lists of the generic sObject type (List<List<sObject>>).
A list of a user-defined type, containing variables of the supported types above or user-defined Apex types, with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure that your class contains at least one member variable with the invocable variable annotation.

You're trying to return a list of Database.LeadConvertResult and that's why you get the error. It needs to be a list of a primitive data type, so try with a list of ID or list of String, in the same link there's an example where a list of ID is returned, you can have a look at that for reference.

 

Hope this helps.

This was selected as the best answer