function Copy() { //step 1 查询当前信息 var new_code = Xrm.Page.getAttribute("new_code").getValue();//促销编号 var new_name = Xrm.Page.getAttribute("new_name").getValue();//促销名称 var new_ruletypecode = Xrm.Page.getAttribute("new_ruletypecode").getValue();//规则类型 选项集 //step 2 组合成新的对象 var newRule = new Object(); newRule.new_code = new_code; newRule.new_name = new_name; newRule.new_ruletypecode = { Value: new_ruletypecode }; // 选项集 必须使用这种赋值方法,否则就会报错 //step 3 创建新记录 var jsonEntity = window.JSON.stringify(newRule); var createEntityReq = CreateEntityRecord("new_Entity", false, false); createEntityReq.send(jsonEntity); if (createEntityReq.readyState == 4 /* complete */) { if (createEntityReq.status == 201) { //做想做的事情 } } }
//创建XMLHttpRequest,创建实体信息 function CreateEntityRecord(entityschema, syncmode, isvmode) { if (typeof (syncmode) == "undefined") { syncmode = false; } if (typeof (isvmode) == "undefined") { isvmode = false; } var orguniquename = ""; if (isvmode) { orguniquename = GetGlobalContext().getOrgUniqueName(); //GetGlobalContext function exists in ClientGlobalContext.js.aspx } else { orguniquename = Xrm.Page.context.getOrgUniqueName(); } var serverUrl = ""; if (location.href.toLowerCase().indexOf(orguniquename.toLowerCase() + ".") > 0) { serverUrl = "//" + location.host; } else { serverUrl = "//" + location.host + "/" + orguniquename; } serverUrl = document.location.protocol + serverUrl; var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + entityschema + "Set"; var createEntityReq = new XMLHttpRequest(); createEntityReq.open("POST", ODataPath, syncmode); createEntityReq.setRequestHeader("Accept", "application/json"); createEntityReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); return createEntityReq; }