You need to sign in to do that
Don't have an account?
Apex Test for Invocable Method
Hello,
I am writing a test for an @InvocableMethod and for some reason I keep getting an error that says Invalid Type: SendPdf. Code below, any help is appreciated!!
Controller
I am writing a test for an @InvocableMethod and for some reason I keep getting an error that says Invalid Type: SendPdf. Code below, any help is appreciated!!
Controller
public with sharing class EchoSign { private ApexPages.StandardController standardController; public EchoSign(ApexPages.StandardController standardController){ this.standardController = standardController;} @InvocableMethod public static void SendPdf(List<ID> conid){ List<Contact> con = [SELECT Id, AccountId, Name FROM Contact WHERE Id =: conid LIMIT 1]; pageReference pdfPage = Page.ContactPDF; pdfPage.getParameters().put('Id',con[0].Id); blob pdfBody; pdfBody = pdfPage.getContentAsPDF(); //creation of Adobe Sign Agreement and Attachment here } }Test
@isTest public class Test_EchoSign2 { static testMethod void testechosign(){ Account ac = new Account(RecordTypeId = [SELECT Id, SobjectType, Name FROM Recordtype WHERE Name = 'Prospect/Client' AND SobjectType = 'Account' LIMIT 1].Id); ac.Name = 'TestAcnt'; ac.Type = 'Prospect'; insert ac; Contact con = new Contact(RecordTypeId = [SELECT Id,SObjectType,Name FROM RecordType WHERE Name = 'Prospect/Client' AND SobjectType = 'Contact' LIMIT 1].Id); con.LastName = 'testLastName'; con.AccountId = ac.Id; con.LeadSource = 'Unknown'; insert con; Test.startTest(); PageReference ContactPDF = Page.ContactPDF; ContactPDF.getParameters().put('Id',con.Id); Test.setCurrentPage(ContactPDF); SendPdf pdf = new SendPdf(); } }
And, since it's a static method in a class you access it like so.
List<Contact> contacts = new List<Contact>();
contacts.add(con.Id);
EchoSign.SendPdf(contacts);
See here for more info
https://salesforce.stackexchange.com/questions/96131/how-to-write-a-test-class-for-an-invocablemethod
All Answers
And, since it's a static method in a class you access it like so.
List<Contact> contacts = new List<Contact>();
contacts.add(con.Id);
EchoSign.SendPdf(contacts);
See here for more info
https://salesforce.stackexchange.com/questions/96131/how-to-write-a-test-class-for-an-invocablemethod
Your answer seems to have pushed me in the right direction. The test now errors when run, with the error Methods defined as TestMethod do not support getContent call. New Test looks like below; any thoughts on how to fix? I think the problem lies with the conid.add(con.id); line.
What do you see if you add the following after your insert con line? Does it print out a Contact record?
System.debug('con : ' + con);
It was creating a Contact, no issue there. The issue actually lied on the Class where the pdfBody = pdfPage.getContentAsPDF();
I had to substitue in:
if(Test.isRunningTest()){
pdfBody = blob.valueOf('Unit.Test');
}else{
pdfBody = pdfPage.getContentAsPDF();}
That way it does not ".getContentAsPDF()" which is where the test was failing. Your original answer was the solution, just didn't account for the rewrite of the controller.
Thanks Tad!