Skip to main content
Conga Support

Opportunity: Post Billing

The use of JavaScript in Conga solutions is considered an advanced method, requiring JavaScript knowledge, and is not supported by Conga or the Conga support team. You are welcome to use this feature at your own risk.

Follow these steps to create the custom action:

  1. Click ActionGrid Setup.
  2. Select the object for which you want to create the custom action.
  3. Select the object Features tab.
  4. Click New Custom Action. The Custom Action screen appears.
  5. Delete the sample code from the Javascript field.
  6. Copy and paste the following code into the Javascript field.
  7. Copy the itemID value and paste it as the Action Name.
  8. Click OK.
crmc.require(['sfdc', 'KendoPopup', 'KendoEntry'], function (sfdc, popup, entry) {
    crmc.addCustomAction({
        "itemID": "AcctSeed_Opportunity_PostBilling",
        "isBatchAddItem": true,
        "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 isOpportunity = context.objectDescribe.name == "Opportunity";
            var multipleSelected = context.selectedRows && context.selectedRows.length > 0;
            return isOpportunity && isEnabled && multipleSelected;
        },
        "getLabel": function (context) {
          // This function returns the display label of the action item and is called before the item is shown
          return "Create Billing";
        },
        "createSubmenuItems": function (context) {
          // If this function returns additional action item objects, they will appear as submenu items
          return [];
        },
        "click": function (context) {
                var ids = [];
                if(context.selectedRows.length <= 5){
                      var submitUrl = "/apex/acctseed__BillingFromOpportunity?id="
                      context.selectedRows.map(function(row) {
                          ids.push(row.Id);
                      });
                      for (var i = 0; i < ids.length; i++) {
                          window.open(submitUrl + ids[i]);
                      };

                      afterUpdate();
                } else {
                    popup.popup("Record Selection", "Currently, there is only support for 5 max records per batch.");
                }

                function afterUpdate(){

                    // Prompt to navigate the user to the results
                    var buttons = [{
                      label: "Yes",
                      click: function() {
                          var billings = sfdc.query("SELECT Name, Id FROM AcctSeed__Billing__c WHERE AcctSeed__Opportunity__c IN('" + ids.join("','") + "') AND isdeleted = false");
                          form(billings);
                      }   
                    },
                    {
                        label: "No"
                    }];

                    popup.popupWithButtons("Finished reviewing tabs?", "Would you like to choose the Posting Status value?", buttons);
                
                    function form(ids){
                        var objectDescribe = sfdc.getSObjectDescribe("AcctSeed__Billing__c");
                        var picklistValues = [];
                        Object.each(objectDescribe.fields, function(field){
                            if (field.name == "AcctSeed__Status__c"){
                                picklistValues = field.picklistValues;
                            }
                        }
                        );

                        entry.entry("Posting Status",
                            [{name: "AcctSeed__Status__c", label: 'Select Status',
                                type: 'picklist', values: picklistValues,
                             }], 
                             null,
                             null,
                             function(selectedValues) {
                                  var selectedStatus = selectedValues["AcctSeed__Status__c"].value;
                                  update(ids, selectedStatus);
                             });
                    }

                    function update(ids, status){
                        var pass = 0;
                        var fail = 0
                        for (var i = 0; i < ids.length; i++) {
                            //create an account
                            var record = new sforce.SObject("AcctSeed__Billing__c");
                           
                            //update that Billing record.
                            record.id = ids[i].Id;
                            record.AcctSeed__Status__c = status;
                            result = sforce.connection.update([record]);
                           
                            if (result[0].getBoolean("success")) {
                                pass++;
                            } else {
                                fail++;
                            }
                        };
                        viewResults(pass, fail, ids);
                    }

                    function viewResults(pass, fail, ids){
                        // Prompt to navigate the user to the results
                        var message = "" + pass + " have been updated" + ", would you like to ActionGrid the results?"
                        var buttons = [{
                            label: "Yes",
                            click: function() {
                                urlPrams = [];
                                for (var i = 0; i < ids.length; i++) {
                                    urlPrams.push(ids[i].Id);
                                };
                                // Navigate to a list of just these recordIds
                                window.open(kendo.format("/apex/CRMC_PP__crmc_grid?object={0}&Ids={1}", "AcctSeed__Billing__c", urlPrams.join()));
                            }
                        },
                        {
                            label: "No"
                        }];
                        popup.popupWithButtons("Updated Billings", message, buttons);
                    }
                } 
        }
    });
});