Question Status

Verified
samuelcheong asked a question on 30 Mar 2013 8:29 AM

in opportunity form, how can I pop out some field's.value in lookup field ? for example, opportunity has 'owner' lookup field, I want to pop out the owner phone number from owners entity.

Reply
Verified Answer
Jason Lattimer responded on 30 Mar 2013 3:42 PM

Try this:

function GetOwner() {

    var owner = new Array();
    owner = Xrm.Page.getAttribute("ownerid").getValue();

    if (owner == null) {
        return;
    }

    var serverUrl = Xrm.Page.context.getClientUrl();
    var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/SystemUserSet?$select=Address1_Telephone1&$filter=SystemUserId eq guid'" + owner[0].id + "'";

    var retrieveReq = new XMLHttpRequest();
    retrieveReq.open("GET", oDataSelect, false);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    retrieveReq.onreadystatechange = function () {
        GetOwnerData(this);
    };
    retrieveReq.send();
}

function GetOwnerData(retrieveReq) {
    if (retrieveReq.readyState == 4) {
        if (retrieveReq.status == 200) {
            var retrieved = JSON.parse(retrieveReq.responseText).d;
            alert(retrieved.results[0].Address1_Telephone1);
        }
    }
}

Bind GetOwner to whichever event is going to trigger

Jason Lattimer
My Blog -  Follow me on Twitter -  LinkedIn

Reply
samuelcheong responded on 31 Mar 2013 11:16 PM

This program work fine if i try to look for Address1_Telephone1. When i change this program to look for business unit instead of address1_telephone1. It cannot works. Is it because "business unit" is a lookup field ?

Reply
Muhammad Adeel Javaid responded on 1 Apr 2013 3:47 AM

The Jave Code to get a look up value might go like this:

var lookup = new Array();

lookup = Xrm.Page.getAttribute("attributename").getValue();

if (lookup != null) {

   var name = lookup[0].name;

   var id = lookup[0].id;

   var entityType = lookup[0].entityType;

}

M.Adeel

Reply
samuelcheong responded on 1 Apr 2013 7:53 AM

when i try to display "business unit" in user entity, this program cannot works. It Shows "UNDEFINED" when i try to alert () this variable. But it works fine for text field.

Reply
Suggested Answer
Muhammad Adeel Javaid responded on 1 Apr 2013 8:08 AM

I am suggesting another JS Code to Retrieve Entity Record. You can alter it or make any changes to get your desired values:

// Only make changes to this function; you may add this function to Form Onload Event,

// Field OnChange events etc.

