Inputs
The inputs to the action would be:
recordId of the record to be processed
Outputs
Outputs of this custom action would be what Create Links returns
Why do we Need a Custom Mock Class
As explained in the About ClientFactory article, ClientFactory returns a default mocked implementation of Client methods, these default methods may not have return values specific to our use case. And hence for more control, you can define a CustomMock class which extends ClientFactory and inject it as a dependency while getting a ClientFactory instance. Below is a custom mock implementation and how to use it for our use case defined earlier.
Apex Custom Mock Implementation Example
public class CustomMock extends cldfs.ClientFactory{
public override List<List<cldfs.Attachment>> listAttachments(List<String> parentIds,String runAs) {
List<List<cldfs.Attachment>> result = new List<List<cldfs.Attachment>>();
List<cldfs.Attachment> attList = new List<cldfs.Attachment>();
cldfs.Attachment file = new cldfs.Attachment();
file.id = 'testid-1';
file.resource = new cldfs.Resource(
'testid-1',
'file',
'test file.pdf',
'library',
'driveId-1'
);
cldfs.Attachment folderAttachment = new cldfs.Attachment();
folderAttachment.id = 'testfolderid-2';
folderAttachment.resource = new cldfs.Resource(
'testfolderid-2',
'folder',
'folder name-2',
'library',
'driveId-1'
);
attList.add(file);
attList.add(folderAttachment);
result.add(attList);
return result;
}
public override List<cldfs.Link> createLinks(List<cldfs.Types.CreateLinkInput> linkInputs, String library, String runAs) {
List<cldfs.Link> links = new List<cldfs.Link>();
for (Integer i = 0; i < linkInputs.size(); i++) {
cldfs.Link l = new cldfs.Link();
l.id = 'testid-' + String.valueOf(i);
l.url = 'http://test.url/' + String.valueOf(i);
links.add(l);
}
return links;
}
}Apex Custom Action: Generate PDF Links from Attachments
public with sharing class GeneratePdfLinksAction {
public static List<cldfs.Link> GeneratePdfLinksAction(Id recordId) {
cldfs.ClientFactory client = cldfs.ClientFactory.getInstance(new CustomMock());
List<List<cldfs.Attachment>> attachments = client.listAttachments(new List<Id>{recordId}, 'integrationUser');
List<cldfs.Attachment> attachment = attachments[0];
List<cldfs.Types.CreateLinkInput> linkInputs = new List<cldfs.Types.CreateLinkInput>();
String library = attachment[0].resource.library;
for (cldfs.Attachment attach : attachment) {
cldfs.Resource resource = attach.resource;
if (resource.name.endsWith('.pdf')) {
linkInputs.add(
new cldfs.Types.CreateLinkInput(
resource.id,
resource.type,
recordId,
null,
resource.driveId
)
);
}
}
return client.createLinks(
linkInputs,
library,
'integrationUser'
);
}
}Apex Test Class for GeneratePdfLinksAction
@IsTest
public with sharing class TestGeneratePdfLinksAction {
@TestSetup
public static void setupData(){
Account acc = new Account(Name = 'Test Account');
insert acc;
}
@IsTest
public static void TestGeneratePdfLinksAction() {
Account account = [SELECT Id FROM Account LIMIT 1];
Test.startTest();
List<cldfs.Link> links= GeneratePdfLinksAction.GeneratePdfLinksAction(account.Id);
Test.stopTest();
Assert.isNotNull(links);
}
}