Free Support Forum - aspose.cloud

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 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;
            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 = Utils.Sign(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;
        }
    }