function GetDetails() {

   var EntityName, EntityId, AccountNumber, AccountEmailAddress, LookupFieldObject;

   var PrimaryContactLookupId, PrimaryContactLookupName, PrimaryContactLookupType;

   var resultXml;

   LookupFieldObject = Xrm.Page.data.entity.attributes.get('parentcustomerid');

   // If lookup field has value then the code will only run

   if (LookupFieldObject.getValue() != null) {

       //Fetch and place Entity Id (GUID) and Name (String) form lookup field into local variables

       EntityId = LookupFieldObject.getValue()[0].id;

       EntityName = LookupFieldObject.getValue()[0].entityType;

       // Paramter explaination for function call

       // RetrieveEntityById('account', AccountId, 'accountnumber,emailaddress1');

       // 1st paramter requires entity name (not display or schema name) just pass it in a string

       // i.e. for Account entity use 'account' for opportunity entity use 'opportunity'

       // 2nd paramter requires entity Id (GUID Type) field which we have retrieved and stored in

       // AccountId local variable

       // 3rd paramter requires attributes to be retrieved schema name, if thrid attributed is

       // required to be retrieved then use like: i.e. 'accountnumber,emailaddress1,telephone2'

       resultXml = RetrieveEntityById(EntityName, EntityId, 'accountnumber,emailaddress1,primarycontactid');

       // In retrieved XML document check if it has accountnumber attribute

       if (resultXml != null && resultXml.selectSingleNode('//q1:accountnumber') != null) {

           // If XML document has account number attribute then assign to local variable AccountNumber

           AccountNumber = resultXml.selectSingleNode('//q1:accountnumber').nodeTypedValue;

           //Display Account Number Value in a Message Box

           alert("Account Number :" + AccountNumber);

           //If required then use the below code line to set value in field on form

           //Xrm.Page.data.entity.attributes.get('new_myaccountnumber').setValue(AccountNumber);

       }

       // In retrieved XML document check if it has emailaddress1 attribute

       if (resultXml != null && resultXml.selectSingleNode('//q1:emailaddress1') != null) {

           // If XML document has account number attribute then assign to local variable AccountEmailAddress

           AccountEmailAddress = resultXml.selectSingleNode('//q1:emailaddress1').nodeTypedValue;

           alert("Email Address :" + AccountEmailAddress);

           //If required then use the below code line to set value in field on form

           //Xrm.Page.data.entity.attributes.get('new_myemailaddress').setValue(AccountEmailAddress);

       }

         // In retrieved XML document check if it has primarycontactid lookup attribute

       if (resultXml != null && resultXml.selectSingleNode('//q1:primarycontactid') != null) {

           // If XML document has primarycontactid lookup attribute then assign to local variable

           PrimaryContactLookupId = resultXml.selectSingleNode('//q1:primarycontactid').nodeTypedValue;

           // If XML document has primarycontactid lookup attribute then assign to local variable

           PrimaryContactLookupName = resultXml.selectSingleNode('//q1:primarycontactid').getAttribute("name");

           // If XML document has primarycontactid lookup attribute then assign to local variable

           PrimaryContactLookupType = resultXml.selectSingleNode('//q1:primarycontactid').getAttribute("type") ;

           alert("Primary Contact Lookup Id :" + PrimaryContactLookupId);

           alert("Primary Contact Lookup Name :" + PrimaryContactLookupName);

           alert("Primary Contact Lookup Type :" + PrimaryContactLookupType);

       }      

   }

}

// Do not make any changes to this function

function RetrieveEntityById(prmEntityName, prmEntityId, prmEntityColumns) {

   var resultXml, errorCount, msg, xmlHttpRequest, arrayEntityColumns, xmlEntityColumns;

   arrayEntityColumns = prmEntityColumns.split(",");

   for (var i = 0; i < arrayEntityColumns.length; i++) {

       xmlEntityColumns += "<q1:Attribute>" + arrayEntityColumns[i] + "</q1:Attribute>";

   }

   var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();

   //Prepare the SOAP message.

   var xml = "<?xml version='1.0' encoding='utf-8'?>" +

   "<soap:Envelope xmlns:soap='schemas.xmlsoap.org/.../& +

   " xmlns:xsi='www.w3.org/.../XMLSchema-instance& +

   " xmlns:xsd='www.w3.org/.../XMLSchema& +

   authenticationHeader +

   "<soap:Body>" +

   "<Retrieve xmlns='schemas.microsoft.com/.../WebServices& +

   "<entityName>" + prmEntityName + "</entityName>" +

   "<id>" + prmEntityId + "</id>" +

   "<columnSet xmlns:q1='schemas.microsoft.com/.../Query& xsi:type='q1:ColumnSet'>" +

   "<q1:Attributes>" +

   xmlEntityColumns +

  "</q1:Attributes>" +

   "</columnSet>" +

   "</Retrieve></soap:Body></soap:Envelope>";

   //call function to create Soap Request to ms crm webservice

   xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

   xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);

   xmlHttpRequest.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Retrieve");

   xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

   xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

   xmlHttpRequest.send(xml);

   resultXml = xmlHttpRequest.responseXML;

   var errorCount = resultXml.selectNodes('//error').length;

   if (errorCount != 0) {

       var msg = resultXml.selectSingleNode('//description').nodeTypedValue;

       alert("Error Message : " + msg);

   }

   else {

       return resultXml;

   }

}

M.Adeel

Reply
Jason Lattimer responded on 1 Apr 2013 8:17 AM

Modified to also get the Business Unit:

