May 102012
 

Do you need the value from a related entity, like a lookup? I had written this for CRM 4 a while back, and people use it, but I occasionally get questions about looking up other values in CRM. This bit of code will help you find those values on a related object. Below are two options to do this. Option 1: using the REST SDK and JSON2, and then Option 2: using Jaimie Ji’s XRM Service Toolkit.

Jaimie Ji’s XRM Service Toolkit provides a comprehensive set of JScript libraries for interacting with the SOAP and REST SDK through JavaScript. While Option 1 in this particular use case is fairly simple, when you start needing to do more advanced things, you may want to look at using that kit. It is fairly comprehensive and he keeps it up to date.

Option 1: Using the REST SDK and JSON2

Using the REST SDK is great, and there are some really good examples in the CRM SDK help file. Now in order to use the below code-block, you need to use JSON.

JSON, or JavaScript Object Notation, is a text format that is used to interchange data. JSON2.js is a lightweight javascript that convert the strings to javascript objects and vice-versa. The CRM REST SDK can return data in JSON format.

To get the code for JSON2, you can go here. You will need to embed this code into your javascript file or include json2.js along with the code below. Without it, you will not be able to parse the data properly, and the code below uses the JSON library in json2.js.

Ok, below we have 3 functions:

  • getServerUrl : which gets the server URL – taken from the CrmRestKit
  • Lookup_Changed : what you call when a lookup is changed
  • retrieveReqCallBack : the callback function that performs any actions

You would put Lookup_Changed in the change event of a lookup field and specify the attributes like one of the following:

  • ‘contactid’,’Contact’,’contactlookup’
  • ‘contactid’,’Contact’,’contactlookup’,[‘Telephone1′]
  • ‘contactid’,’Contact’,’contactlookup’,[‘Telephone1′,’FullName]

Then you’d put your actions inside the retrieveReqCallBack.

function getServerUrl() {  
    // From CrmRestKit.js
    var localServerUrl = window.location.protocol + "/" + window.location.host;
    var context = parent.Xrm.Page.context;

    if (context.isOutlookClient() && !context.isOutlookOnline()) {
        return localServerUrl;
    }
    else {
        var crmServerUrl = context.getServerUrl();
        crmServerUrl = crmServerUrl.replace(/^(http|https):\/\/([_a-zA-Z0-9\-\.]+)(:([0-9]{1,5}))?/, localServerUrl);
        crmServerUrl = crmServerUrl.replace(/\/$/, "");
    }
    return crmServerUrl;
}

function Lookup_Changed(attributeName, entityName, callbackId, columns) {
    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
    
    var lookup = Xrm.Page.data.entity.attributes.get(attributeName).getValue();
    if (lookup===null) {
        return false;
    }
  
    var id = lookup[0].id;
    if (id == null) { 
        return false;
    }
    
    var retrieveReq = new XMLHttpRequest();
    
    var url = ODataPath + "/"+entityName+"Set(guid'" + id + "')";
    if (columns !== undefined && columns !== null) {
        url = url + "?$select=" + columns.join(',');
    }
    
    retrieveReq.open("GET", url, true);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    retrieveReq.onreadystatechange = function () {
        if (this.readyState == 4 /* complete */) {
            if (this.status == 200) {
                var data;
                var jData;
                jData = JSON.parse(this.responseText);
                if (jData && jData.d && jData.d.results && jData.d.results.length > 0) {
                    data = jData.d.results[0];
                } else if (jData && jData.d) {
                    data = jData.d;
                }
                
                if (data == null) {
                    return;
                }
                retrieveReqCallBack(callbackId, data);
            }
        }
    };
    retrieveReq.send();
    return true;
}

