2012-06-19 51 views
0

預期行爲:用戶單擊註冊按鈕並在相關實體中創建記錄。直到現在,這一直髮展到現在,但現在我們需要根據當前表單上的查找來更新外鍵。我該如何做到這一點?下面的粘貼代碼會在瀏覽器中產生錯誤「創建記錄時出錯;錯誤 - 錯誤請求」如何使用odata json更新外鍵

// 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); 
     } 

    }); 

} 

回答

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 
};