使用XRMServiceToolkit在HTML Web资源中使用移动号码检索联系人

时间:2018-09-09 09:46:18

标签: javascript html dynamics-crm contacts webresource

我一直在关注HTML Webresource,我需要获取联系人ID,然后再进行其余工作。它提示我我有1个针对该特定电话号码的联系人,但在retreivedContact处引发异常(以下javascript中的变量)。当我查看并检查mobileNumber调试器时,它也未定义。您能看到我的东西吗?

    <!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>

1 个答案:

答案 0 :(得分:0)

此问题已解决,我更改了以下代码段

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

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