I'm having difficulty finding the syntax to set my invocable variables in a test class related to the below code. I just put the relevant top part of the code in there. How can I set the ids to my test objects or change my code to be more easily testable?

Thank you,

global class ShareFilesToAgreements {

    //Declare variables to be generated from Process Builder
global class AgreementParameter{
    global id OpptyId;
    global id AgreementId;

//Use invocable method to launch from Process Builder
global static void linkFilesToAgreement(AgreementParameter [] AgreementParameters) {

        Id OpptyId = AgreementParameters[0].OpptyID;
        Id AgreementId = AgreementParameters[0].AgreementId;

    //Query ContentDocumentLinks related to Opportunity and then the Files from that Id list
    list<id> cdids = new List<id>();
    for (ContentDocumentLink cdl : [select ContentDocumentId from ContentDocumentLink where LinkedEntityID = :OpptyId]){
    ContentDocument[] ContentDocuments = [select Id, Title from ContentDocument where Id IN :cdids];
    System.debug('ContentDocumentLink Query:' + cdids);

It's not any different by being an InvocableMethod than any other test code. Your test class should create and insert an Opportunity and Agreement, and then call your method with an instance of AgreementParameter containing those parameters.

Opportunity o;
Agreement__c a;

// Create data that's relevant, like ContentDocuments, as well as `o` and `a`.

// Now, create the argument data structure for the InvocableMethod
ShareFilesToAgreements.AgreementParameter argument;
argument = new ShareFilesToAgreements.AgreementParameter();
argument.OpptyId = o.Id;
argument.AgreementId = a.Id;

    new List<ShareFilesToAgreements.AgreementParameter>{ argument }

// Make assertions about the behavior of the code.
| improve this answer | |

On the same lines as David's answer, that having the annotations doesn't stop you from using normal Apex constructs, you can add methods or constructors to simplify product or test code e.g.:

global class AgreementParameter{

    global id OpptyId;
    global id AgreementId;

    AgreementParameter(id OpptyId, id AgreementId) {
        this.OpptyId = OpptyId;
        this.AgreementId = AgreementId;

so that in a test:

ShareFilesToAgreements.linkFilesToAgreement(new ShareFilesToAgreements.AgreementParameter[] {
    new ShareFilesToAgreements.AgreementParameter(o.Id, a.Id)
| improve this answer | |

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.