Pages

Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Sunday, December 6, 2015

Salesforce: Mass Update from List View using JavaScript

Sometimes back, we discussed on how to update Salesforce with JavaScript from a button in a page layout. Next, recently we have issue on how to replicate the same for many records to mass update from list view? Inline Editing in one the option to mass update records from a list view, read this blog for more information on how to use inline editing in list view.

But how about if we always need to mass update with the same data? It can be for only 1 field or for multiple fields, example: update Account type to Customer. Using inline editing may not always be the best option with all the limitations of inline editing.

Solution:
We can use similar approach of using JavaScript to mass update many records from list view.
1. Create custom button with Java Script
2. Add the button to list view

For this blog, we will use scenario to mass update Account type to Customer.

1. Create custom button
  • Navigate to Setup | Customize | AccountsButtons, Links, and Actions
  • Click New Button or Link
  • Enter Label, select Display Type = List Button and enable Display Checkboxes (for Multi-Record Selection), Behavior = Execute JavaScript, Content Source = OnClick JavaScript
  • Copy and paste following script
 {!REQUIRESCRIPT("/soap/ajax/35.0/connection.js")}  
 var records = {!GETRECORDIDS($ObjectType.Account)};   
 var updateRecords = [];   
 if (records[0] == null)   
 {   
  alert("Please select at least one Account")   
 }   
 else   
 {   
  for (var i=0; i<records.length; i++)   
  {   
   var acc = new sforce.SObject("Account");   
   acc.id = records[i];   
   acc.Type = "Customer";   
   updateRecords.push(acc);   
  }   
  result = sforce.connection.update(updateRecords);   
  window.location.href = "/001";   
 }  




2. Add the button to list view
  • Navigate to Setup | Customize | Accounts | Search Layouts
  • Click Edit on Accounts List View
  • Select the custom button created and move it to the right on Selected Buttons
  • Save and done



Note: if the record unable to save for some reasons, maybe validation rule, the error message will NOT display and user may not aware. But, admin can debug this with Debug Log.


This operation will consider as "Api" with Application pointing to the custom button id.



Sunday, April 6, 2014

Salesforce: Selective Mass Close Case

This is continuation from blog Mass Close Case. Using out of the blog feature, user can close all Case selected, but how if user only allowed to close Case only with selected criteria?

You need to create new custom button using JavaScript (although this is also possible using Visualforce page with Apex code). Here we go:

Create custom button 
  1. Go to Setup | Customize | Cases | Search Layouts |  Buttons, Links, and Actions
  2. Click 'New Button or Link' button
  3. Select Display Type = List Button, and check Display Checkboxes (for Multi-Record Selection)
  4. Copy and paste script below:
 {!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}   
   
 var selectedCases = {!GETRECORDIDS($ObjectType.Case)};   
 if(selectedCases.length > 0){   
    var idList = "(";   
    for(var j=0; j<selectedCases.length; j++)   
    idList += "'" + selectedCases[j] + "',";   
    idList = idList.substring(0, idList.length - 1) + ")";   
   
    var searchResult = sforce.connection.query( "SELECT Id " +   
                                     "FROM Case " +   
                                     "WHERE " +   
                                     "( " +   
                                     "Status != 'New' AND " +   
                                     "Id IN " + idList +   
                                     " )"   
                                  );   
   
    if(searchResult.getInt("size") > 0){   
       var varCases = searchResult.getArray("records");   
       var varCasesToUpdate = [];   
       for(var i=0; i<varCases.length; i++){   
          var updatedCase = new sforce.SObject("Case");   
          updatedCase.Id = varCases[i].Id;   
          updatedCase.Status = 'Closed';   
          varCasesToUpdate.push(updatedCase);   
       }   
   
       var updateResult = sforce.connection.update(varCasesToUpdate);   
       location.reload();   
    }   
    else{   
       alert("Selected Case(s) are all Closed!");   
    }   
 }   
 else{   
    alert("Please select at-least one case." );   
 }  


Add button created to Cases List View
  1. Go to Setup | Customize | Cases | Search Layouts |  Cases List View
  2. Click Edit link
  3. Select button in Custom Buttons
  4. Done and Save

Based on the solution above, you can use the same technique for other object with different criteria but similar purpose.


Thursday, December 26, 2013

Update Salesforce using JavaScript AJAX Toolkit

For you who are not from developer background, don't be afraid with the title of this blog, this is not about how to write JavaScript blog. But, using a simple script provided with Ajax Toolkit, you can make use of it for your business needs in Salesforce area.

Use Case:
To change Account Type from Prospect to Customer via a button click, and Customer value itself maybe not a picklist in the account type.

Solution:
Create a custom button in Account object. Here we go:
  1. Navigate to Setup | Customize | Accounts | Buttons, Links, and Actions
  2. Click New Button or Link, select Detail Page Button in Display Type, and in Behavior select Execute JavaScript
  3. Paste script below
  4. Add custom button created to the Account page layout.
  5. Done - simple ?
The script:
 {!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}  
 var AccObj = new sforce.SObject("Account");  
 AccObj.Id = '{!Account.Id}';  
 AccObj.Type = 'Customer';  
 var result = sforce.connection.update([AccObj]);  
 location.reload(true);  




Further tweak, you also can differentiate Customer record type with Prospect record type, so in the script above, add one line to change Account Record Type with Customer Record Type Id, then assign different Page Layout for Customer.

Page-level ad