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

Hi,

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.

@franckg

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

@franckg

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.setEndpoint('https://api.aspose.cloud/connect/token');
        request.setMethod('GET');                
        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 ;
        request.setBody(body);
        
        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 Mailmerge using data from cloud storage
            String strURl = Product.BaseProductUri + '/words/' + FileName + '/MailMerge?destFileName=MailMergeOut.doc&mailMergeDataFile=' + dataFile;
            //build URI to Mailmerge data from string (json/xml)
            //String strURl = Product.BaseProductUri + '/words/' + FileName + '/MailMerge?destFileName=MailMergeOut.doc';

            //Mailmerge from data file
            String strJSON = Utils.ProcessCommand(strURl, 'PUT', null, 'json');
            //Mailmerge from HTML/Rich Text String
            //XML  
         	//String htmlData='<?xml version="1.0" encoding="utf-8" ?><root><data format="html">&lt;html&gt;&lt;head&gt;&lt;style type=&quot;text/css&quot;&gt;h2{color:green} .color_red{color:red} .color_green{color:green} .color_magenta{color:magenta} .color_olive{color:olive} .color_teal{color:teal}&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Table&lt;/h2&gt;&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;4&quot;&gt;&lt;tr&gt;&lt;th class=&quot;color_red&quot;&gt;Column 1&lt;/th&gt;&lt;th class=&quot;color_green&quot;&gt;Column 2&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;color_magenta&quot;&gt;Value 1.1&lt;/td&gt;&lt;td class=&quot;color_olive&quot;&gt;Value 1.2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;color_teal&quot;&gt;Value 2.1&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;h2&gt;Div&lt;/h2&gt;&lt;div class=&quot;color_olive&quot;&gt;Outer text&lt;div class=&quot;color_magenta&quot;&gt;Inner text&lt;/div&gt;Outer text&lt;/div&gt;&lt;h2&gt;Image&lt;/h2&gt;&lt;br/&gt;&lt;img src=&quot;data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsTAAALEwEAmpwYAAADUUlEQVR42lWTWUwTURSGaUtIKY5QweISTUzceDGu0Rj1xURjTNQEE+ODGn0hPhBEQdqZztahBbrAtFih1Fprh9IBiVLLErZSU7BUoS7VqNH44ovx0S0ovdfO4Ez04cw9Ofec7/5zzr15NEnkUxSVFx8ObZ1x7nzHN6z9nOhnzOFwWCnECcKoEFbBRrrp6iF/XRvD0BoplkdTuAiIhtt3P3Fu+jJlKlkM1WoXLFjVeSnpXrC94gV3ciJp1X0L1K7+NtDfvXdpj1TmmUwm8QTfLa867T+ceWTSwO5r5b87XNaDEoBrv3p2Ai+AU1Qh5OiDGS4UXi4roAisQHBiYfJS2obAVPMyELYcGxViOIaKcLaNLXzadej5nBUBPXW6hdYW0z4RQOYUXK2tUfr8QeXL0Kn4JKEBk7QWdtrrLggJJEmoCBxVCf50qIaaa0HABFkEPZaLNnGfwPNFGV0u05Y554bvMQoBPLbxs9t9QydJpHP/KayRbsf+pG1NdgTTgDuGXa8dbS41ZtArxaQYT1WlHVqYaloGOcvJIan4X2MdViTl3vExbS+GPfXlvzzutj1LPch9HrYea59t0YIRtAAO+PQuIWYwNKikYty41Iuk/3TvMzsCB9HcQR7msgzgjZs7Z5rLQPR6PojeJW2d3oDCoG/IlwAYqlexbp/i9eB19ziuBoMoAlrR03YZ0G8+0JFhS8EjuhD6zWfuCTEjppcBOKYX1bwaqA5MM2oYZ4pBy7XjrAyY7sPqU01FcNZW/jtwZeXXVpt5u1RM4JhYzHFBddqz5/28fUU2NwngtVUbZUCE79o6x67/MYiXLiSbS8D9xp1vI7znkLvDK07A5/MiT7mzdxMMAsdMqxaiWDHkgzePigBUXy9KTfdWeVKNSjjPrvv5uKkYxszlIHX7eDLeVfkgbt30bt5eAlOOtT9nLRrI03szrOumOtcb4SrTSj2KKfr4nuUZ7kR8HFPAYXLlYqxRl43TGjjTWAhHCC1MNK/+lWA0cJLRgfsh95G/N1H136z7+BCSuHMuMEGVLo4a1XCKKcuO0WXZBw1FcNighmPWbe8jIeeJpdFiCrkHguWUyM92NBKseBut8T92bfsw66z49IyrHH2T5CvlqeCYUvL/ANIZ7+ohXSdWAAAAAElFTkSuQmCC&quot;&gt;&lt;/body&gt;&lt;/html&gt;</data></root>';
         	//Json
         	//String htmlData='{"root":{"data":{"format":"html","htmlText":"&lt;html&gt;&lt;head&gt;&lt;style type=&quot;text/css&quot;&gt;h2{color:green} .color_red{color:red} .color_green{color:green} .color_magenta{color:magenta} .color_olive{color:olive} .color_teal{color:teal}&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Table&lt;/h2&gt;&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;4&quot;&gt;&lt;tr&gt;&lt;th class=&quot;color_red&quot;&gt;Column 1&lt;/th&gt;&lt;th class=&quot;color_green&quot;&gt;Column 2&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;color_magenta&quot;&gt;Value 1.1&lt;/td&gt;&lt;td class=&quot;color_olive&quot;&gt;Value 1.2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;color_teal&quot;&gt;Value 2.1&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;h2&gt;Div&lt;/h2&gt;&lt;div class=&quot;color_olive&quot;&gt;Outer text&lt;div class=&quot;color_magenta&quot;&gt;Inner text&lt;/div&gt;Outer text&lt;/div&gt;&lt;h2&gt;Image&lt;/h2&gt;&lt;br/&gt;&lt;img src=&quot;data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsTAAALEwEAmpwYAAADUUlEQVR42lWTWUwTURSGaUtIKY5QweISTUzceDGu0Rj1xURjTNQEE+ODGn0hPhBEQdqZztahBbrAtFih1Fprh9IBiVLLErZSU7BUoS7VqNH44ovx0S0ovdfO4Ez04cw9Ofec7/5zzr15NEnkUxSVFx8ObZ1x7nzHN6z9nOhnzOFwWCnECcKoEFbBRrrp6iF/XRvD0BoplkdTuAiIhtt3P3Fu+jJlKlkM1WoXLFjVeSnpXrC94gV3ciJp1X0L1K7+NtDfvXdpj1TmmUwm8QTfLa867T+ceWTSwO5r5b87XNaDEoBrv3p2Ai+AU1Qh5OiDGS4UXi4roAisQHBiYfJS2obAVPMyELYcGxViOIaKcLaNLXzadej5nBUBPXW6hdYW0z4RQOYUXK2tUfr8QeXL0Kn4JKEBk7QWdtrrLggJJEmoCBxVCf50qIaaa0HABFkEPZaLNnGfwPNFGV0u05Y554bvMQoBPLbxs9t9QydJpHP/KayRbsf+pG1NdgTTgDuGXa8dbS41ZtArxaQYT1WlHVqYaloGOcvJIan4X2MdViTl3vExbS+GPfXlvzzutj1LPch9HrYea59t0YIRtAAO+PQuIWYwNKikYty41Iuk/3TvMzsCB9HcQR7msgzgjZs7Z5rLQPR6PojeJW2d3oDCoG/IlwAYqlexbp/i9eB19ziuBoMoAlrR03YZ0G8+0JFhS8EjuhD6zWfuCTEjppcBOKYX1bwaqA5MM2oYZ4pBy7XjrAyY7sPqU01FcNZW/jtwZeXXVpt5u1RM4JhYzHFBddqz5/28fUU2NwngtVUbZUCE79o6x67/MYiXLiSbS8D9xp1vI7znkLvDK07A5/MiT7mzdxMMAsdMqxaiWDHkgzePigBUXy9KTfdWeVKNSjjPrvv5uKkYxszlIHX7eDLeVfkgbt30bt5eAlOOtT9nLRrI03szrOumOtcb4SrTSj2KKfr4nuUZ7kR8HFPAYXLlYqxRl43TGjjTWAhHCC1MNK/+lWA0cJLRgfsh95G/N1H136z7+BCSuHMuMEGVLo4a1XCKKcuO0WXZBw1FcNighmPWbe8jIeeJpdFiCrkHguWUyM92NBKseBut8T92bfsw66z49IyrHH2T5CvlqeCYUvL/ANIZ7+ohXSdWAAAAAElFTkSuQmCC&quot;&gt;&lt;/body&gt;&lt;/html&gt;"}}}';
            //String strJSON = Utils.ProcessCommand(strURl, 'PUT', htmlData, 'json');
            this.apiResponse = strJSON;
            system.debug(strJSON);
            //prepare signed download link
            String downloadURL = null;
            
          Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(strJSON);
          if(params.containsKey('Document')){
          Map<String, Object> doc = (Map<String, Object>)params.get('Document');
          String DocName = (String)doc.get('FileName');

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

          downloadURL = strURl;
          }
          return downloadURL;

        }
        
        catch (Exception ex) {
            system.debug(ex);
            return null;
        }   
     
     
    }   
}

Product.BaseProductUri = 'https://api.aspose.cloud/v4.0';


//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) {
                request.setBody(strContent);
                len = strContent.length();
            }
            system.debug('httpcommand: ' + strHttpCommand);
            request.setEndpoint(strURI);
            request.setMethod(strHttpCommand);
            
            if (ContentType.toLowerCase() == 'xml')
                request.setHeader('Content-Type', 'application/xml');
            else
                request.setHeader('Content-Type', 'application/json');
            request.setHeader('Content-Length', String.valueOf(len));
            request.setHeader('Accept', 'application/json');
            request.setHeader('Authorization', accessToken);
            request.setTimeout(120000);

            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());
            system.debug(ex.getStackTraceString());
            return null;
        }
    }