3

I believe this is more due to my lack of knowledge about Javascript. But since i am working on salesforce, i thought i would post the question in this forum.

There is a label SS_TLIfieldIDs, whose value is

"Contract_Line_Item__c:00NQ0000001MDob;Service_Contract__c:00NQ0000001MF96"

It basically stores field names and their IDs, separated by semicolon, which i intend to use in a custom button for URL hacking.

Here is the code for custom button (onclick Javascript), which goes on a custom object (Tranaction_Line_Item__c)-

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}

var TLIfieldIds=('{!$Label.SS_TLIfieldIDs}').split(';');
var fieldNameToIdMap=new Map();

for(i=0;i<(TLIfieldIds.length);i++)
{
  var currMap=TLIfieldIds[i].split(':');
  var fieldName=currMap[0];
  var fieldId=currMap[1];
  fieldNameToIdMap.set(fieldName,fieldId);
}

As you can see, my goal is to parse that label and store field names and their IDs in a map (which i will use later for URL hacking).

That last line fieldNameToIdMap.set(fieldName,fieldId) is causing an error "Undefined is not a function" when i click on the button. And i have no clue why (I have googled it).

However fieldNameToIdMap[fieldName]=fieldId works fine. Can anyone explain why? Is there anyway i can make this code compact? I am trying bunch of things to make this code concise, but keep running into errors (due to my lack of javascript knowledge)

4
  • Welcome to SFSE. For future reference, there's a handy code formatting button {} which will help you format your question body :)
    – Matt Lacey
    Commented Apr 27, 2015 at 23:20
  • I think your error must be somewhere else, I ran this code in the Firefox JS console and it works fine with the value you've indicated.
    – Matt Lacey
    Commented Apr 27, 2015 at 23:24
  • 1
    @LaceySnr It's not terribly cross-browser compatible, though-- IE doesn't support it until 11, for example. I'd recommend normal objects in this case, or find a Map shim if you wanted.
    – sfdcfox
    Commented Apr 27, 2015 at 23:28
  • Yeah that's entirely fair. I was going to say to use a hash but was genuinely surprised when the map functions worked in FF. Should have guessed it was one of those non-standard areas.
    – Matt Lacey
    Commented Apr 28, 2015 at 1:12

2 Answers 2

2

Map.prototype.set is not completely implemented as a technology in Javascript today. You can read about this in this article about it in MDN.

If you were implementing this as a JS app, you'd probably use a polyfill or a framework to take up the slack between using this and what your browser can support.

In the mean time, the notation you've settled on will work just fine:

fieldNameToIdMap[fieldName]=fieldId
1
  • Thank you LaceySnr and sfdcfox. I will use var fieldNameToIdMap = {} in that case. Commented Apr 27, 2015 at 23:52
2

JavaScript lacks a formal "map" (edit: until the latest standards, anyways), as all objects are naturally associative arrays (hashes). You should be able to get away with just declaring an empty object ("{}"), then assigning the values directly to the object, as follows:

var fieldNameToIdMap = {};
...
fieldNameToIdMap[fieldName] = fieldId;

Looking at Map, you'll see it's not available until IE 11.

You must log in to answer this question.

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