Activity Forums Salesforce® Discussions How to change label on custom button based on value in the record being displayed?

  • Gourav

    Member
    August 31, 2016 at 12:52 pm

    This can be pulled off, but you need to get crafty here’s one I prepared with pink candy polka dots:

    1. go to Setup > Create > Objects > [Your Object] > Custom Buttons and Links, and hit New,
    2. create a Detail Page Button whose behaviour is to Execute JavaScript,
    3. modify the following JavaScript, using your lowercased button name in lieu of your_button:
      (function() {
      var button = document.getElementsByName(‘your_button’)[0];
      button.style.background = ‘url(“https://addons.mozilla.org/_files/311357/preview_small.jpg”)’;
      }());
    4. use a base64 encoder to format your JavaScript, which we’ll load and execute as a Data URI,
    5. paste it into the formula, noting the data:application/javascript;base64, prefix:
      {!REQUIRESCRIPT(“data:application/javascript;base64,KGZ1bmN0aW9…wZyIpJzsKfSgpKTs=”)}
      window.location = ‘/apex/ns__PageName’; //button action when clicked

    The usual caveat applies: using a Data URI here may be subject to future validation, but rest assured for now Salesforce support the use of REQUIRESCRIPT in a Custom Button. Your use is only cosmetic.

    Further, if you wanted to separate the button click outcome from the pink candy polka dots, you could put the cosmetic JavaScript in a separate Custom Link which you place somewhere on the page layout. It’s one novel way of running JavaScript in the scope of a standard page without needing to configure the sidebar or use a narrow Home Page Component.

     

    Use this approach and made changes accordingly:-

    Applying the same trick find the name of the button and the ID of the field that you want to copy over to the button and just replace the name = and j$('#') part in the below jquery 🙂

    var j$ = jQuery.noConflict();
    j$(document).ready(function(){
    //newb = name of the button and #acc2_ileiner = id of the custom field
    j$('[name="newb"]').val( j$('#acc2_ileinner').text().replace('[View Hierarchy]', ''));
    });

    I encoded this to base64 and pasted it in my button code:

    Note: To use this hack, create a custom detail page button and choose 'Execute JavaScript' for Behavior and 'OnClick JavaScript' for Content Source. In the formula editor for your new button we first need to load some jQuery resources with the following REQUIRESCRIPT lines

    Refrence: http://blog.scottmcclung.com/2014/01/hack-together-jquery-modal-popup-in.html

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos