Tuesday, October 2, 2012

MSCRM 2011 LOOKUP Multi Value Selection.


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.

For that you have to create one Lookup called Sales Order where its name is new_salesorder
and one another create Multiline TextBox called Order Value where its name is new_ordervalue

On Form Properties Load Event Add Library and called function load.(Create a Web Resource for Adding Javascript.)

function load() {

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

     document.getElementById("new_salesorder").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_ordervalue").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";

        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;


        }

        if(i!=0 && j!=0)
              crmForm.all["new_salesorder"].DataValue = arr;

    }


}

On Form Properties Save Event Add Library and called Function Save(Create a Web Resource for Adding Javascript.)

function save() {

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


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


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

}

No comments:

Post a Comment