2

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

4
  • I believe this is what you are looking for- salesforce.stackexchange.com/questions/32746/…
    – C0DEPirate
    May 6, 2016 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, 2016 at 7:09
  • but unable to do for alert
    – sudh
    May 6, 2016 at 7:10
  • Please share your code
    – ajinkyah
    May 6, 2016 at 7:12

1 Answer 1

3

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:

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

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

function errorHandler() {
    console.log($Label.MyError);
    alert($Label.MyMessage);
}
2
  • 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, 2016 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, 2016 at 7:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .