Apurv's Dynamics 365 Blog

Code Samples & Troubleshooting Tips on Dynamics 365!

Jscripts snippets for CRM 2011

I have tried to outline couple of examples that you could use in CRM. These examples are based on Xrm patterns which tells you how you can utilize the resources available in better way. These are newly introduced in CRM 2011. The whole idea is to put together a set of common functions as an library which can be used across the forms. You just need to register them under one common place called web resources and keep attaching them to forms where you need to use these. I will also demonstrate how you can achieve that. Let’s get to the list and see what we have in store for today 🙂

  • Server URL.
Xrm.Page.context.getServerUrl()
  • Get the string value of a text field
function GetMainPhone() {
   var MainPhone = Xrm.Page.data.entity.attributes
     .get(“telephone1”).getValue();
}
  • Set the value of a string field
function SetMainPhone() {
  Xrm.Page.data.entity.attributes
     .get(“telephone1”).setValue();
}
  • Adding value to option set
var optionsetbox = Xrm.Page.getControl(“customertypecode”)
optionsetbox.text= “Apurv”;
optionsetbox.value= “10”;
Xrm.Page.ui.controls.get(“customertypecode”).addOption(optionsetbox,12);
  • Removing a value from option set on fly
Xrm.Page.getControl(“customertypecode”).removeOption(4);

I’m still working on more user friendly examples. Please do wait for more. Keep surfing and visiting.

Cheers,
Apurv