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"><html><head><style type="text/css">h2{color:green} .color_red{color:red} .color_green{color:green} .color_magenta{color:magenta} .color_olive{color:olive} .color_teal{color:teal}</style></head><body><h2>Table</h2><table border="2" cellspacing="0" cellpadding="4"><tr><th class="color_red">Column 1</th><th class="color_green">Column 2</th></tr><tr><td class="color_magenta">Value 1.1</td><td class="color_olive">Value 1.2</td></tr><tr><td class="color_teal">Value 2.1</td></tr></table><h2>Div</h2><div class="color_olive">Outer text<div class="color_magenta">Inner text</div>Outer text</div><h2>Image</h2><br/><img src="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"></body></html></data></root>';
//Json
//String htmlData='{"root":{"data":{"format":"html","htmlText":"<html><head><style type="text/css">h2{color:green} .color_red{color:red} .color_green{color:green} .color_magenta{color:magenta} .color_olive{color:olive} .color_teal{color:teal}</style></head><body><h2>Table</h2><table border="2" cellspacing="0" cellpadding="4"><tr><th class="color_red">Column 1</th><th class="color_green">Column 2</th></tr><tr><td class="color_magenta">Value 1.1</td><td class="color_olive">Value 1.2</td></tr><tr><td class="color_teal">Value 2.1</td></tr></table><h2>Div</h2><div class="color_olive">Outer text<div class="color_magenta">Inner text</div>Outer text</div><h2>Image</h2><br/><img src="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"></body></html>"}}}';
//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;
}
}