function GetOwner() {

    var owner = new Array();
    owner = Xrm.Page.getAttribute("ownerid").getValue();

    if (owner == null) {
        return;
    }

    var serverUrl = Xrm.Page.context.getClientUrl();
    var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/SystemUserSet?$select=Address1_Telephone1,BusinessUnitId&$filter=SystemUserId eq guid'" + owner[0].id + "'";

    var retrieveReq = new XMLHttpRequest();
    retrieveReq.open("GET", oDataSelect, false);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    retrieveReq.onreadystatechange = function () {
        GetOwnerData(this);
    };
    retrieveReq.send();
}

function GetOwnerData(retrieveReq) {
    if (retrieveReq.readyState == 4) {
        if (retrieveReq.status == 200) {
            var retrieved = JSON.parse(retrieveReq.responseText).d;
            alert(retrieved.results[0].Address1_Telephone1);
            if (retrieved.results[0].BusinessUnitId != null) {
                alert(retrieved.results[0].BusinessUnitId.Name);
            }
        }
    }
}

Jason Lattimer
My Blog -  Follow me on Twitter -  LinkedIn

Reply
samuelcheong responded on 1 Apr 2013 8:42 PM

Hi jlattimer,

Thanks for your answer, now i would like to assign the BusinessUnitId.Name into my customized field "BUSINESS UNIT" in opportunity form. Is it possible ?

see here.

i1208.photobucket.com/.../opp_zps3f299c20.png

Reply
Suggested Answer
Jason Lattimer responded on 2 Apr 2013 6:10 AM

Try this:

function GetOwner() {

    var owner = new Array();
    owner = Xrm.Page.getAttribute("ownerid").getValue();

    if (owner == null) {
        return;
    }

    var serverUrl = Xrm.Page.context.getClientUrl();
    var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/SystemUserSet?$select=Address1_Telephone1,BusinessUnitId&$filter=SystemUserId eq guid'" + owner[0].id + "'";

    var retrieveReq = new XMLHttpRequest();
    retrieveReq.open("GET", oDataSelect, false);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    retrieveReq.onreadystatechange = function () {
        GetOwnerData(this);
    };
    retrieveReq.send();
}

function GetOwnerData(retrieveReq) {
    if (retrieveReq.readyState == 4) {
        if (retrieveReq.status == 200) {
            var retrieved = JSON.parse(retrieveReq.responseText).d;
            alert(retrieved.results[0].Address1_Telephone1);
            var value = new Array();
            value[0] = new Object();
            value[0].id = retrieved.results[0].BusinessUnitId.Id;
            value[0].name = retrieved.results[0].BusinessUnitId.Name;
            value[0].entityType = retrieved.results[0].BusinessUnitId.LogicalName;
            Xrm.Page.getAttribute("new_businessunit").setValue(value);
        }
    }
}

Jason Lattimer
My Blog -  Follow me on Twitter -  LinkedIn

Reply
MadPro .... responded on 2 Aug 2014 11:53 PM

same we need data with putting any id only "Top1" and "Orderby" criteria. how is possible

Reply
Suggested Answer
Aileen Gusni responded on 3 Aug 2014 2:35 AM

Hi Shaikh,

You can use $top and $orderyy

for example:

$top=5&$orderby=Name desc

and

/OpportunitySet?$orderby=ActualCloseDate desc&$top=1

see these links:

pogo69.wordpress.com/.../crm-2011-odata-rest-and-the-querystring

www.odata.org/.../uri-conventions

mscrmbasics.blogspot.com/.../crm-2011-odata-explained.html

Hope it helps!

Thanks.

Reply
Verified Answer
Jason Lattimer responded on 30 Mar 2013 3:42 PM

Try this:

function GetOwner() {

    var owner = new Array();
    owner = Xrm.Page.getAttribute("ownerid").getValue();

    if (owner == null) {
        return;
    }

    var serverUrl = Xrm.Page.context.getClientUrl();
    var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/SystemUserSet?$select=Address1_Telephone1&$filter=SystemUserId eq guid'" + owner[0].id + "'";

    var retrieveReq = new XMLHttpRequest();
    retrieveReq.open("GET", oDataSelect, false);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    retrieveReq.onreadystatechange = function () {
        GetOwnerData(this);
    };
    retrieveReq.send();
}

