On Form Load Check the User Team and Lock the form the if the owner of the record team doesn’t matches with Logged in user.

Following code will help you in the following:

  • getting the team names in the system
  • getting the team associated with the user.
  • validating the action if the team name doesn’t match

On Load Function that calls the getCurrentUserTeamNames

function Form_OnLoad_ValidateUserTeam() {
    var userTeams = getCurrentUserTeamNames();
    if (userTeams != null && userTeams.length > 0) {
        if (!isValidateFormAccess(userTeams)) {
            DisableFormFields();
        }
    }
}

function isValidateFormAccess(userTeams) {
    debugger;
    var recordOwner = Xrm.Page.getControl("header_ownerid").getAttribute().getValue();
 
    //var myStringArray = ["Hello", "World"];
    var userTeamsLength = userTeams.length;
    for (var i = 0; i < userTeamsLength; i++) {
        if (userTeams[i] == recordOwner[0].name) {
            return true;
        }   //Do something
    }
    return false;
}

GetCurrentUserTeamNames: this function will return the team names of the user

function getCurrentUserTeamNames() {
    debugger;
    //ajax call to get data from CRM
    var serverUrl = location.protocol + "//" + location.host + "/" + Xrm.Page.context.getOrgUniqueName();
    var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + "TeamSet?$select=Name,TeamId";
    var userTeams = [];
    $.ajax
    (
     {
         type: "GET",
         contentType: "application/json; charset=utf-8",
         datatype: "json",
         async: false,
         url: odataSelect,
         beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
         success: function (data, textStatus, XmlHttpRequest) {
             for (var i = 0; i < data.d.results.length; i++) {                  if (Check(data.d.results[i].TeamId)) {                      //alert("You are associated with team: " + data.d.results[i].Name);                        userTeams.push(data.d.results[i].Name);                  }              }              },            error: function (XmlHttpRequest, textStatus, errorThrown) {              if (XmlHttpRequest && XmlHttpRequest.responseText) {                  alert('OData Select Failed: ' + textStatus + errorThrown + odataSelect);              }          }      }     );       return userTeams; }       function Check(teamid) {       var userId = Xrm.Page.context.getUserId(); //Fetching user Id of current logged in user     var serverUrl = location.protocol + "//" + location.host + "/" + Xrm.Page.context.getOrgUniqueName();     var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + "TeamMembershipSet?$filter=SystemUserId eq guid'" + userId + "' and TeamId eq guid'" + teamid + "'";     var boolFlag = false;     $.ajax     (      {          type: "GET",          contentType: "application/json; charset=utf-8",          datatype: "json",            async: false,          url: odataSelect,            beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },          success: function (data, textStatus, XmlHttpRequest) {              if (data.d.results.length > 0) {
                 //alert(data.d.results[0].TeamId);
                 boolFlag = true;
             }
         },
 
         error: function (XmlHttpRequest, textStatus, errorThrown) {
             if (XmlHttpRequest && XmlHttpRequest.responseText) {
                 alert('OData Select Failed: ' + textStatus + errorThrown + odataSelect);
             }
         }
     }
    );
 
    return boolFlag;
    //return false; //Returing false if the user is not associated with the team
}
 
 
 
function DisableFormFields() {
    disableAttrs();
    RefreshGrids();
}
 
 
 
function RefreshGrids() {
 
    var opportunitySubgrids = Xrm.Page.getControl(function (ctrl, i) {
        if (ctrl.getControlType() == "subgrid") {
            return (ctrl);
        }
        else {
            return false;
        }
    });
 
    opportunitySubgrids.forEach(function (attribute, index) {
        var control = Xrm.Page.getControl(attribute.getName());
        if (control) {
            control.refresh();
        }
    });
 
 
 
}
 
function disableAttrs() {
    debugger;
    Xrm.Page.data.entity.attributes.forEach(function (attribute, index) {
        var control = Xrm.Page.getControl(attribute.getName());
        if (control && (control.getName() != "processid" && control.getName() != "stageid")) {
            control.setDisabled(true);
 
            //notification();
 
        }
    });
    Xrm.Page.ui.setFormNotification("The Form is Locked", "INFORMATION")
}
 
function tabdisable(tabname, disablestatus) {
    var tab = Xrm.Page.ui.tabs.get(tabname);
    if (tab == null) alert("Error: The tab: " + tabname + " is not on the form");
    else {
        var tabsections = tab.sections.get();
        for (var i in tabsections) {
            var secname = tabsections[i].getName();
            sectiondisable(secname, disablestatus);
        }
    }
}   // tabdisable
function sectiondisable(sectionname, disablestatus) {
    var ctrlName = Xrm.Page.ui.controls.get();
    for (var i in ctrlName) {
        var ctrl = ctrlName[i];
        var ctrlSection = ctrl.getParent().getName();
        if (ctrlSection == sectionname) {
            ctrl.setDisabled(disablestatus);
        }
    }
}  // sectiondisable

Please Feel Free to contact me if anyone face issues. The current Code requires that

Jquery and Json must be included in the form

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s