locked
How to set lookup field value in crm 2013 by using javascript RRS feed

  • Question

  • HI,

    I want to set value in lookup field from getting another lookup field. I used this script its working fine but object value is not set into lookup

    function getProject()
    {
    var serverUrl;
    var account=Xrm.Page.getAttribute("new_task").getValue();
    var accountid = account[0].name;
    var guid=account[0].id;

    var guidId = guid.replace('{', '').replace('}', '');
    //alert(guidId);
    if (Xrm.Page.context.getClientUrl !== undefined)
    {
    serverUrl = Xrm.Page.context.getClientUrl();
    }
    else
    {
    serverUrl = Xrm.Page.context.getServerUrl();
    }
       var oDataURI = serverUrl
            + "/XRMServices/2011/OrganizationData.svc/"
            + "TaskSet"
            + "?$filter="
            + "ActivityId eq (guid'" + guidId+ "')"
            + "&$select=new_Project";
    alert(oDataURI);

    var userRequest = new XMLHttpRequest();
    //alert(completeUrl);
    userRequest.open("GET", oDataURI, false);
    userRequest.setRequestHeader("Accept", "application/json");
    userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    userRequest.send();
    if (userRequest.status === 200)
    {
    var project=Array();
    var retrievedUser = JSON.parse(userRequest.responseText).d;
    project = retrievedUser.results[0].new_Project;
    alert(project);

    Xrm.Page.getAttribute("new_projectname").setValue(project); //new_projectname is lookup field
    }
    else
    {
    alert("error");
    return "error";
    }
    }

    here is result:

    Thursday, April 3, 2014 10:37 AM

All replies

  • Hi,

    I want to set value in lookup field from getting another lookup field. I used this script its working fine but object value is not set into lookup

    function getProject()
    {
    var serverUrl;
    var account=Xrm.Page.getAttribute("new_task").getValue();
    var accountid = account[0].name;
    var guid=account[0].id;

    var guidId = guid.replace('{', '').replace('}', '');
    //alert(guidId);
    if (Xrm.Page.context.getClientUrl !== undefined)
    {
    serverUrl = Xrm.Page.context.getClientUrl();
    }
    else
    {
    serverUrl = Xrm.Page.context.getServerUrl();
    }
       var oDataURI = serverUrl
            + "/XRMServices/2011/OrganizationData.svc/"
            + "TaskSet"
            + "?$filter="
            + "ActivityId eq (guid'" + guidId+ "')"
            + "&$select=new_Project";
    alert(oDataURI);

    var userRequest = new XMLHttpRequest();
    //alert(completeUrl);
    userRequest.open("GET", oDataURI, false);
    userRequest.setRequestHeader("Accept", "application/json");
    userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    userRequest.send();
    if (userRequest.status === 200)
    {
    var project=Array();
    var retrievedUser = JSON.parse(userRequest.responseText).d;
    project = retrievedUser.results[0].new_Project;
    alert(project);

    Xrm.Page.getAttribute("new_projectname").setValue(project); //new_projectname is lookup field
    }
    else
    {
    alert("error");
    return "error";
    }
    }

    Thursday, April 3, 2014 9:27 AM
  • Keep drilling into the results, the first result [0] will have the 3 attributes you need to create a new lookup value. You aren't able to set a lookup field directly to this result.

    So something like:

    var value = new Array();
    value[0] = new Object();
    value[0].id = retrievedUser.results[0].new_Project.Id;
    value[0].name = retrievedUser.results[0].new_Project.Name;
    value[0].entityType = retrievedUser.results[0].new_Project.LogicalName;
    Xrm.Page.getAttribute("new_projectname").setValue(value;)
    


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Thursday, April 3, 2014 1:00 PM
    Moderator
  • okay thanx JLattimer
    Thursday, April 3, 2014 1:06 PM