Thursday 12 April 2012

MSCRM 2011 LOOKUP Multi Value Selection




MSCRM 2011 LOOKUP Multi Value  

Hi This is Sudhakar Reddy Chidipudi

By default you can select only one value from lookup.I thought to post the below code ,by using below code you can select multiple values from lookup.

Below you can see i wrote two functions ,OnSave of the form you can get the multiple values from lookup and save those values in a multiline text box.

To use the below code you need a lookup and one multiline text box.

//Form Onload
function  load()

{

document.getElementById("Your lookup Schema Name").setAttribute("lookupstyle", "multi");

document.getElementById("Your 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("new_textbox").getValue();

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";

 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["Your lookup Schema Name"].DataValue = arr;

}

}
//Form OnSave

function save()

{


 var value = crmForm.all["Your 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("new_textbox").setValue(temp);

Xrm.Page.getAttribute("Your lookup Schema Name").setValue(null);

document.getElementById("Your lookup Schema Name").setAttribute("lookupstyle", "single");

 }


Have a nice day.................................
Sudhakar Reddy Chidipudi


17 comments:

  1. Thanks Sudhakar .. Keep Posting the useful things like these...

    ---Suresh Kumar

    ReplyDelete
  2. Hi.. Sudhakar Reddy Chidipudi
    Really amazing Article.. It really work perfect in all entity except Training Entity, Can you plz help to solve this issue.

    ReplyDelete
    Replies
    1. Hi,
      Can i know the issue you are facing in your Training Entity.

      Delete
    2. This comment has been removed by the author.

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hi its reaaly amazing!!!!
      Can i know one thing we i select multiple values for a lookup
      can i open any value from their itself

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hi sudhakar,
    Thanks for you post.

    while saving its takes only one value, it doesn't save the multiple values. can u pls give the solution?.

    This is bit urgent .. Please reply me as soon as possible

    Thanks in advance :)

    ReplyDelete
    Replies
    1. Hi

      I checked the code .It works perfectly

      Delete
    2. sudhakar,

      Thanks for ur reply.

      its getting multi value properly, after saving it shows only one value not saving multi selected value.. thats my issue.

      pls give solution for this.

      Thanks in advance

      Delete
  6. Do I have to insert "primarycontactid" as a "userid" if my lookup field is looking for Users? (Appointment entity, multiple lookup field, users) document.getElementById("primarycontactid").setAttribute("lookupstyle", "multi");

    ReplyDelete
  7. My code below, could you check it is correct? And I call these just by attribute "load()" or "save()" from the event handler?

    //Form Onload
    function load()


    {


    document.getElementById("primarycontactid").setAttribute("lookupstyle", "multi");


    document.getElementById("primarycontactid").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("evl_new_textbox").getValue();


    data=store.split(";");


    var typename = "User";

    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["evl_Colleague"].DataValue = arr;

    }


    }

    //Form OnSave

    function save()


    {




    var value = crmForm.all["evl_Colleague"].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("evl_new_textbox").setValue(temp);


    Xrm.Page.getAttribute("evl_Colleague").setValue(null);


    document.getElementById("evl_Colleague").setAttribute("lookupstyle", "single");

    }

    ReplyDelete
  8. This code is awesome. Works like a charm except one issue.

    When you try to close the form, system gives a message that you have some changes pending to save.

    How to skip/avoid this message?

    ReplyDelete
  9. Hi
    This code is really helpful but don't have browser compatiblity .
    When i run the same script in chrome it give me an error.
    Can anybody help me to perform the same thing by silverlight?

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. After roll up 12 , the above code is not working for multi value selection in lookup field. Any idea??

    ReplyDelete