0

My scenario is I have a lightning flow, which is a form basically, would capture users input and do searching by apex method.

public class AccountController {
    public class flowInput {
        @InvocableVariable public String inputId;
    }
    
    @InvocableMethod(label='Get Account Names'description='Pass list of ids in, then return list of name.' category='account')
    public static List<String> getAccountNames(List<flowInput> params) {
        List<String> accountNames = new List<String>();
        String varId = params[0].inputId;
        List<Account> accounts = [
            SELECT Name
            FROM Account
            WHERE Id = :varId
        ];
        
        for (Account account : accounts) {
            accountNames.add(account.Name);
        }
        
        return accountNames;
    }
}

But the problem I encountered was whenever I set the input box from screen as the apex method parameter and click “Done” button, the system would not save my setting and the reference disappear. Please help. enter image description here

In addition, if I have a lwc and output var as an array like below:

[ { id:’123’, name:’abc’} ]

How can I modify my apex code to handle it?

0

For the first problem, I think it is my iPad problem as I was using iPad to edit my flow. After switching to my windows pc, the bug solved.

For the second problem, I have a workaround solution for that. Not sure whether it's the best solution. Suppose I have a lwc that store users input in the following form:

var input = [{ Name: 'abc', Phone: '12345' }];

I pass this list as String to the flow by

<targetConfig targets="lightning__FlowScreen">
   <property name="lwcInput" type="String" label="User Input From LWC"></property>
</targetConfig>
@api lwcInput = JSON.stringify(this.input);

The flow will accept this JSON string and pass this as parameter to flow invocable apex method. The code will first accept the JSON string and transform it into a self-declared class variable and use it for SOQL querying. The apex code are as following:

public with sharing class DuplicationCheck {
    
    public class flowInputVar {
        @InvocableVariable public String jsonString;
    }
    
    public class Params {
        public String Name;
        public String Phone;
        
        public Params (String Name, String Phone) {
            this.Name= Name;
            this.Phone= Phone;
        }
        
    }
    
    @InvocableMethod
    public static List<List<Account>> getAccount(List<flowInputVar> inputVars) {
        Params param;
        
        JSONParser parser = JSON.createParser(inputVars[0].jsonString);
        while (parser.nextToken() != null) {
            if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
                while (parser.nextToken() != null) {
                    if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
                        param = (Params)parser.readValueAs(Params.class);
                        parser.skipChildren();
                    }
                }
            }
        }
        
        List<List<Account>> results = new List<List<Account>>();
        List<Account> accountResults = new List<Account>();
        
        if (param.Name!= null || param.Phone!= null) {
            accountResults = [
                SELECT Id
                FROM Account
                WHERE (Name= :Name) AND (Phone= :param.Phone)
            ];   
        }
        
        if (!accountResults .isEmpty()) {
            results.add(accountResults );
        }
        
        return results;
        
    }
    
}

Not sure whether it is the best solution or not but anyway it works. For those who share the same scenario with me, you can try this method. If you find any better solutions, please feel free to comment below. Thanks!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.