function retrieveReqCallBack(calltype, data) {
    var toLookup=function(data) {
        if (data==null || data=="" || (data.Id==null)) {
            return null;
        }
    
        var result=new Array();
        result[0] = {};
        result[0].id = data.Id;
        result[0].name = data.Name;
        result[0].entityType = data.LogicalName;
        return result;
    }

    switch (calltype) {
        case 'pricelookup':
            Xrm.Page.data.entity.attributes.get("new_pricecategorylookup").setValue(toLookup(data.new_pricecategorylookup);
            break;
        case 'contactlookup':
            Xrm.Page.data.entity.attributes.get("insp_poctelephonenumber").setValue(data.Telephone1);
            break;
        default:
            break;
    }
}

Option 2: Using the XrmServiceToolkit with similar functions

Ok, now let’s assume you are using the XRM Service Toolkit, and you want to do the same thing, using a Lookup_Changed function and the retrieveReqCallBack function. Instead of embedding JSON and a method to get the server url, you can use the following:

function Lookup_Changed(attributeName, entityName, callbackId, columns) {
    var lookup = Xrm.Page.data.entity.attributes.get(attributeName).getValue();
    if (lookup===null) {
        return false;
    }
  
    if (lookup[0].id == null) { 
        return false;
    }
	
	XrmServiceToolkit.Rest.Retrieve(
        lookup[0].id,
        entityName,
        columns,
		null,
        function (result) {
            retrieveReqCallBack(callbackId, result);
        },
        function (error) {
            throw error;
        },
		true
    );
}

function retrieveReqCallBack(calltype, data) {
    var toLookup=function(data) {
        if (data==null || data=="" || (data.Id==null)) {
            return null;
        }
    
        var result=new Array();
        result[0] = {};
        result[0].id = data.Id;
        result[0].name = data.Name;
        result[0].entityType = data.LogicalName;
        return result;
    }

    switch (calltype) {
        case 'pricelookup':
            Xrm.Page.data.entity.attributes.get("new_pricecategorylookup").setValue(toLookup(data.new_pricecategorylookup);
            break;
        case 'contactlookup':
            Xrm.Page.data.entity.attributes.get("insp_poctelephonenumber").setValue(data.Telephone1);
            break;
        default:
            break;
    }
}

The XRM Service Toolkit does a lot, and I would suggest checking it out.

Nov 012011
 

Recently I’ve received some questions on how to change the optionset (previously called picklist) values in CRM 2011 the same way I did in my previous CRM 4 post about using the OnChange event of a yes/no field to change the values of a picklist.

This technet page shows the methods available for the different types of controls.

Here is an example of how to change the optionset based on a yes/no field:

function BuildOptionSet(picklist, valuelist)
{
	picklist.clearOptions();
	for(var i=0; i<valuelist.length; i++)
	{
		var listitem = valuelist[i];
		picklist.addOption(listitem[0], listitem[1]);
	}
}

var lista = new Array(); 
lista[0] = new Array('Alpha',0);
lista[1] = new Array('Beta',1);

var listb = new Array(); 
listb[0] = new Array('Charlie',2);
listb[1] = new Array('Delta',3);

var uselista = Xrm.Page.data.entity.attributes.get("YesNo").getValue();
var optionset = Xrm.Page.ui.controls.get("OptionSet");

if (uselista==true) {
   BuildOptionSet(optionset, lista);
} else {
   BuildOptionSet(optionset, listb);
}

You just need to change the values of lista and listb and the YesNo and OptionSet lines to reflect the fields on your form, and add it to the onchange event of the dropdown Yes/No form field.

Feb 242011
 

Often times we need to toggle the visiblity of an element based on the value of a field. This JScript file adds the methods to accomplish this by calling the setVisibility function. For example, in an onChange event for new_mypicklist I can call:

setVisibility('section','new_mypicklist','section_main',42);

This hides the section_main section if the value is not 42, which simplifies the process of hiding and showing relevant information.

I also like to use checkboxes to handle the visibility of elements, but checkbox values dont’ change until they lose focus. To work around the issue, I register an onClick event during the form load instead of using the onChange event. This unsupported method allows me to modify the value onClick and change the visibility immediately. To use this I just need to call it in the onload:

registerToggle('section', 'new_checkbox', 'section_main');

If you wanted to hide it when new_checkbox was false (or no), then you would need to specify the value, 0.

registerToggle('section', 'new_checkbox', 'section_main',0);

JScript: visibility.js

This is the main javascript file… visibility.js that you would need to add and reference as a web resource in CRM 2011, everything except for the registerToggle uses supported methods.

function registerToggle(t, attr, c, v) {
    if (typeof (v) === "undefined" || v === null) {
        var v = 1;
    }
    var ctrl = Xrm.Page.ui.controls.get(attr);
    var a = ctrl.getAttribute();
    var el = document.getElementById(attr);

    // Build Toggle Function
    var f = "var ef=function() { " +
              "var a = Xrm.Page.data.entity.attributes.get(attr); " +
              "a.setValue(!a.getValue()); " +
              "setVisibility('" + t + "','" + attr + "','" + c + "'," + v + ");" +
              " };";

    eval(f);

    // Attach to click event
    el.attachEvent('onclick', ef, false);

    // Set visibility
    setVisibility(t, attr, c, v);
}

function setVisibility(t, attr, c, v) {
    switch (t.toLowerCase().charAt(0)) {
        //tab
        case 't': setTabVisibility(attr, c, v);
            break;
        //section
        case 's': setSectionVisibility(attr, c, v);
            break;
        //control
        case 'c': setControlVisibility(attr, c, v);
            break;
        //navigation
        case 'n': setNavigationVisibility(attr, c, v);
            break;
    }
}

function setNavigationVisibility(attributename, navitemname, value) {
    var attribute = Xrm.Page.data.entity.attributes.get(attributename);
    var navitem = Xrm.Page.ui.navigation.items.get(navitemname);
    if (navitem === null)
    {
        return;
    }
    navitem.setVisible(attribute.getValue() == value);
}

function setTabVisibility(attributename, tabname, value) {
    var attribute = Xrm.Page.data.entity.attributes.get(attributename);
    var tab = Xrm.Page.ui.tabs.get(tabname);
    if (tab === null)
    {
        return;
    }
    tab.setVisible(attribute.getValue() == value);
}

function setSectionVisibility(attributename, sectionname, value) {
    var attribute = Xrm.Page.data.entity.attributes.get(attributename);
    var tabs = Xrm.Page.ui.tabs.get();
    for(var i in tabs) {
        var tab = tabs[i];
        var section = tab.sections.get(sectionname);
        if (section !== null) {
            section.setVisible(attribute.getValue() == value);
            return;
        }
    }
}

function setControlVisibility(attributename, controlname, value) {
    var attribute = Xrm.Page.data.entity.attributes.get(attributename);
    var control = Xrm.Page.ui.controls.get(controlname);
    if (control === null) 
    {
        return;
    }
    control.setVisible(attribute.getValue() == value);
}
Oct 132009
 

I use a lot of the same function on multiple pages, and I think a lot of you do to. You can create a common JScript file and load it during an onload event, extending the functionality of your CRM. This is particularly useful because it reduces the amount of changes you have to make when you want to change how something works.

Code reuse is important, and I believe in creating libraries and using libraries. Javascript files are libraries, and they are cached. So if you update a Javascript file you are loading this way, make sure your users clear their cache.

function loadjs(filename, onload){
  var scriptfile=document.createElement('script');
  scriptfile.setAttribute("type","text/javascript");
  scriptfile.setAttribute("src", filename);
  scriptfile.onreadystatechange=onload;
  document.getElementsByTagName("head")[0].appendChild(scriptfile);
}

loadjs("/MoreJavascript/tools.js", function(){
	if (this.readyState == 'loaded' || this.readyState == 'complete') {
		FunctionInJavascriptFile();
		this.onreadystatechanged=null;
	}
});

CRM 2011 uses web-resources which can be a common JScript file.

Oct 032009
 

This article was written for CRM 4. If you are interested in seeing how this would be done in CRM 2011, please click here.

Recently on the Microsoft forums someone asked how to change the values of a picklist based on the yes no value of a boolean field. Just add this script to the OnChange event of the yes/no field.

This technet page shows some of the functions on different form fields.

Here is an example of how to change the picklist based on a yes/no field:

function CreatePicklistValue(Name,Value)
{
	var retval = new Array();
	retval[0]  = Name;
	retval[1]  = Value;
	return retval;
}

function BuildPickList(picklist, valuelist)
{
	var options = picklist.Options;
	for(var i=0; i<options.length; i++)
	{
		var option = options[i];
		picklist.DeleteOption(option.DataValue);
	}
	for(var j=0; j<valuelist.length; j++)
	{
		var listitem = valuelist[j];
		picklist.AddOption(listitem[0], listitem[1]);
	}
}

var lista = new Array(); 
lista[0] = CreatePicklistValue('Alpha',0);
lista[1] = CreatePicklistValue('Beta',1);

var listb = new Array(); 
listb[0] = CreatePicklistValue('Charlie',2);
listb[1] = CreatePicklistValue('Delta',3);

var uselista = crmForm.all.YesNo.DataValue;
var picklist = crmForm.all.Picklist;

if (uselista==true) {
   BuildPickList(picklist, lista);
} else {
   BuildPickList(picklist, listb);
}

You just need to change the values of lista and listb, and the crmForm.all.YesNo and crmForm.all.Picklist.

Oct 012009
 

This blog entry contains some helpful information about removing special characters from a string using javascript. I was able to modify it to be just a few of code in an onchange event. Just rename new_name to the attribute your correcting.

if (crmForm.all.new_name.DataValue && crmForm.all.new_name.DataValue.length >0)
{
crmForm.all.new_name.DataValue = crmForm.all.new_name.DataValue.replace(/[^a-zA-Z 0-9]+/g,'');
}