Skip to content

Instantly share code, notes, and snippets.

@redryan
Created March 10, 2016 23:03
Show Gist options
  • Save redryan/1c53507a963868601f7b to your computer and use it in GitHub Desktop.
Save redryan/1c53507a963868601f7b to your computer and use it in GitHub Desktop.
<apex:page controller="ZipOpportunities" showHeader="true" sidebar="true">
<head>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script type="text/javascript">
var j$ = jQuery.noConflict();
j$(document).ready(function() {
var accountId = '{!accountId}'; // Get Account Id from the controller
if(!accountId) {
j$('#message').text('Error: This page must be used in combination with a specific Account. Please navigate to an Account and click the Create Opportunities ZIP button to use this page.');
return;
}
var zipFile = new JSZip(); // Instantiate a new zip file
var zipFilename = '{!zipFilename}'; // Get filename from the controller
var count = 0; // File count
// Call controller function to get a list of XML files
// Response is an object with filename as key and XML body as value
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.ZipOpportunities.getXMLFiles}',
accountId,
function(result, event) {
if(event.status) {
// Loop through files and add each one to the zip
// One result contains the Schedule Ids for all Schedule records that were used
for(var xmlFilename in result) {
count++;
compressFile(zipFile, xmlFilename, result[xmlFilename]);
}
if(count > 0) {
saveZip(zipFile);
} else {
j$('#message').html('No XML files were generated. Please make sure your Account is setup correctly with at least one Opportunity record. <a href="/' + accountId + '">Click here</a> to return to the Account.');
}
} else if(event.type === 'exception') {
j$('#message').html('An error occurred and the XML files were not able to be generated. <a href="/' + accountId + '">Click here</a> to return to the Account.<br/>Error message: ' + event.message);
} else {
j$('#message').html('An error occurred and the XML files were not able to be generated. <a href="/' + accountId + '">Click here</a> to return to the Account.<br/>Error message: ' + event.message);
}
}
);
// Compress a file and add it to the zip
function compressFile(zipFile, filename, body) {
zipFile.file(filename, body, {base64: true});
}
// Generate the zip file and send it back to Salesforce for attaching
function saveZip(zipFile) {
var data = zipFile.generate();
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.ZipOpportunities.saveData}',
accountId,
zipFilename,
data,
function(result, event) {
if(event.status) {
if(result == 'SUCCESS') {
j$('#message').html('Success!');
alert('The XML files were successfully generated and a new zip file has been attached to the Account. You will now be redirected back to the Account.');
window.location = '/' + accountId;
} else {
j$('#message').html('An error occurred and the XML files were not able to be generated. <a href="/' + accountId + '">Click here</a> to return to the Account.<br/>Error message: ' + result);
}
} else if(event.type === 'exception') {
j$('#message').html('An error occurred and the XML files were not able to be generated. <a href="/' + accountId + '">Click here</a> to return to the Account.<br/>Error message: ' + event.message);
} else {
j$('#message').html('An error occurred and the XML files were not able to be generated. <a href="/' + accountId + '">Click here</a> to return to the Account.<br/>Error message: ' + event.message);
}
}
);
}
});
</script>
</head>
<body>
<br/>
<h2 id="message"></h2>
<br/>
</body>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment