0

预期行为:用户单击注册按钮并在相关实体中创建记录。到目前为止,这一直有效,但现在我们需要根据当前表单的查找来更新外键。我该如何做到这一点?下面粘贴的代码在浏览器中引发错误“创建记录时出错;错误 - 错误请求”

// JScript source code
CreateButton = function () {
    var fieldTable = crmForm.all.new_registerbutton_d;
    var html = "<TABLE border=0 cellSpacing=0 cellPadding=0><TBODY><TR><TD width=0px>" + fieldTable.innerHTML + "</TD><TD width=200><INPUT style='BACKGROUND-COLOR: #d8e8ff' onclick=Button_OnClick() value='Register' type=button></TD></TR></TBODY></TABLE>";

    fieldTable.innerHTML = html;
    document.all.new_registerbutton.style.display = 'none';
    crmForm.all.new_registerbutton_c.innerText = "";
}

Button_OnClick = function () {

    //var new_se_registration = new Object();
    var invitationName = Xrm.Page.getAttribute("new_name").getValue();
    //new_se_registration.new_name = invitationName;

    var invitationGUID = Xrm.Page.data.entity.getId().replace('{', '').replace('}', '');
    alert(invitationGUID);
    var value = new Array();
    value[0] = new Object();
    value[0].id = invitationGUID;
    value[0].name = invitationName;
    //var invitation = {
    //id: invitationGUID, name: invitationName, entityType: "new_se_registration"
    //};
    //var invitation= { Id: invitationGUID, LogicalName: "new_se_registration", Name: invitationName };
    //value[0].entityType = Xrm.Page.data.entity.getEntityType();
    //new_se_registration.new_new_se_invitation_new_se_registid.setValue([{ id: invitationGUID, name: invitationName, entityType: "new_se_registration"}]);
    //new_se_registration.new_new_se_invitation_new_se_registid = invitation;

    //new_se_registration.new_eventname= Xrm.Page.getAttribute("new_eventname").getValue();
    //new_invitationid is the Event id
    var lookupItem = new Array;
    lookupItem = crmForm.all.new_invitationid.DataValue;
    var eventname = lookupItem[0].name;
    //new_se_registration.new_eventname = eventname;

    lookupItem = crmForm.all.new_invitationid.DataValue;

    var new_se_registration = {

        new_new_se_invitation_new_se_registid: {
        __metadata: { type: "Microsoft.Crm.Sdk.Data.Services.EntityReference" },
        Id: invitationGUID,
        LogicalName: invitationName
        },

        new_name: invitationName,
        new_eventname: eventname
    };

    //alert (  eventname);

    // end Ek - adding event name

    //deleteRecord(new_se_registration.new_name, "new_se_registrationSet");

    var reg = Xrm.Page.getAttribute("new_registered").getValue();



    if (reg == null) {
        reg = "N";
    }


    if (reg != "Y") {

        //Xrm.Page.getAttribute("new_registered").setValue("Y");
        //alert(reg);
        //return;

        createRecord(new_se_registration, "new_se_registrationSet", RegisterCompleted, null);
        Xrm.Page.getAttribute("new_registered").setValue("Y");
        //Xrm.Page.data.entity.attributes.get("new_registered").setSubmitMode("always");
        //Xrm.Page.data.entity.save(); 
    }
    else {
        alert("\"" + Xrm.Page.getAttribute("new_name").getValue() + "\" has been registered already.");
    }
}


function RegisterCompleted(data, textStatus, XmlHttpRequest) {
    var new_se_registration = data;
    Xrm.Page.data.entity.save();
    alert("\"" + Xrm.Page.getAttribute("new_name").getValue() + "\" has been registered successfully.");
}

function checkDuplicate() {
    alert("test");
}

function deleteRecord(id, odataSetName) {

    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";

    $.ajax({

        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')",
        beforeSend: function (XMLHttpRequest) {

            //XMLHttpRequest.setRequestHeader("Accept", "application/json");
            XMLHttpRequest.setRequestHeader("X-HTTP-Method", "DELETE");
        },

        success: function (data, textStatus, XmlHttpRequest) {
            alert("Record deleted successfully!");
        },

        error: function (XmlHttpRequest, textStatus, errorThrown) {
            alert("Error while deletion – " + errorThrown);
        }
    });
}


function createRecord(entityObject, odataSetName, successCallback, errorCallback) {

    var jsonEntity = window.JSON.stringify(entityObject);
    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";

    $.ajax({

        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName,
        data: jsonEntity,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },

        success: function (data, textStatus, XmlHttpRequest) {
            if (successCallback) {
                successCallback(data.d, textStatus, XmlHttpRequest);
            }
        },

        error: function (XmlHttpRequest, textStatus, errorThrown) {

            if (errorCallback)
                errorCallback(XmlHttpRequest, textStatus, errorThrown);
            else
                alert("Error on the creation of record; Error – " + errorThrown);
        }

    });

}
4

1 回答 1

1

解决了。区分大小写。为 new_new_se_invitation_new_se_registId 使用了错误的大小写。

var new_se_registration = {

    new_new_se_invitation_new_se_regist**I**d: {
    __metadata: { type: "Microsoft.Crm.Sdk.Data.Services.EntityReference" },
    Id: invitationGUID,
    LogicalName: invitationName
    },

    new_name: invitationName,
    new_eventname: eventname
};
于 2012-06-19T18:34:17.163 回答