[SalesForce] Javascript custom button throwing error – Undefined is not a function

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)

Best Answer

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
Related Topic