需要帮助:联系电话上的帐户名称

时间:2014-06-15 19:47:01

标签: dynamics-crm-2011

我是CRM的新手,并且能够弄清楚到目前为止的一切。我已经阅读了很多帖子和互联网帖子并尝试了它们。我看了很多视频。看起来应该很容易。联系人记录具有ParentCustomerID和ParentCustomerName,如果有一个关联,则持有该帐户。现在我对所需的步骤感到困惑。

要求: - 我需要在联系人电话表格上显示帐户名称并保存在电话呼叫表中,以便在电话呼叫视图中显示。

我有电话呼叫N:1关系字段str_companyid(查找)主要实体是具有参照行为的帐户。

我尝试了一个电话N:1关系字段new_companystring(lookup)primary Entity是Contact with Referential行为。我得出的结论是,这不是一种有效的方法。如果不正确,请告诉我。

我需要N:N吗?

我已将str_companyid字段添加到表单中。我去了"给联系人打了一个电话"工作流程。在"创建PhoneCall"步骤我添加了动态字段{Company(Contact)}。保存并发布后;我创建了一个电话,但它没有填充。

我尝试过不同的Web Resource JS。我已经在表单属性的onload中添加了JS。

为什么这项工作不容易?我似乎无法使retrieveRecord工作。我也尝试了xmlHttpObject对象,但它返回0.

有人可以帮助我解决我所缺少的问题吗?完成此任务的完整步骤是什么? ![我有下面的截图和我正在运行的代码] [1]

function PopulateCompanyName() 
{

    //get group GUID
    if (Xrm.Page.getAttribute("to").getValue()[0].id != null) {

        var lookup = Xrm.Page.getAttribute("to").getValue();

        alert(lookup[0].id);
        alert(lookup[0].typename);
        alert(lookup[0].name);

        alert(lookup);

        SDK.JQuery.retrieveRecord(lookup[0].id,
            lookup[0].typename,
            "ParentCustomerID",
            null, 
            function (lookup) {
                Xrm.Page.getAttribute("Company").setValue(lookup[0].str_companyid);
         });
    }

    else {
        Xrm.Page.getAttribute("str_companyid").setValue(null);
    }
}

function GetCompany()

///Get lookup ID

{
alert("I am Here");
 var lookupfield = Xrm.Page.getAttribute("to").getValue();
 if (lookupfield != null && lookupfield [0] != null)
 {
 var householdlookupvalue = lookupfield [0].id;
 }
 else
 {
 var householdlookupvalue = " ";
 }
alert("I am here2");
alert(householdlookupvalue);
}

// Prepare variables for a contact to retrieve.
 var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();

// Prepare the SOAP message.
 var xml = ""+
 ""+
 authenticationHeader+
 ""+
 ""+
 "contact"+
 ""+lookupfield [0].id+""+
 ""+
 ""+
 "parentcustomerid"+
 ""+
 ""+
 ""+
 ""+
 "";
alert(xml );
// Prepare the xmlHttpObject and send the request.
 var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
 xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
 xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
 xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
 xHReq.setRequestHeader("Content-Length", xml.length);
 xHReq.send(xml);

// Capture the result.
 var resultXml = xHReq.responseXML;
alert("at results");
var errorCount = resultXml.selectNodes('//error').length;
alert("errorCount " + errorCount); ////////////////////////////////////returns 0; it shouldn't
alert("After the result XML "+resultXml .toString() + " ::::");

// Check for errors.
 var errorCount = resultXml.selectNodes('//error').length;
 if (errorCount != 0)
 {

}
 // Display the retrieved value.
 else
 {

//Create an array to set as the DataValue for the lookup control.
 var lookupData = new Array();

//Create an Object add to the array.
 var lookupItem= new Object();

//Set the id, typename, and name properties to the object.
 lookupItem.id = resultXml.selectSingleNode("//q1:parentcustomerid").nodeTypedValue;
 lookupItem.entityType = 'account';
 lookupItem.name = resultXml.selectSingleNode("//q1:parentcustomerid").getAttribute("name");
 // Add the object to the array.
 lookupData[0] = lookupItem;
alert(lookupitem.name)
// Set the value of the lookup field to the value of the array.
 Xrm.Page.getAttribute("str_companyid").setValue(lookupData);
 }


    var contact = new Array();
    contact = Xrm.Page.getAttribute("to").getValue();
alert("I am here");
alert(contact);
    if (contact == null || contact[0].entityType != "contact" || contact.length > 1) {
        return;
    }
alert("inside if")
    var serverUrl = Xrm.Page.context.getClientUrl();
    var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/ContactSet?$select=ParentCustomerId&$filter=ContactId eq guid'" + contact[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 () {
        GetContactData(this);
    };
    retrieveReq.send();
}

1 个答案:

答案 0 :(得分:0)

我在其他论坛回复,但我在这里作为参考和更多技术细节发布。 to实体的phonecall属性是partylist类型,这意味着它可以处理来自不同实体的多个记录。在您的情况下,如果联系人在parentcustomerid字段内,您希望获得父帐户(字段to)。

您的第一个代码包含相同的拼写错误并使用msdn Retrieve示例,第二个代码使用CRM 4.0端点,它们仍然在CRM 2011中受支持,但最好避免它们,以便您可以使用REST端点代替SOAP的一个。

您的案例中的工作代码可以是:

function setToParentAccount() {
    // set only if to Field (Recipient) has 1 record and is a contact
    if (Xrm.Page.getAttribute("to").getValue() != null) {
        var recipient = Xrm.Page.getAttribute("to").getValue();
        if (recipient.length == 1 && recipient[0].entityType == "contact") {

            var contactId = recipient[0].id;
            var serverUrl;
            if (Xrm.Page.context.getClientUrl !== undefined) {
                serverUrl = Xrm.Page.context.getClientUrl();
            } else {
                serverUrl = Xrm.Page.context.getServerUrl();
            }

            var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; 
            var contactRequest = new XMLHttpRequest();
            contactRequest.open("GET", ODataPath + "/ContactSet(guid'" + contactId + "')", false); 
            contactRequest.setRequestHeader("Accept", "application/json"); 
            contactRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");

            contactRequest.send();
            if (contactRequest.status === 200) {
                var retrievedContact = JSON.parse(contactRequest.responseText).d;
                var parentAccount = retrievedContact.ParentCustomerId;

                if (parentAccount.Id != null && parentAccount.LogicalName == "account") {
                    var newParentAccount = new Array();
                    newParentAccount[0] = new Object();
                    newParentAccount[0].id = parentAccount.Id;
                    newParentAccount[0].name = parentAccount.Name;
                    newParentAccount[0].entityType = parentAccount.LogicalName;
                    Xrm.Page.getAttribute("str_companyid").setValue(newParentAccount);
                } else {
                    Xrm.Page.getAttribute("str_companyid").setValue(null);
                }
            } else {
                    alert("error");
            }
        } else {
            Xrm.Page.getAttribute("str_companyid").setValue(null);
        }
    }
}

to字段的OnLoad事件和OnChange事件中调用。