Another Trick for Grabbing Key Values in Client Side code

Hints, Tips and Tricks

Technical Hints Tips and Tricks that cover customization and development using Sage CRM. API usage and coding are covered.

Another Trick for Grabbing Key Values in Client Side code

  • Comments 4
  • Likes
The code in article has been corrected, 7th May 2009.
 
An individual screen within CRM will be produced as a result of a hyperlink that looks like this:

http://servername/crm/eware.dll/Do?SID=5351668533575&Act=166&Mode=1&CLk=T&Key0=4&Key4=4&Key27=80&T=New

I wrote a little while ago about the function GetKeys() that is added automatically into the system built screens.
https://community.sagecrm.com/blogs/hints_tips_and_tricks/archive/2009/01/05/grabbing-key-values-client-side.aspx

GetKeys will return the keys used to set the context in a handy to use string like this:
&Key0=1&Key1=28&Key2=30&Key4=1

But in some circumstances you may not want to get hold of the whole context string but you may want to get hold of an individual variable that has been passed in the querystring. For example you may just want the SID.

The function below allows you to get the value of an individual variables and can be added to the custom content.

<script>
function GetKeyValue(querystringname)
{
var strPath = window.location.search.substring(1);
var arrayKeys = strPath.split("&");
for (var i=0;i<arrayKeys.length;i++)
{
var arrayValue = arrayKeys[i].split("=");
if (arrayValue[0].toLowerCase()== querystringname.toLowerCase())
{
return arrayValue[1];
}
}
return "";
}

window.alert(GetKeyValue("xxxxx"));
</script>
Comments
  • Using a regular expression makes it much more concise:

    function QryStr(key){

     var m=window.location.search.match(new RegExp("([?&]" + key + "=)([^&]*)", "i"));

     return (m)?m[2]:"";

    }

    alert(QryStr("SID"));

    Paul

  • That is very nice usage of a regular expression.  I try and avoid using them to make the code as accessible as possible.  I think if I used regular expressions it would make the code too dense and unfriendly to people not used to coding all the time.

  • Hi, is there an easy way to get the CRM instance name from the querystring?

  • In Sage CRM 7.2 you can use

    crm.infoMessage(crm.installName());

    To return the instance/install name.