locked
fetching lookup record attribute error in crm 2013 RRS feed

  • Question

  • Hi,

    I want to get specific attribute value of selected lookup record. When i run oDataURI in browser it displays bad request error.

    https://bahrian/XRMServices/2011/OrganizationData.svc/new_datacentreSet?$filter=new_name eq'data1'&$select=new_Website,Content

    userRequest.Status is "bad request" Please tell me correct code

    here is my code

    function getCurrentUserFullName()
    {
    var serverUrl;
    var account=Xrm.Page.getAttribute("new_datacentre").getValue();
    var accountid = account[0].name;
    //accountid = accountid.replace('{', '').replace('}', '');
    alert(accountid);
    if (Xrm.Page.context.getClientUrl !== undefined)
    {
    alert("2");
    serverUrl = Xrm.Page.context.getClientUrl();
    }
    else
    {
    serverUrl = Xrm.Page.context.getServerUrl();
    }
    alert("3");
       var oDataURI = serverUrl
            + "/XRMServices/2011/OrganizationData.svc/"
            + "new_datacentreSet"
            + "?$filter="
            + "new_name eq '" + accountid + "'"
            + "&$select=new_Website,Content";
     alert(oDataURI);

    //var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
    //var completeUrl=ODataPath+"/new_datacentreSet(guid'" +accountid+ "')";
    var userRequest = new XMLHttpRequest();
    //alert(completeUrl);
    userRequest.open("GET", oDataURI, false);
    //userRequest.open("GET", completeUrl, true);
    alert("sds");
    userRequest.setRequestHeader("Accept", "application/json");
    alert("sds1");
    userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    userRequest.send();
    alert("sds4");
    if (userRequest.status === 200)
    {
    alert("sdsds");
    var retrievedUser = JSON.parse(userRequest.responseText).d;
    alert("sdsdfdfs");
    var userFullName = retrievedUser.new_Website;
    alert(userFullName);
    }
    else
    {
    alert("sorry");
    return "error";
    }
    }

    Sunday, March 23, 2014 3:41 PM

Answers

All replies

  • Hi Aamir,

    It seems your query is correct, but try to make sure your are using correct schema names and try debug your code and see if you are getting correct url, also try to give some space between value and operator like below.

    eq 'data1' &$


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, March 24, 2014 12:32 AM
    Moderator
  • Hi Mahender,

    My code is working fine but userFullName value is undefined. Please tell me what did i mistake in this line? how can i get attribute value because retrievedUser is in array.

    var userFullName = retrievedUser.new_Website;

    Monday, March 24, 2014 5:05 AM
  • Ohh, I missed it, it should be like below

    > retrievedUser.results[index].new_Website, for example first records will be retrievedUser.results[0].new_Website


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, March 24, 2014 5:26 AM
    Moderator
  • I got it. thanx 
    Monday, March 24, 2014 5:28 AM
  • Now tell me 1 thing I want to get new_startdate and set into new_enddate field both fields are DateTime field but it is not working. Do you know that how to set date in other field?

    I am using like this

    var userFullName = retrievedUser.results[0].new_EndDate;
    Xrm.Page.getAttribute("new_startdate").setValue(userFullName);

    Monday, March 24, 2014 5:34 AM
  • Hello,

    Getting datetime fields from rest result is not just getting that field directly, instead you have to parse it , checkk my post it will help you to implement your requirement.


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, March 24, 2014 5:41 AM
    Moderator
  • thanx mahender
    Monday, March 24, 2014 5:59 AM
  • Mahender

    If I want to get attribute value by guid id of selected lookup then how to modify my query?

    I modified my query

    var oDataURI = serverUrl
            + "/XRMServices/2011/OrganizationData.svc/"
            + "new_datacentreSet"
            + "?$filter="
            + "new_name eq '" + accountid + "'"
            + "&$select=new_EndDate";

    to

    var account=Xrm.Page.getAttribute("new_datacentre").getValue();
    var guidId=account[0].id;
    var guid = guidid.replace('{', '').replace('}', '');

    var oDataURI = serverUrl
            + "/XRMServices/2011/OrganizationData.svc/"
            + "new_datacentreSet"
            + "?$filter="
            + "new_datacentreId eq '" + guid + "'"
            + "&$select=new_EndDate";

    Is it correct code?

    Monday, March 24, 2014 6:43 AM
  • Hello,

    I hope you want to query based on primary attribute id then it should be like below

    var oDataURI = serverUrl
            + "/XRMServices/2011/OrganizationData.svc/"
            +"new_datacentreSet(guid'" + guidId+ "')"
            + "&$select=new_EndDate";


    but if you are fetching data based on other lookup field in that entity then it should below like below

    var oDataURI = serverUrl
            + "/XRMServices/2011/OrganizationData.svc/"
            + "new_datacentreSet"
            + "?$filter="
            + "new_datacentreId/Id eq (guid'" + guidId+ "')"
            + "&$select=new_EndDate";


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.


    Monday, March 24, 2014 7:03 AM
    Moderator
  • Hello Mahender,

    Please tell me what is Id in this line

    + "new_datacentreId/Id eq (guid'" + guidId+ "')"

    Monday, March 24, 2014 7:14 AM
  • How can i check guid id name in xml for example entity name is Task now tell me how can i get guid name of Task?
    Monday, March 24, 2014 7:25 AM
  • it's a syntax, please check this for different options for query Odata, but make sure this is the case where you are fetching data from entity using lookup field in that entity.


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, March 24, 2014 7:26 AM
    Moderator
  • You need to check for primary attribute schema name of that entity for example, in case of task it will be ActivityId, so it should be something like below

    retrieved.results[0].ActivityId.Name

    Also I will suggest you to start a new thread if you different question, so that it can help others as well.


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, March 24, 2014 7:50 AM
    Moderator