Preview:
string button.generate_Sales_offer1(String recordId)
{
// ========== Inputs / Variables ==========
moduleAPI = "Sales_Offers";
// Your module API name
//recordId = "5971686000117622076";
templateName = "Sales Offer";
// Name of the mail merge template defined in CRM
desiredFileName = "Generated Sales Offer";
outputFormat = "pdf";
// ========== Build Request Body ==========
mergeEntry = Map();
tMap = Map();
tMap.put("name",templateName);
mergeEntry.put("mail_merge_template",tMap);
mergeEntry.put("file_name",desiredFileName);
mergeEntry.put("output_format",outputFormat);
wrapper = Map();
mergeList = List();
mergeList.add(mergeEntry);
wrapper.put("download_mail_merge",mergeList);
// ========== Call Download Mail Merge API ==========
download_resp = invokeurl
[
	url :"https://www.zohoapis.com/crm/v8/" + moduleAPI + "/" + recordId + "/actions/download_mail_merge"
	type :POST
	body:wrapper.toString()
	connection:"mail_merge"
];
info download_resp;
download_resp.setParamName("file");
response = invokeurl
[
	url :"https://www.zohoapis.com/crm/v8/files"
	type :POST
	files:download_resp
	connection:"newzohocrm"
];
info response;
if(response.get("data") != null && response.get("data").size() > 0)
{
	fileId = response.get("data").get(0).get("details").get("id");
	// Prepare file upload field value
	fileMap = Map();
	fileMap.put("file_id",fileId);
	fileList = List();
	fileList.add(fileMap);
}
//
///////////////
// ========= Upload file to CRM Attachments ========= //
bodyMap = Map();
attachList = List();
attachList.add({"id":fileId});
bodyMap.put("attachments",attachList);
attachUrl = "https://www.zohoapis.com/crm/v8/" + moduleAPI + "/" + recordId + "/Attachments";
attachResp = invokeurl
[
	url :attachUrl
	type :POST
	files:download_resp
	connection:"newzohocrm"
];
info attachResp;
if(attachResp.get("data").size() > 0)
{
	return "Sales Offer has been generated and attached successfully.";
}
else
{
	return "There was an error while generating or attaching the Sales Offer. Please try again.";
}
//return "";
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter