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
Michael MMichael M 

Test class for invocable class for bot

Hello, I have the following apex class that I want to invoke from my einstein bot. Can someone help me with writing the test class for this code? 

public with sharing class BotUrlClass {
public class TranscriptInput {
    @InvocableVariable(required=true)
    public ID routableID;
  }
  
  public class VisitorUrlOutput {
    @InvocableVariable(required=true)
    public String url;
  }
 
  @InvocableMethod(label='Get User Name')
  public static List<VisitorUrlOutput> getUserName(List<TranscriptInput> transcripts) {
 
    List<VisitorUrlOutput> urls = new List<VisitorUrlOutput>();
  
    for (TranscriptInput transcript : transcripts) {
    
      // Query for the transcript record based on the ID
      LiveChatTranscript transcriptRecord = [SELECT Name, URL__c
                                             FROM LiveChatTranscript 
                                             WHERE Id = :transcript.routableID 
                                             LIMIT 1];
      
      // Store the first name in an output variable
      VisitorUrlOutput urlData = new VisitorUrlOutput();
      urlData.url = transcriptRecord.URL__c;
      
      // Add the name to the list of outputs
      urls.add(urlData);
    }
    
    return urls;
  }
}
Best Answer chosen by Michael M
AnudeepAnudeep (Salesforce Developers) 
Michael - I made a mistake with the syntax. Use the following instead
 
BotUrlClass.TranscriptInput trans = new BotUrlClass.TranscriptInput(); 
trans.routableID = '//populate id here';


Same with other variables

All Answers

AbhishekAbhishek (Salesforce Developers) 
https://salesforce.stackexchange.com/questions/249123/creating-test-class-for-einstein-bot-invocable-apex-class-to-search-articles

Try this once.

For refernce,

http://amitsalesforce.blogspot.com/2018/07/einstein-bots-with-apex-class-how-to.html

Thanks.
AnudeepAnudeep (Salesforce Developers) 
Hi Michael, 

The invocable method is just a static method. You can simply call it on your test to verify that it works as intended.
 
@isTest
private class BotUrlClassTest {

    private static testMethod void doTest() {

    List<Transcriptinput> inputList = new    List<TranscriptInput>();
        Test.startTest(); 
      
TranscriptInput input = new TranscriptInput();

input.routableID = 'populate an id here...';

inputList.add(input); 

VisitorUrlOutput output = new VisitorUrlOutput();

output.url = 'enter valid url here';

        BotUrlClass.getUserName(inputList);

    Test.stopTest();

    
    }
}

Let me know if it gives you any coverage

Regards, 
Anudeep
Michael MMichael M
Hi Anudeep,
Thank you very much for sending. I am trying to save this class but it is giving me errors, saying "Invalid type: Transcriptinput'. Here are the "Problems" it is showing: 

User-added image
AnudeepAnudeep (Salesforce Developers) 
Michael - I made a mistake with the syntax. Use the following instead
 
BotUrlClass.TranscriptInput trans = new BotUrlClass.TranscriptInput(); 
trans.routableID = '//populate id here';


Same with other variables
This was selected as the best answer
Michael MMichael M
Hi Anudeep, it's still saying that List<TranscriptInput> is an invalid type. 
AnudeepAnudeep (Salesforce Developers) 
Michael wherever Transcriptinput is referenced it should be BotUrlClass.TranscriptInput 

List<BotUrlClass.TranscriptInput>

See this example

Let me know if this helps, if it does, please close the query by marking it as solved. It may help others in the community. Thank You!
Michael MMichael M
Oh I see! Ok so it is now allowing me to save. But it did not pass the test. The error message for why it did not pass was this: System.QueryException: List has no rows for assignment to SObject   Class.BotUrlClass.getUrlName: line 20, column 1
Class.BotUrlTESTClass.doTest: line 20, column 1

Here is what the test class is looking like:
@isTest
public class BotUrlTESTClass {
    private static testMethod void doTest() {
//LiveChatTranscript lct = new LiveChatTranscript();
 //       insert lct;
        
List<BotUrlClass.Transcriptinput> inputList = new List<BotUrlClass.TranscriptInput>();
        Test.startTest(); 
      
BotUrlClass.TranscriptInput input = new BotUrlClass.TranscriptInput(); 

input.routableID = '5703l000003EF3NAAW';

inputList.add(input); 

BotUrlClass.VisitorUrlOutput output = new BotUrlClass.VisitorUrlOutput();

output.url = 'https://www.google.com';

        BotUrlClass.getUrlName(inputList);

    Test.stopTest();

    
    }

}
Michael MMichael M
Thank you!