Free Support Forum -

Word Template MailMerge with data from Salesforce Apex using Aspose.Words Cloud


I’m trying (and not succeeding) to MailMerge a word document with data using a PUT request from Apex code.
The sample given on Github uses a dataFile and the POST method and using version 1.1 of the api.

Any sample using a PUT request would be more than helpful.



Thanks for your inquiry. We are looking into the issue and will update you shortly.


Thanks for your patience. We are working on Github APEX samples and will update these samples as well soon. Meanwhile, please find APEX implementation of Mail Merge PUT request. Hopefully it will help you to accomplish your task.

PUT ​/words​/{name}​/MailMerge

//Function to get the JSON WEB Token

public static string GetJWT(String client_id, String client_secret){
        String grant_type = 'client_credentials';
        HttpRequest request = new HttpRequest();
        request.setHeader('Content-Type', 'application/x-www-form-urlencoded');              
        request.setHeader('Accept', 'application/json');
        String body = 'grant_type=' + grant_type + '&client_id=' + client_id + '&client_secret='+ client_secret ;
        Http http = new Http();
        HttpResponse res = http.send(request);
        String jsonInput = res.getBody();
        Map<String, Object> a =(Map<String, Object>)JSON.deserializeUntyped(jsonInput);
        system.debug('JWT Token is: '+a.get('access_token'));
        return a.get('access_token').ToString();

//ExecuteMail Merge class 

public with sharing class MailMerge {

    public string apiResponse {get; set;}
    public String ExecuteMailMerege(String FileName, String dataFile, SaveFormat outFormat) {
        return ExecuteMailMerege(FileName, dataFile, String.valueOf(outFormat));

    public String ExecuteMailMerege(String FileName, String dataFile, String outFormat) {
     try {
            //build URI to get Image
            String strURl = Product.BaseProductUri + '/words/' + FileName + '/MailMerge?destFileName=MailMergeOut.doc&mailMergeDataFile=' + dataFile;

            String signedURl = Utils.Sign(strURl);
            system.debug('Signed URl =' + signedURl);
            String strJSON = Utils.ProcessCommand(strURl, 'PUT', null, 'json');
            this.apiResponse = strJSON;
            //prepare signed download link
            String downloadURL = null;
          Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(strJSON);
          Map<String, Object> doc = (Map<String, Object>)params.get('Document');
          String DocName = (String)doc.get('FileName');

          strURl = Product.BaseProductUri + '/words/' + DocName + '?format=' + outFormat;

          downloadURL = Utils.Sign(strURl);
          return downloadURL;

        catch (Exception ex) {
            return null;

Product.BaseProductUri = '';

//Function to make api call

public static String ProcessCommand(String strURI, String strHttpCommand, String strContent, String ContentType, String accessToken) {
        try {
            HttpRequest request = new HttpRequest();
            Integer len = 0;
            if (strContent != null) {
                len = strContent.length();
            system.debug('httpcommand: ' + strHttpCommand);
            if (ContentType.toLowerCase() == 'xml')
                request.setHeader('Content-Type', 'application/xml');
                request.setHeader('Content-Type', 'application/json');
            request.setHeader('Content-Length', String.valueOf(len));
            request.setHeader('Accept', 'application/json');
            request.setHeader('Authorization', accessToken);

            Http http = new Http();
            HttpResponse res = http.send(request);
            system.debug('util response:'+res);
            return res.getBody();
        } catch (Exception ex) {
            system.debug('HTTP ERROR' + ex.getMessage());
            return null;