Merge Mappings From Custom Programming

Generate a Document

If you're looking a way to trigger the merge of a specific mapping from code in your Org, we've built a global namespace that allows you to merge documents from anywhere inside your SFDC instance.  Here is what the function looks like:

Boolean WebmergeGenerator.generateDocument(
    String webmergeMappingId, 
    String salesforceRecordId, 
    String salesforceObjectName
);


As you can see from the definition above, you need to pass along the Mapping ID, the Record ID, and the name of the Object.  With these 3 parameters, we can automatically trigger the merge. 

Here is an example of what this might look like in your Salesforce APEX code.

Boolean mergeSuccess = webm.WebmergeGenerator.generateDocument(
    'XYZ00000001234', 
    'ABC00000000123', 
    'Account'
);

 

Preview a Document

If you're looking a way to trigger the preview for a specific mapping, you can use previewDocument(). Here is what the function looks like:

String WebmergeGenerator.previewDocument(
    String webmergeMappingId, 
    String salesforceRecordId, 
    String salesforceObjectName
);


As you can see from the definition above, you need to pass along the Mapping ID, the Record ID, and the name of the Object. With these 3 parameters, we can automatically trigger the preview.

Here is an example of what this might look like in your Salesforce APEX code.

String fileURL = webm.WebmergeGenerator.previewDocument(
    'XYZ00000001234', 
    'ABC00000000123', 
    'Account'
);

 

Get Generated Document Attachments

If you're looking for a way to get a list of the documents that were created, you can use the following function:

List<Attachment> WebmergeGenerator.generateDocumentGetAttachments(
    String webmergeMappingId, 
    String salesforceRecordId, 
    String salesforceObjectName
);


This function returns a list of attachments (even if there is only 1 attachment, it still returns a list) that have been created (but not inserted/committed) for the record.

Here is an example of what this might look like in your Salesforce APEX code.

List<Attachment> generatedAttachments = webm.WebmergeGenerator.generateDocumentGetAttachments(
    'XYZ00000001234', 
    'ABC00000000123', 
    'Account'
);

 

Generate Documents in Batch

If you're looking for a way to generate multiple documents in batch (for example, from a list view), you can use this function:

Boolean generateDocumentBatch(
    String webmergeMappingId, 
    List<String> salesforceRecordIds, 
    String salesforceObjectName
);


This function returns a boolean true/false base on the success of creating the batch.

Here is an example of what this might look like in your Salesforce APEX code.

 

List<String> sfRecords = new List<String> {'ABC00000000123', 'ABC00000000789'}; 
Boolean success = webm.WebmergeGenerator.generateDocumentBatch(
    'XYZ00000001234', 
    sfRecords, 
    'Account'
);

 

Generate Batch Combined PDF

If you're looking for a way to generate multiple documents in batch and combine them into a single PDF, you can use this function:

Boolean generateDocumentBatchCombined(
    String webmergeMappingId, 
    List<String> salesforceRecordIds, 
    String salesforceObjectName
);


This function returns a boolean true/false base on the success of creating the batch.

Here is an example of what this might look like in your Salesforce APEX code.

List<String> sfRecords = new List<String> {'ABC00000000123', 'ABC00000000789'}; 
Boolean success = webm.WebmergeGenerator.generateDocumentBatchCombined(
    'XYZ00000001234', 
    sfRecords, 
    'Account'
);

 

Merge Document without Mapping

If you're looking for a way to send data over to WebMerge to merge into a document (without a mapping), you can use this function:

HttpResponse doMerge(
    Map<String, String> mergeData,
    String mergeUrl
);


This function returns the HttpResponse from the merge request. You can save the file/response from there.

Here is an example of what this might look like in your Salesforce APEX code.

Map<String, String> mergeData = new Map<String, String>{};
mergeData.put('Name', 'John Smith');
mergeData.put('Phone', '434-555-3499');
HttpResponse response = webm.WebmergeGenerator.doMerge(
    mergeData, 
    'https://www.webmerge.me/merge/12345/abcdef'
);

 

Merge JSON Data

You can also use JSON to send data over to your WebMerge document:

HttpResponse doMerge(
    String jsonString,
    String mergeUrl
);

 

Here is an example of what this might look like in your Salesforce APEX code.

String jsonString = '{"Name":"John Smith", "Phone":"434-555-3499"}';
HttpResponse response = webm.WebmergeGenerator.doMerge(
    jsonString, 
    'https://www.webmerge.me/merge/12345/abcdef'
);

 

Preview Document

You can also preview a merge using doPreview() instead of doMerge():

HttpResponse doPreview(
    Map<String, String> mergeData,
    String mergeUrl
);

HttpResponse doPreview(
String jsonString,
String mergeUrl
);

 

Merge Report Data

If you're looking for a way to send report data over to WebMerge to merge into a document, you can use this function:

Boolean mergeReport(
    String reportId,
    String mergeUrl
);


This function returns a boolean true/false base on the success of creating the merge.

Here is an example of what this might look like in your Salesforce APEX code.

