crmc.require(['sfdc', 'KendoPopup'], function (sfdc, popup) {
 crmc.addCustomAction({
 "itemID": "UnPost_AP_Batch_From_Account_Payable_Line",
 "isAvailable": function (context) {
 // This function is called before the action item is displayed and returns a boolean if the item should be displayed
 // By default determine availability based on Feature Security for this action
 var isEnabled = this.featureSecurity.getSetting(context.objectDescribe.name, this.itemID) !== false;
 // Only allow batch adding from Accounts object for now
 var isVisible = context.objectDescribe.name == ("AcctSeed__Account_Payable_Line__c");
 var multipleSelected = context.selectedRows && context.selectedRows.length > 0;
 return isVisible && isEnabled && multipleSelected;
 },
 "getLabel": function (context) {
 // This function returns the display label of the action item and is called before the item is shown
 return "UnPost Accounts Payable";
 },
 "createSubmenuItems": function (context) {
 // If this function returns additional action item objects, they will appear as submenu items
 return [];
 },
 "click": function (context) {
 //https://acctseed.cs7.visual.force.com/apex/AccountPayablePayBatch?retURL=%2Fa0H%3Ffcf%3D00BF0000006rihU%26rolodexIndex%3D-1%26page%3D1&wrapMassAction=1&scontrolCaching=1
 // Include static resource from managed namespace pre-creates session and allows us to safely POST data without redirect issue
 var head = document.getElementsByTagName('HEAD').item(0);
 var script = document.createElement("script");
 script.type = "text/javascript";
 head.appendChild(script);
 var domains = window.location.hostname.split(".");
 if (domains.length == 3) {
 instance = domains[0];
 }
 else {
 instance = domains[1];
 }
 script.src = "https://AcctSeed." + instance + ".visual.force.com/resource/AcctSeed__ButtonJSFunctions?ts=" + new Date().getTime();
 script.onerror = function() {
 // Couldn't load app script, assume not installed
 popup.popup("Could not obtain session for Accounting Seed Financial Suite is the app installed?");
 };
 script.onload = function() {
 if (context.selectedRows.length > 0) {
 var selectedIds = [];
 Object.each(context.selectedRows, function (item) {
 var id = item["AcctSeed__Account_Payable__c"];
 if(id === undefined || id === null){
 //Do nothing if empty.
 } else if(!checkIds(id, selectedIds)){
 selectedIds.push({name: "ids", value: id}); 
 }
 });

 if(selectedIds.length > 0) {
 var parameters = {
 retURL: context.actionGrid.settings.pageURL,
 };
 sfdc.postToVFPage("acctseed", "AccountPayableBatchUnpost", parameters, selectedIds);
 } else {
 popup.popup('No IDs to POST', 'Make sure that Accounts Payable is loaded on the grid and that the fields are not empty');
 }
 }
 else {
 popup.popup('Please select records', 'Select at least one record to add to a post to accounts payable.');
 }
 };

 //Does the ID already exist within the array?
 function checkIds(id, arr){
 for (var i = 0; i < arr.length; i++) {
 if(arr[i].value === id){
 return true;
 }
 };
 return false;
 }
 }
 });
});