CRM2011对删除和添加操作会自动做判断,比如A打开纪录x,B也打开纪录x,然后B删除了纪录x,A去更新的话就会有提示。更新的话是后者覆盖前者,比如A打开纪录x,B打开纪录x,然后B把记录x中的字段y更新成了3,然后点保存,A把字段y更新成了5保存,最后5会覆盖3。这篇日志解决的问题就是当A改成5想保存的时候提示记录x已在某个时间点被某人更新过了,如果继续保存就会覆盖记录,否则放弃保存。
用js完成整个验证,然后注册form的onload和onsave达到目的,当然也要添加jquery和json2两个js类库
上js先
var previousModificationDateOnLoad; //var serverUrl = Xrm.Page.context.getServerUrl(); var serverUrl = document.location.protocol + "//" + document.location.host + "/" + Xrm.Page.context.getOrgUniqueName(); var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc"; var recordId; var entityName; function getPreviousModificationDate() { recordId = Xrm.Page.data.entity.getId().replace('{','').replace('}',''); entityName = Xrm.Page.data.entity.getEntityName() + "Set"; previousModificationDateOnLoad = new Date(); retrieveRecord(recordId,entityName, function(data){ previousModificationDateOnLoad = data.ModifiedOn; previousModificationDateOnLoad = new Date(parseInt(//Date((d+).*/.exec(previousModificationDateOnLoad)[1])); },null, false); //alert(previousModificationDateOnLoad); } function validateConcurrency(ExecutionObj) { if(Xrm.Page.ui.getFormType()==1) { return; } var previousModificationDateOnSave, previousModifiedByOnSave; retrieveRecord(recordId,entityName, function(data){ previousModificationDateOnSave = data.ModifiedOn; //alert(previousModificationDateOnSave); previousModifiedByOnSave = data.ModifiedBy.Name; //alert(previousModifiedByOnSave); //change timestamp to date format previousModificationDateOnSave = new Date(parseInt(//Date((d+).*/.exec(previousModificationDateOnSave)[1])); //alert(previousModificationDateOnLoad + " "+previousModificationDateOnSave) if(previousModificationDateOnLoad < previousModificationDateOnSave) { var continueSave = confirm("The record was recently modified by "+previousModifiedByOnSave+ " On " + previousModificationDateOnSave + ".If you chose to save this record you will overwrite the recent changes made by "+ previousModifiedByOnSave+" Are you sure you want to save this record?"); if(!continueSave) { ExecutionObj.getEventArgs().preventDefault(); event.returnValue = false; } } //previousModificationDateOnLoad = previousModificationDateOnSave; },null, false); } function retrieveRecord(id, odataSetName,successCallback, errorCallback, aSync) { if(aSync==null || aSync==undefined) { aSync = true; } //alert(serverUrl +ODATA_ENDPOINT +"/"+odataSetName+"(guid'"+id+"')"); $.ajax({ type:"GET", contentType:"application/json;charset=utf-8", datatype:"json", async:aSync, url:serverUrl +ODATA_ENDPOINT +"/"+odataSetName+"(guid'"+id+"')", 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){ alert("error"); if(errorCallback){ errorCallback(XmlHttpRequest,textStatus,errorThrown); } } }); }
CRM中的配置
然后选中validateConcurrency点击Edit,勾选Pass execution context as first parameter
就这些配置,可以实现排他机制咯。