function GetOwnerData(retrieveReq) {
    if (retrieveReq.readyState == 4) {
        if (retrieveReq.status == 200) {
            var retrieved = JSON.parse(retrieveReq.responseText).d;
            alert(retrieved.results[0].Address1_Telephone1);
        }
    }
}

Bind GetOwner to whichever event is going to trigger

Jason Lattimer
My Blog -  Follow me on Twitter -  LinkedIn

Reply
Suggested Answer
Muhammad Adeel Javaid responded on 1 Apr 2013 8:08 AM

I am suggesting another JS Code to Retrieve Entity Record. You can alter it or make any changes to get your desired values:

// Only make changes to this function; you may add this function to Form Onload Event,

// Field OnChange events etc.

function GetDetails() {

   var EntityName, EntityId, AccountNumber, AccountEmailAddress, LookupFieldObject;

   var PrimaryContactLookupId, PrimaryContactLookupName, PrimaryContactLookupType;

   var resultXml;

   LookupFieldObject = Xrm.Page.data.entity.attributes.get('parentcustomerid');

   // If lookup field has value then the code will only run

   if (LookupFieldObject.getValue() != null) {

       //Fetch and place Entity Id (GUID) and Name (String) form lookup field into local variables

       EntityId = LookupFieldObject.getValue()[0].id;

       EntityName = LookupFieldObject.getValue()[0].entityType;

       // Paramter explaination for function call

       // RetrieveEntityById('account', AccountId, 'accountnumber,emailaddress1');

       // 1st paramter requires entity name (not display or schema name) just pass it in a string

       // i.e. for Account entity use 'account' for opportunity entity use 'opportunity'

       // 2nd paramter requires entity Id (GUID Type) field which we have retrieved and stored in

       // AccountId local variable

       // 3rd paramter requires attributes to be retrieved schema name, if thrid attributed is

       // required to be retrieved then use like: i.e. 'accountnumber,emailaddress1,telephone2'

       resultXml = RetrieveEntityById(EntityName, EntityId, 'accountnumber,emailaddress1,primarycontactid');

       // In retrieved XML document check if it has accountnumber attribute

       if (resultXml != null && resultXml.selectSingleNode('//q1:accountnumber') != null) {

           // If XML document has account number attribute then assign to local variable AccountNumber

           AccountNumber = resultXml.selectSingleNode('//q1:accountnumber').nodeTypedValue;

           //Display Account Number Value in a Message Box

           alert("Account Number :" + AccountNumber);

           //If required then use the below code line to set value in field on form

           //Xrm.Page.data.entity.attributes.get('new_myaccountnumber').setValue(AccountNumber);

       }

       // In retrieved XML document check if it has emailaddress1 attribute

       if (resultXml != null && resultXml.selectSingleNode('//q1:emailaddress1') != null) {

           // If XML document has account number attribute then assign to local variable AccountEmailAddress

           AccountEmailAddress = resultXml.selectSingleNode('//q1:emailaddress1').nodeTypedValue;

           alert("Email Address :" + AccountEmailAddress);

           //If required then use the below code line to set value in field on form

           //Xrm.Page.data.entity.attributes.get('new_myemailaddress').setValue(AccountEmailAddress);

       }

         // In retrieved XML document check if it has primarycontactid lookup attribute

       if (resultXml != null && resultXml.selectSingleNode('//q1:primarycontactid') != null) {

           // If XML document has primarycontactid lookup attribute then assign to local variable

           PrimaryContactLookupId = resultXml.selectSingleNode('//q1:primarycontactid').nodeTypedValue;

           // If XML document has primarycontactid lookup attribute then assign to local variable

           PrimaryContactLookupName = resultXml.selectSingleNode('//q1:primarycontactid').getAttribute("name");

           // If XML document has primarycontactid lookup attribute then assign to local variable

           PrimaryContactLookupType = resultXml.selectSingleNode('//q1:primarycontactid').getAttribute("type") ;

           alert("Primary Contact Lookup Id :" + PrimaryContactLookupId);

           alert("Primary Contact Lookup Name :" + PrimaryContactLookupName);

           alert("Primary Contact Lookup Type :" + PrimaryContactLookupType);

       }      

   }

}

