Pages

Sunday 26 January 2014

Set Multi-Select Lookup in CRM 2011

Basically MSD CRM 2011 does not contains Multi-Select Lookup . So to set Multi-Select Lookup in CRM we need to go for coding. The below Snippet is essential for Multi-Select Lookup. If we place the below code in "On-Load" event of  a form then the lookup will be a Multi-Select Lookup.

function  load()
{
document.getElementById("lookup Schema Name").setAttribute("lookupstyle", "multi");
}

The above syntax makes the Lookup Feild as Multi-Select. suppose if we replace "multi" by "single" in the above code, then the lookup will be normal lookup.

The above is not enough, it is just use to select mutiple records in lookup. but we want to retrive those selected record details. I am taking a scenario in which I am Slelcting multiple records from a lookup and Placing those selected record details in one multiline-textfeild and again retrieving those field in the same lookup.
So this snippet consists of two functions for On-Load and On-Save.



Code for On-Load
------->>
function  load()
{
document.getElementById("lookup schema name").setAttribute("lookupstyle", "multi");
if(Xrm.Page.ui.getFormType()!=1)
{
var data=new Array();
// I created a one multiline text box to save the lookup values.
// set the multiline textbox visible equal to false.
var store=Xrm.Page.getAttribute("schema name of multiple text feild").getValue();
if(store!=null)
data=store.split(";");
// To get the "Target record type" Go to your form Customization ---> double click on your lookup--->Details--->Edit--->Here you can see "Target Record Type"
var typename = "Target Record Type";   //its mandatory to mention type name
 var arr= new Array();
 var  i=0;
var   j=0;
for(i=0;i<((data.length-1)/2);i++)
{
arr[i] = new Object();
arr[i].name = data[j];
arr[i].id= data[j+1];
arr[i].typename= typename ;
j++;
j=j+1;
}
crmForm.all["lookup schema name"].DataValue = arr;
}
}




Code for On-Save
On Saving, the selected records from lookup are stored in a MultiLine text-box with GUID.. so that it can be used while retrieving the record details.
-------->>
function save() {

var value = crmForm.all["lookup schema name"].DataValue;
var s;
var temp = "";

for (s = 0; s < value.length; s++) {
var temp2 = "";
temp2 = value[s].name+";"+value[s].id+";";
temp = temp+""+temp2;
}
Xrm.Page.getAttribute("schema name of multiple text feild").setValue(temp);
Xrm.Page.getAttribute("lookup schema name").setValue(null);
document.getElementById("lookup schema name").setAttribute("lookupstyle", "single");
}

3 comments:

  1. on save code is not working

    ReplyDelete
  2. How to create a multi select lookup field in crm account form.

    ReplyDelete
  3. on save code still seems to be broken. Anyone know the fix? It doesn't show an error, just "undefined"

    ReplyDelete