Custom Action Name: Batch_Add_Case_Comment

Objects affected: Case and Case Comment

Description: This custom action adds a case comment from the Case object. Use this on the Case object level. You can modify the fields that are set on the Case Comment by adding and removing fields from the BATCHADD_CASECOMMENT field set on the Case object.

Use Cases: Batch add the same comment to a group of selected cases to track the status of a case or to record new information on a group of cases.

Steps: Follow these steps to create the custom action:

  1. Click Conga Grid Setup.
  2. Select the Cases tab.
  3. Select the Features tab on the Cases object.
  4. Create a field set on the Case object with a name of BATCHADD_CASECOMMENT
  5. Add the Case's "Description" field to that field set
  6. Click New Custom Action. The Custom Action screen appears.
  7. Delete the sample code from the Javascript field.
  8. Copy and paste the following code into the Javascript field.

    // Load in a CRMC library for entry window (note these are undocumented and unsupported for now)
    crmc.require(["KendoEntry", "KendoPopup", "ListButton"], function(prompt, popup, navigate) {
    
    /**
       * @author CRMCulture
       * @description Basic batch create example
    */
    
    /**
       * "ITEM_ID" Is the ID that uniquely identifies our Action item. 
       * "CONTEXT_OBJECT" Is the context object that your action is based on. 
       * "CREATE_OBJECT" Is the name of the object that you are batch creating and/or updating. 
       * "SINGULAR_NAME" Is the singular label text of CREATE_OBJECT. 
       * "PLURAL_NAME" Is the plural version of the SINGULAR_NAME.
    */
    var ITEM_ID = "Batch_Add_Case_Comment";
    var CONTEXT_OBJECT = "Case";
    var CREATE_OBJECT = "CaseComment";
    var SINGULAR_NAME = "Case Comment";
    var PLURAL_NAME = "Case Comments";
    
    /**
       * "FIELD_SET_NAME" Is the name of a field set based on the object that you are creating. 
          This is the template of fields used when creating the modal form window.
       * "DEFAULT_FORM_VALUES" Is a list of Field Names and values used to populate the modal form fields. 
          Each line item must be in the format of "Field Name": "Value" (include comma if not only or last item).
    */
    var FIELD_SET_NAME = "BATCHADD_CASECOMMENT";
    var DEFAULT_FORM_VALUES = {
    
    }
    
    /** Language strings.*/
    var TEXT_BATCH_ADD_OBJECT = "Batch Add" + PLURAL_NAME;
    var TEXT_ERROR = "There was an error: ";
    var TEXT_MESSAGE = "Added {0} " + PLURAL_NAME + ", would you like to ActionGrid the results?";
    var TEXT_BATCH_ADD_RESULTS = "Batch Add Results";
    var TEXT_YES = "Yes";
    var TEXT_NO = "No";
    
    /** 
       * "SET_CUSTOM_RECORD_VALUES" Is a function that sets the specified values "Under the hood" rather than by a form. Please note that you do not have access to fields that are not currently loaded into the grid. 
       * @row
       * @record
    */
    var SET_CUSTOM_RECORD_VALUES = function(SELECTED_ROW, NEW_RECORD){
        /**Required, set the Primary ID of each row to the Foreign Key relationship.*/
        NEW_RECORD.ParentId = SELECTED_ROW.Id;
    
    }
    
    //////////////////////////////////////
    /**
       * Custom action core code below.
    */
    //////////////////////////////////////
    
      // Define a custom action for batch adding
      crmc.addCustomAction({
        // Uniquely identifies our Action item
        "itemID": ITEM_ID,
        // Indicates this item should appear under batch add menu
        "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 isAccessible = context.objectDescribe.name == CONTEXT_OBJECT; 
          var multipleSelected = context.selectedRows && context.selectedRows.length > 0; 
          return isAccessible && isEnabled && multipleSelected; 
          },
          "getLabel": function (context) {
            // This function returns the display label of the action item and is calld before the item is shown 
            // Note the use of kendo ui library can be used in any actions 
            return kendo.format(PLURAL_NAME + " ({0}) ", context.selectedRows.length);
           },
           "click": function (context) {
             var records = [];
             var row_Ids = [];
             // Show a prompt that contains the fields in FIELDSETNAME
    
             // modified to pass in CONTEXT OBJECT because Case Comments doesn't support Field Sets
             prompt.fieldSetEntry(TEXT_BATCH_ADD_OBJECT, CONTEXT_OBJECT , FIELD_SET_NAME, DEFAULT_FORM_VALUES, function(values) {
               // For every selected row 
               context.selectedRows.map(function(row) {
                   var record = new sforce.SObject(CREATE_OBJECT);
                   SET_CUSTOM_RECORD_VALUES(row, record); 
                   row_Ids.push(row.Id); 
                   // Set values specified in prompt 
                   for (var field in values) { 
                                    // Hardcoding the field name because we're only prompting for a single field but using the  
                                    // Case objects field set so the fields don't match 
                     record.CommentBody = values[field].value; 
                   } 
                   records.push(record);
                }); 
                var onfailure = function(error) {
                  alert(TEXT_ERROR + (error.message || error.faultstring));
                }; 
                // Insert opportunities using sforce ajax toolkit
                sforce.connection.create(records, {
                  onSuccess: function(results) {
                    // Get the new record Ids
                    var recordIds = [];
                    $.each(results, function(i, row) {
                      if (row.errors) {
                        // Handle first error if any 
                        onfailure(row.errors);
                        return; 
                       }
                       recordIds.push(row.id);
                       }); if (results.length == recordIds.length) {
                              // Prompt to navigate the user to the results
                              var message = kendo.format(TEXT_MESSAGE, results.length);
                              var buttons = [{
                                label: TEXT_YES,
                                click: function() {
                                  // Navigate to a list of just these recordIds 
                                  window.open(kendo.format("/apex/CRMC_PP__crmc_grid?object={0}&Ids={1}", CONTEXT_OBJECT, row_Ids.join()));
                                 }
                                },
                                { 
                                 label: TEXT_NO 
                                }];
                                popup.popupWithButtons(TEXT_BATCH_ADD_RESULTS, message, buttons);
                               } 
                              }, 
                              onFailure: onfailure
                            });
                          });
                         }
                        });
                      });
    CODE
  9. Copy the itemID value and paste it as the Action Name.
  10. Click OK.