How to add custom label in static resource JS alert , i want to create a alert error message using a custom label in static resources

  • I believe this is what you are looking for- salesforce.stackexchange.com/questions/32746/…
    – C0DEPirate
    May 6 '16 at 7:03
  • Custom label is not stored in static resources. Custom label is a different metadata type which can be accessed in Apex/Visualforce/Javascript directly.
    – ajinkyah
    May 6 '16 at 7:09
  • but unable to do for alert
    – sudh
    May 6 '16 at 7:10
  • Please share your code
    – ajinkyah
    May 6 '16 at 7:12

If we need to access labels in JS file which is saved in static resource: There are two way to do the same requiremnt:

  1. Pass label as a parameter or pass-through attributes to JS function.

  2. JavaScript bridging component:

2.a) loads before your script, like so:

    window.$Label = window.$Label || {};
    $Label.MyError = '{!JSENCODE($Label.MyError)}';
    $Label.MyPrompt = '{!JSENCODE($Label.MyPrompt)}';
    $Label.MyMessage = '{!JSENCODE($Label.MyMessage)}';

2.b) Now in your javascript (in a static resource) use the variables just as if they were VF without {!}

function errorHandler() {
  • filesToUpload.length = 0; console.log('inside'); _fadeOutFileUploadPopUp(); $j("#closeButton").click; alert($Label.Easy_Archive_Storage_Space_Error); } } else { console.log(event.status);
    – sudh
    May 6 '16 at 7:15
  • @sudh When you add javascript in static resource you cannot access Custom Labels directly. As in the answer, you need to first store it in variables/constants and then JS in static resource can access these variables/constants.
    – ajinkyah
    May 6 '16 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.