Boolean success = webm.WebmergeGenerator.mergeReport(
    'RRR00000001234', 
    'https://www.webmerge.me/merge/12345/abcdef'
);
Have more questions? Submit a request

20 Comments

  • Avatar
    Steve Jones

    I've been successfully using generateDocumentBatch, and want to try producing a single pdf using generateDocumentBatchCombined.
    When I try it however I don't see any merge. I'm using a data route which is adding a coversheet and delivering to Dropbox. I've tried test mode on and off. Salesforce is logging batch apex activity as normal, but there's no record of a merge in the Data Route overview and no document is produced.

  • Avatar
    Jeremy Clarke

    Hi Steve,

    Please submit a support ticket and we'll help you from there!

    Thanks :)

  • Avatar
    Muriel DENAIS

    Hi, I'm getting the following error when my batch is running :
    First error: webm:Too many SOQL queries: 201

    Where can I define the batch size, please ?

  • Avatar
    Jeremy Clarke

    Hi Muriel,

    Can you create a support ticket and send us a snippet of your code so we can see what you're doing?

    Thanks!

  • Avatar
    Pradnya Desai

    Hi Jeremy,

    How can we use One document to generateDocument and Generate preview.
    Do you have 'generatePreview' method?

    Thanks.

  • Avatar
    Jeremy Clarke

    Hi Pradnya,

    We have added a function called previewDocument() to the package (version 1.199). Please install the latest version of our managed package from the AppExchange and you can find details of previewDocument() above.

    Thanks!

  • Avatar
    Connor Giles

    Hi Jeremy,

    If we use generateDocumentBatchCombined() is there a way to then get the result of that PDF in code or do we need to use a delivery option for that?

    Thanks!

  • Avatar
    Jeremy Clarke

    Hi Connor,

    Unfortunately, you'll need to use a delivery option for documents generated in Batch.

    Thanks!

  • Avatar
    Pradnya Desai

    Hi Jeremy,
    How can we send email for the document generated using previewDocument method.

    Thanks,
    Pradnya

  • Avatar
    Jeremy Clarke

    Hi Pradnya,

    Unfortunately, there isn't a way to send an email from the previewDocument method because it's meant to be a real-time preview. You'll need to use the mergeDocument method instead to trigger the email delivery you have setup on the document.

    Thanks!

  • Avatar
    Pradnya Desai

    Hi Jeremy,
    Is there any way to generate document in Draft mode.

    Thanks,
    Pradnya

  • Avatar
    Jeremy Clarke

    Hi Pradnya,

    Unfortunately, the only option is to use the doMerge() method where you can provide the Merge URL to the document and you can add "?test=1" parameter to the URL. This will merge the document in test mode and still send the email deliveries.

  • Avatar
    Robby Johnson

    What should the value be for the mergeURL variable in the mergeReport function? thanks

  • Avatar
    Robby Johnson

    I found it on the Custom Integration section of the Merge tab for the document. should have looked better -- thanks

  • Avatar
    Jeremy Clarke

    Robby - Glad you found it :)

  • Avatar
    Saraswathi D

    Hi Jeremy Clarke,

    How do i attach merged file/document to the any one of salesforce object using Merge Document without Mapping method ?

    Thanks in advance

  • Avatar
    Jeremy Clarke

    Hi Saraswathi,

    You'll want to make sure you have "download=1" as a URL parameter in your Merge URL, then our system will return the document in the response and you can use code like this:

    Attachment attachment = new Attachment();
    attachment.Body = response.getBodyAsBlob();;
    attachment.Name = 'File Name.pdf';
    attachment.ParentId = sfObject.Id;
    attachment.ContentType = 'application/pdf';
    insert attachment;

  • Avatar
    Joe Stolz

    Hello I made an invocable method using both generateDocumentGetAttachments and previewDocument.

    When using the method in anonymous apex it runs perfectly, when running in a trigger or firing it in a process builder - process, GetAttachments returns null and doPreview returns a blank string.

    To my knowledge there were no error messages.

    In the debug logs for the process it shows soql queries that the functions (getAttachments and doPreview) are apparently making. The soql queries and # of rows returned were identical in both the anon apex call and process builder- process call.

    Is there something i'm missing here? I'm running in a sandbox for all current testing. I verified the doc I want to merge as well as the mapping is present. There's virtually little to no difference in how i'm calling the function in anon apex vs. process builder - process.

  • Avatar
    Jeremy Clarke

    Hi Joe,

    Based on the detail you gave, I'm 90% sure that Salesforce is blocking the callout over to WebMerge due to this: https://help.salesforce.com/articleView?id=000079772&type=1

    There are a few suggested fixes on this help article from Salesforce.

    Thanks!

  • Avatar
    Joe Stolz

    Hello Jeremy, It appears moving the callout to a @future function fixed the issue. I'm still not entirely sure why but I greatly appreciate the help.

    I read another article about callouts being blocked when SF readwrite mode is in read-only but I verified it was in default mode when the callout happened.

    Would like to learn more about how callouts are handled at certain times in triggers and process but that's getting off the subject of webmerge.

    Again, Thank you.

Please sign in to leave a comment.
Powered by Zendesk