2

I'm trying to put in place a screen flow component in order to display knowledge articles based on the case fields and Knowledge-articles data category.

In order to do so, I've created an apex class that I use to query for my KnowledgeArticleVersion that I will be using in my flow.

Here's my Apex Class:

public class KnowledgeArticleLookup {

   public static List<KnowledgeArticleVersion> getMyArticles (){
     
     string varType1;
     string varType2;
     List<KnowledgeArticleVersion> Articles = new List<KnowledgeArticleVersion>();
   
    if(varType1 == 'Global Info'){
        If(varType2 == 'Location'){
                Articles = [SELECT Id,Title FROM KnowledgeArticleVersion WHERE PublishStatus='online' with DATA CATEGORY Global_Knowledge__c AT Location__c];
        }
     }
      return Articles;
 }

Knowing that vartype1 and vartype2 are values used in my flow ( in a get element), I was wondering how I can pass these two variables to my apex class and how I can pass my Articles(list) to my Flow.

1 Answer 1

5

To create an input and output from flow to apex or vice versa, you will need to ensure you annotate them as @InvocableVariable.

To make your apex method available as an action, annotate the method with @InvocableMethod

So here is how you will modify your apex,

public class KnowledgeArticleLookup {

   @InvocableMethod(label='Get Knowledge Articles' description='Gets knowledge article results')
   public static List<List<Response>> getMyArticles (List<Request> req){
    List<KnowledgeArticleVersion> Articles = new List<KnowledgeArticleVersion>();

    if(req[0].varType1 == 'Global Info'){
      if(req[0].varType2 == 'Location'){
            articles = [SELECT Id,
                              Title 
                             FROM KnowledgeArticleVersion 
                             WHERE PublishStatus='online' 
                             WITH DATA CATEGORY 
                             Global_Knowledge__c AT Location__c];
      }
    }
    List<List<Response>> outputResponse = new List<List<Response>>();
    List<Response> responses = new List<Response>();
    for(KnowledgeArticleVersion kav: articles) {
         Response response = new Response();
         response.articleversionId = kav.Id;
         response.articleTitle = kav.Title;
         responses.add(response);
    }
    outputResponse.add(responses);
   return outputResponse;
  }

  public class Request {
    @InvocableVariable(required=true)
     public String varType1;
    @InvocableVariable(required=true)
     public String varType2;
   }

   public class Response {
      @InvocableVariable
       public String articleversionId;
      @InvocableVariable
       public String articleTitle;
   }
}
11
  • Hi @Mohith, Thanks for your response. I copied and pasted the code that you provided. However, I get the following error : Illegal conversion from Object to List<List<KnowledgeArticleVersion>>
    – user52619
    Aug 5, 2021 at 14:46
  • btw, methods with @Invocable annotation accept only one parameter
    – user52619
    Aug 5, 2021 at 14:54
  • You can create a wrapper class for response and request and that should be good enough to fix these. Hope that helps Aug 5, 2021 at 15:34
  • I did create my wrapper class which looks like this: public class Request{ @InvocableVariable(required=true) public String varType1; @InvocableVariable(required=true) public String varType2; } public class Result{ @InvocableVariable public List<List<KnowledgeArticleVersion>> outputResponse; } but what should i put as a return value? i'm confused.
    – user52619
    Aug 5, 2021 at 16:24
  • I did update the answer with wrapper Aug 5, 2021 at 16:25

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.