// Do not make any changes to this function

function RetrieveEntityById(prmEntityName, prmEntityId, prmEntityColumns) {

   var resultXml, errorCount, msg, xmlHttpRequest, arrayEntityColumns, xmlEntityColumns;

   arrayEntityColumns = prmEntityColumns.split(",");

   for (var i = 0; i < arrayEntityColumns.length; i++) {

       xmlEntityColumns += "<q1:Attribute>" + arrayEntityColumns[i] + "</q1:Attribute>";

   }

   var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();

   //Prepare the SOAP message.

   var xml = "<?xml version='1.0' encoding='utf-8'?>" +

   "<soap:Envelope xmlns:soap='schemas.xmlsoap.org/.../& +

   " xmlns:xsi='www.w3.org/.../XMLSchema-instance& +

   " xmlns:xsd='www.w3.org/.../XMLSchema& +

   authenticationHeader +

   "<soap:Body>" +

   "<Retrieve xmlns='schemas.microsoft.com/.../WebServices& +

   "<entityName>" + prmEntityName + "</entityName>" +

   "<id>" + prmEntityId + "</id>" +

   "<columnSet xmlns:q1='schemas.microsoft.com/.../Query& xsi:type='q1:ColumnSet'>" +

   "<q1:Attributes>" +

   xmlEntityColumns +

  "</q1:Attributes>" +

   "</columnSet>" +

   "</Retrieve></soap:Body></soap:Envelope>";

   //call function to create Soap Request to ms crm webservice

   xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

   xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);

   xmlHttpRequest.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Retrieve");

   xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

   xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

   xmlHttpRequest.send(xml);

   resultXml = xmlHttpRequest.responseXML;

   var errorCount = resultXml.selectNodes('//error').length;

   if (errorCount != 0) {

       var msg = resultXml.selectSingleNode('//description').nodeTypedValue;

       alert("Error Message : " + msg);

   }

   else {

       return resultXml;

   }

}

M.Adeel

Reply
Suggested Answer
Jason Lattimer responded on 2 Apr 2013 6:10 AM

Try this:

function GetOwner() {

    var owner = new Array();
    owner = Xrm.Page.getAttribute("ownerid").getValue();

    if (owner == null) {
        return;
    }

    var serverUrl = Xrm.Page.context.getClientUrl();
    var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/SystemUserSet?$select=Address1_Telephone1,BusinessUnitId&$filter=SystemUserId eq guid'" + owner[0].id + "'";

    var retrieveReq = new XMLHttpRequest();
    retrieveReq.open("GET", oDataSelect, false);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    retrieveReq.onreadystatechange = function () {
        GetOwnerData(this);
    };
    retrieveReq.send();
}

function GetOwnerData(retrieveReq) {
    if (retrieveReq.readyState == 4) {
        if (retrieveReq.status == 200) {
            var retrieved = JSON.parse(retrieveReq.responseText).d;
            alert(retrieved.results[0].Address1_Telephone1);
            var value = new Array();
            value[0] = new Object();
            value[0].id = retrieved.results[0].BusinessUnitId.Id;
            value[0].name = retrieved.results[0].BusinessUnitId.Name;
            value[0].entityType = retrieved.results[0].BusinessUnitId.LogicalName;
            Xrm.Page.getAttribute("new_businessunit").setValue(value);
        }
    }
}

Jason Lattimer
My Blog -  Follow me on Twitter -  LinkedIn

Reply
Suggested Answer
Aileen Gusni responded on 3 Aug 2014 2:35 AM

Hi Shaikh,

You can use $top and $orderyy

for example:

$top=5&$orderby=Name desc

and

/OpportunitySet?$orderby=ActualCloseDate desc&$top=1

see these links:

pogo69.wordpress.com/.../crm-2011-odata-rest-and-the-querystring

www.odata.org/.../uri-conventions

mscrmbasics.blogspot.com/.../crm-2011-odata-explained.html

Hope it helps!

Thanks.

Reply