Wednesday, 18 May 2016

Set fields mandatory on the basis of team of user

Javascript/Odata to perform action on the basis of team of  user


function MandatorFieldBasisOfTeam1 () {
    debugger;
var formtype=Xrm.Page.ui.getFormType();
if(formtype!=1)return; //If not a new form then return
var Team1 = "Team1";
var serverUrl = Xrm.Page.context.getClientUrl();
    var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
    oDataEndpointUrl += "TeamSet?$select=TeamId,Name&$filter=Name eq '" + Team1 + "'";
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: oDataEndpointUrl,
        beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
        success: function (data, textStatus, XmlHttpRequest) {
            foundresultsteamids1(data.d);

        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
            alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);

        }
    });
}
function foundresultsteamids1(data) {

    debugger;
    if (data.results.length > 0) {
        for (i = 0; i < data.results.length; i++) {
            var teamname = data.results[i].Name;
            var teamid = data.results[i].TeamId;
            if (teamname != null && teamname != "" && teamid != null && teamid != "") {
                MandatorFieldBasisOfTeam2(teamid, teamname);
            }
        }
    }
}

function MandatorFieldBasisOfTeam2(teamid, teamname) {
    debugger;
    var owner = Xrm.Page.getAttribute("ownerid").getValue();  //User this line if want to perform action for owner of record
//owner=Xrm.Page.context.getUserId();  //User this line if want to perform action for logged in user
    if (owner == null) return;
    var userId = owner[0].id;
    var Team_Name = teamname;
    var Team_id = teamid;
    var serverUrl = Xrm.Page.context.getClientUrl();
    var oDataEndpointUrl2 = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
    oDataEndpointUrl2 += "TeamMembershipSet?$select=TeamId&$filter=SystemUserId eq guid'" + userId + "' and TeamId eq guid'" + Team_id + "'";
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: oDataEndpointUrl2,
        beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
        success: function (data2, textStatus, XmlHttpRequest) {
            foundresults2(data2.d, Team_Name);

        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
            alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
        }
    });
}

function foundresults2(data2, Team_Name)
{
    debugger;
    if (data2.results.length > 0)
{
        {
            if (Team_Name == "Team1")
{
Xrm.Page.getAttribute("yourfieldname").setRequiredLevel("required");
}
        }
    }

}








No comments:

Post a Comment