Search code examples
javascripthtmldynamics-crmcontactswebresource

Retreive Contacts Using Mobile Number in an HTML Webresource using XRMServiceToolkit


I've following HTML webresource, I need to get the contact id and then do rest of the stuff. It prompt me that I've 1 contact against that specific phone number but it throw exception at retreivedContact (variable in following javascript). When I go over and check the mobileNumber debugger also says that its undefined. Can you please see and let me know what is messing up the things:

    <!DOCTYPE html>
<html>
<head>
    <title>Contacts</title>
    <script type="text/javascript" src="ClientGlobalContext.js.aspx"></script>
    <script type="text/javascript" src="../WebResources/new_jquery_1.9.1.min" ></script>
    <script type="text/javascript" src="../WebResources/new_xrmjson" ></script>
    <script type="text/javascript" src="../WebResources/new_xrmservicetoolkit" ></script>
    <script type="text/javascript">
        function getLoggedInUserRoles() {
            try {
                debugger;
            var context = GetGlobalContext();
            var mobileNumber = "xxx-xxx-xxx";
            if (mobileNumber.length > 0) {
                    var query = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                        "  <entity name='contact'>" +
                        "    <attribute name='fullname' />" +
                        "    <attribute name='telephone1' />" +
                        "    <attribute name='contactid' />" +
                        "    <order attribute='fullname' descending='false' />" +
                        "    <filter type='and'>" +
                        "      <condition attribute='mobilephone' operator='eq' value='" + mobileNumber + "'/>" +
                        "    </filter>" +
                        "  </entity>" +
                        "</fetch>";

                    var retreivedContacts = XrmServiceToolkit.Soap.Fetch(query);

                    if (retreivedContacts.length == 1) {
                        alert(retreivedContacts.length);
                        alert(retreivedContacts[0].attributes['contactid']);
                        //Xrm.Utility.openEntityForm("contact", retreivedContacts[0].attributes['contactid'])
                    }
                }
            } catch (e) {
            alert("Error: "+e.Message);
            }
    }
    </script>
</head>
<body>  
    <button onclick="getLoggedInUserRoles()">Click here</button>
</body>
</html>

Solution

  • This is resolved, I change following snippet

    alert(retreivedContacts[0].attributes['contactid']);
    

    to

    alert(retreivedContacts[0].attributes['contactid'].value);