CRM2011 concurrency問題及解決方案

CRM2011對刪除和添加操做會自動作判斷,好比A打開紀錄x,B也打開紀錄x,而後B刪除了紀錄x,A去更新的話就會有提示。更新的話是後者覆蓋前者,好比A打開紀錄x,B打開紀錄x,而後B把記錄x中的字段y更新成了3,而後點保存,A把字段y更新成了5保存,最後5會覆蓋3。這篇日誌解決的問題就是當A改爲5想保存的時候提示記錄x已在某個時間點被某人更新過了,若是繼續保存就會覆蓋記錄,不然放棄保存。jquery

用js完成整個驗證,而後註冊form的onload和onsave達到目的,固然也要添加jquery和json2兩個js類庫ajax

上js先json

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 + "\n"+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+"\nAre 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中的配置app

 

 

而後選中validateConcurrency點擊Edit,勾選Pass execution context as first parameterasync

就這些配置,能夠實現排他機制咯。ui

相關文章
相關標籤/搜索