function CheckUserRole() {
debugger;
var currentUserRoles = Xrm.Page.context.getUserRoles();
for (var i = 0; i < currentUserRoles.length; i++) {
var userRoleId = currentUserRoles[i];
userRoleName = GetRoleName(userRoleId);
return userRoleName;
}
return false;
}
function GetRoleName(roleId) {
debugger;
var roleName = null;
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/roles(" + roleId + ")?$select=name", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
roleName = result["name"];
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
return roleName;
}
function submitForApproval() {
debugger;
var status = Xrm.Page.getAttribute("pcl_status_expensemanagement");
var userRoleName = CheckUserRole();
if (Xrm.Page.getAttribute("pcl_expensesubmit").getValue() == 0) {
if (userRoleName == "Technical Officer") {
Xrm.Utility.confirmDialog("Do you want to submit the record to Reporting Manager for approval? If Yes, then Please click on Ok.", function () {
Xrm.Page.getAttribute("pcl_reviewvp").setValue(false);
Xrm.Page.getAttribute("pcl_reviewfinance").setValue(false);
Xrm.Page.getAttribute("pcl_reviewreporting").setValue(false);
// Xrm.Utility.alertDialog("Record has been assigned to Reporting Manager for approval");
Xrm.Page.getAttribute("pcl_expensesubmit").setValue(true);
status.setValue(0);
status.setSubmitMode("always");
Xrm.Page.data.entity.save("save");
SaveAndRefresh();
}, function () {
//Cancel button logic
Xrm.Page.getAttribute("pcl_expensesubmit").setValue(false);
//status.setValue(null);
});
}
else {
Xrm.Utility.alertDialog("Current user doesn't have sufficient privileges to Submit the record.");
}
}
else {
Xrm.Utility.alertDialog("Message: Record is already submitted.");
}
}
function Approved() {
debugger;
var status = Xrm.Page.getAttribute("pcl_status_expensemanagement");
var userRoleName = CheckUserRole();
if (Xrm.Page.getAttribute("pcl_reportingapproval").getValue() == 0 || Xrm.Page.getAttribute("pcl_vpapproval_expns").getValue() == 0 || Xrm.Page.getAttribute("pcl_financeapproval").getValue() == 0 || Xrm.Page.getAttribute("pcl_rejectreporting").getValue() == 1 || Xrm.Page.getAttribute("pcl_rejectvp").getValue() == 1 || Xrm.Page.getAttribute("pcl_rejectfinance").getValue() == 1) {
if (userRoleName == "Dy Manager Marketing" || userRoleName == "Manager Marketing") {
Xrm.Utility.confirmDialog("Do you want to assign the record to Regional V.P for approval? If Yes, then Please click on Ok.", function () {
setLookupField("ownerid", "pcl_approvedby");
setLookupNull("pcl_rejectedby");
//Xrm.Utility.alertDialog("Record has been assigned to Regional Head for approval");
Xrm.Page.getAttribute("pcl_reportingapproval").setValue(true);
status.setValue(1);
status.setSubmitMode("always");
Xrm.Page.data.entity.save("save");
SaveAndRefresh();
}, function () {
//Cancel button logic
Xrm.Page.getAttribute("pcl_reportingapproval").setValue(false);
//status.setValue(null);
});
}
else if (userRoleName == "Vice President") {
Xrm.Utility.confirmDialog("Do you want to assign the record to Finance Officer for approval? If Yes, then Please click on Ok.", function () {
setLookupField("ownerid", "pcl_approvedby");
setLookupNull("pcl_rejectedby");
//Xrm.Utility.alertDialog("Record has been assigned to Sales Account for approval");
Xrm.Page.getAttribute("pcl_vpapproval_expns").setValue(true);
status.setValue(1);
status.setSubmitMode("always");
Xrm.Page.data.entity.save("save");
SaveAndRefresh();
}, function () {
//Cancel button logic
Xrm.Page.getAttribute("pcl_vpapproval_expns").setValue(false);
//status.setValue(null);
});
}
else if (userRoleName == "Finance Officer") {
Xrm.Utility.confirmDialog("Do you want to approve the record? If Yes, then Please click on Ok.", function () {
setLookupField("ownerid", "pcl_approvedby");
setLookupNull("pcl_rejectedby");
//Xrm.Utility.alertDialog("Record has been approved by the Sales Account");
Xrm.Page.getAttribute("pcl_financeapproval").setValue(true);
status.setValue(1);
status.setSubmitMode("always");
Xrm.Page.data.entity.save("save");
//lock all fields.
Xrm.Page.ui.controls.forEach(function (control) {
if (control.setDisabled) {
control.setDisabled(true);
}
});
SaveAndRefresh();
}, function () {
//Cancel button logic
Xrm.Page.getAttribute("pcl_financeapproval").setValue(false);
//status.setValue(null);
});
}
else {
Xrm.Utility.alertDialog("Current user doesn't have sufficient privileges to Approve the record.");
}
}
else {
Xrm.Utility.alertDialog("Message: Record is already approved.");
}
}
function rejected() {
var status = Xrm.Page.getAttribute("pcl_status_expensemanagement");
var userRoleName = CheckUserRole();
if (Xrm.Page.getAttribute("pcl_reportingapproval").getValue() == 0 || Xrm.Page.getAttribute("pcl_vpapproval_expns").getValue() == 0 || Xrm.Page.getAttribute("pcl_financeapproval").getValue() == 0 || Xrm.Page.getAttribute("pcl_rejectreporting").getValue() == 0 || Xrm.Page.getAttribute("pcl_rejectvp").getValue() == 0 || Xrm.Page.getAttribute("pcl_rejectfinance").getValue() == 0) {
if (userRoleName == "Dy Manager Marketing" || userRoleName == "Manager Marketing") {
Xrm.Utility.confirmDialog("Do you want to reject the record? If Yes, then Please click on Ok.", function () {
setLookupField("ownerid", "pcl_rejectedby");
setLookupNull("pcl_approvedby");
//Xrm.Utility.alertDialog("Record has been assigned to Technical Officer for rejected");
Xrm.Page.getAttribute("pcl_rejectreporting").setValue(true);
status.setValue(2);
status.setSubmitMode("always");
Xrm.Page.data.entity.save("save");
Xrm.Page.ui.controls.forEach(function (control) {
if (control.setDisabled) {
control.setDisabled(false);
}
});
SaveAndRefresh();
}, function () {
//Cancel button logic
Xrm.Page.getAttribute("pcl_rejectreporting").setValue(false);
//status.setValue(null);
});
}
else if (userRoleName == "Vice President") {
Xrm.Utility.confirmDialog("Do you want to reject the record? If Yes, then Please click on Ok.", function () {
setLookupField("ownerid", "pcl_rejectedby");
setLookupNull("pcl_approvedby");
//Xrm.Utility.alertDialog("Record has been assigned to Technical Officer for rejected");
Xrm.Page.getAttribute("pcl_rejectvp").setValue(true);
status.setValue(2);
status.setSubmitMode("always");
Xrm.Page.data.entity.save("save");
Xrm.Page.ui.controls.forEach(function (control) {
if (control.setDisabled) {
control.setDisabled(false);
}
});
SaveAndRefresh();
}, function () {
//Cancel button logic
Xrm.Page.getAttribute("pcl_rejectvp").setValue(false);
//status.setValue(null);
});
} else if (userRoleName == "Finance Officer") {
Xrm.Utility.confirmDialog("Do you want to reject the record? If Yes, then Please click on Ok.", function () {
setLookupField("ownerid", "pcl_rejectedby");
setLookupNull("pcl_approvedby");
//Xrm.Utility.alertDialog("Record has been assigned to Technical Officer for rejected");
Xrm.Page.getAttribute("pcl_rejectfinance").setValue(true);
status.setValue(2);
status.setSubmitMode("always");
Xrm.Page.data.entity.save("save");
Xrm.Page.ui.controls.forEach(function (control) {
if (control.setDisabled) {
control.setDisabled(false);
}
});
SaveAndRefresh();
}, function () {
//Cancel button logic
Xrm.Page.getAttribute("pcl_rejectfinance").setValue(false);
//status.setValue(null);
});
}
else {
Xrm.Utility.alertDialog("Current user doesn't have sufficient privileges to Reject the record.");
Xrm.Page.getAttribute("pcl_rejectedremarks").setRequiredLevel(none);
}
}
else {
Xrm.Utility.alertDialog("Message: Record is already rejected.");
}
}
function SaveAndRefresh() {
try {
setTimeout(function () {
var id = Xrm.Page.data.entity.getId();
Xrm.Page.data.save();
Xrm.Utility.openEntityForm("pcl_expensemanagement", id);
}, 1000);
}
catch (e) {
Xrm.Utility.alertDialog("Error: " + (e.message || e.description));
}
}
function local_setCode() {
setEmpCode("pcl_local_expenseid", "pcl_name");
}
function outstation_setCode() {
setEmpCode("pcl_outstation_expenseid", "pcl_name");
}
function setEmpCode(fieldSchema, targetFieldSchema) {
var selectedText = Xrm.Page.getAttribute(fieldSchema);
if (selectedText != null) {
var selectedTextValue = selectedText.getValue();
if (selectedTextValue != null) {
var Name = selectedTextValue[0].name;
Xrm.Page.getAttribute(targetFieldSchema).setValue(Name);
}
}
}
// Functionality : To Hide and Show the Grids
function hideGrids() {
var isCrmForMobile = (Xrm.Page.context.client.getClient() == "Mobile")
if (isCrmForMobile) {
if (Xrm.Page.getAttribute("pcl_travelplantype") != undefined && Xrm.Page.getAttribute("pcl_travelplantype").getValue() != null) {
var travelplanstage = Xrm.Page.getAttribute("pcl_travelplantype").getValue();
if (travelplanstage == 0) {
Xrm.Page.ui.tabs.get("domestic_tab").setVisible(false);
Xrm.Page.ui.tabs.get("outstation_tab").setVisible(false);
}
var travelplanstage = Xrm.Page.getAttribute("pcl_travelplantype").getValue();
if (travelplanstage == 1) {
Xrm.Page.ui.tabs.get("domestic_tab").setVisible(true);
Xrm.Page.ui.tabs.get("outstation_tab").setVisible(false);
}
var travelplanstage = Xrm.Page.getAttribute("pcl_travelplantype").getValue();
if (travelplanstage == 2) {
Xrm.Page.ui.tabs.get("domestic_tab").setVisible(false);
Xrm.Page.ui.tabs.get("outstation_tab").setVisible(true);
}
}
}
else if (!isCrmForMobile) {
if (Xrm.Page.getAttribute("pcl_travelplantype") != undefined && Xrm.Page.getAttribute("pcl_travelplantype").getValue() != null) {
var travelplanstage = Xrm.Page.getAttribute("pcl_travelplantype").getValue();
if (travelplanstage == 0) {
Xrm.Page.ui.tabs.get("domestic_tab").setVisible(false);
Xrm.Page.ui.tabs.get("outstation_tab").setVisible(false);
}
else if (travelplanstage == 1) {
Xrm.Page.ui.tabs.get("domestic_tab").setVisible(true);
Xrm.Page.ui.tabs.get("outstation_tab").setVisible(false);
}
else if (travelplanstage == 2) {
Xrm.Page.ui.tabs.get("domestic_tab").setVisible(false);
Xrm.Page.ui.tabs.get("outstation_tab").setVisible(true);
}
}
}
}
function setLookupField(fieldSchema, targetFieldSchema) {
var selectedText = Xrm.Page.getAttribute(fieldSchema);
if (selectedText != null) {
var selectedTextValue = selectedText.getValue();
if (selectedTextValue != null) {
var Id = selectedTextValue[0].id;
var Name = selectedTextValue[0].name;
var Type = selectedTextValue[0].entityType;
Xrm.Page.getAttribute(targetFieldSchema).setValue(selectedTextValue);
Xrm.Page.getAttribute(targetFieldSchema).setSubmitMode("always");
}
}
}
function setLookupNull(lookupAttribute) {
var lookupObject = Xrm.Page.getAttribute(lookupAttribute);
if (lookupObject != null) {
Xrm.Page.getAttribute(lookupAttribute).setValue(null);
}
}
///Functional review to salesperson
function Review() {
var status = Xrm.Page.getAttribute("pcl_status_expensemanagement");
var userRoleName = CheckUserRole();
if (Xrm.Page.getAttribute("pcl_reportingapproval").getValue() == 0 || Xrm.Page.getAttribute("pcl_vpapproval_expns").getValue() == 0 || Xrm.Page.getAttribute("pcl_reviewreporting").getValue() == 0 || Xrm.Page.getAttribute("pcl_reviewvp").getValue() == 0 || Xrm.Page.getAttribute("pcl_reviewfinance").getValue() == 0 || Xrm.Page.getAttribute("pcl_financeapproval").getValue() == 0) {
if (userRoleName == "Dy Manager Marketing" || userRoleName == "Manager Marketing") {
Xrm.Utility.confirmDialog("Do you want to review the record? If Yes, then Please click on Ok.", function () {
setLookupField("ownerid", "pcl_reviewedbyid");
setLookupNull("pcl_approvedby");
Xrm.Page.getAttribute("pcl_reportingapproval").setValue(false);
Xrm.Page.getAttribute("pcl_expensesubmit").setValue(false);
//Xrm.Utility.alertDialog("Record has been assigned to Technical Officer for review");
Xrm.Page.getAttribute("pcl_reviewreporting").setValue(true);
status.setValue(4);
status.setSubmitMode("always");
Xrm.Page.data.entity.save("save");
Xrm.Page.ui.controls.forEach(function (control) {
if (control.setDisabled) {
control.setDisabled(false);
}
});
SaveAndRefresh();
}, function () {
//Cancel button logic
Xrm.Page.getAttribute("pcl_reviewreporting").setValue(false);
//status.setValue(null);
});
}
else if (userRoleName == "Vice President") {
Xrm.Utility.confirmDialog("Do you want to review the record? If Yes, then Please click on Ok.", function () {
setLookupField("ownerid", "pcl_reviewedbyid");
setLookupNull("pcl_approvedby");
Xrm.Page.getAttribute("pcl_reportingapproval").setValue(false);
Xrm.Page.getAttribute("pcl_expensesubmit").setValue(false);
Xrm.Page.getAttribute("pcl_vpapproval_expns").setValue(false);
//Xrm.Utility.alertDialog("Record has been assigned to Technical Officer for review");
Xrm.Page.getAttribute("pcl_reviewvp").setValue(true);
status.setValue(4);
status.setSubmitMode("always");
Xrm.Page.data.entity.save("save");
Xrm.Page.ui.controls.forEach(function (control) {
if (control.setDisabled) {
control.setDisabled(false);
}
});
SaveAndRefresh();
}, function () {
//Cancel button logic
Xrm.Page.getAttribute("pcl_reviewvp").setValue(false);
//status.setValue(null);
});
} else if (userRoleName == "Finance Officer") {
Xrm.Utility.confirmDialog("Do you want to review the record? If Yes, then Please click on Ok.", function () {
setLookupField("ownerid", "pcl_reviewedbyid");
setLookupNull("pcl_approvedby");
Xrm.Page.getAttribute("pcl_reportingapproval").setValue(false);
Xrm.Page.getAttribute("pcl_expensesubmit").setValue(false);
Xrm.Page.getAttribute("pcl_vpapproval_expns").setValue(false);
Xrm.Utility.alertDialog("Record has been assigned to Technical Officer for review");
Xrm.Page.getAttribute("pcl_reviewfinance").setValue(true);
status.setValue(4);
status.setSubmitMode("always");
Xrm.Page.data.entity.save("save");
Xrm.Page.ui.controls.forEach(function (control) {
if (control.setDisabled) {
control.setDisabled(false);
}
});
SaveAndRefresh();
}, function () {
//Cancel button logic
Xrm.Page.getAttribute("pcl_reviewfinance").setValue(false);
//status.setValue(null);
});
}
else {
Xrm.Utility.alertDialog("Current user doesn't have sufficient privileges to Review the record.");
Xrm.Page.getAttribute("pcl_rejectedremarks").setRequiredLevel(none);
}
}
else {
Xrm.Utility.alertDialog("Message: Record is already review.");
}
}
////Hide and Show Approve,Review,Reject Buttons on Expense Management
function approveRejectReview() {
debugger;
//Lead Status
var leadStatus = null;
if (Xrm.Page.getAttribute("pcl_status_expensemanagement").getValue() != null && Xrm.Page.getAttribute("pcl_status_expensemanagement").getValue() != undefined) {
leadStatus = Xrm.Page.getAttribute("pcl_status_expensemanagement").getValue();
}
//Current logged in user Id
var contextUser = Xrm.Page.context.getUserId();
var isQualifyingUser = false;
//Finance approval
var financeApproval = false;
if (Xrm.Page.getAttribute("pcl_financeapproval").getValue() != null && Xrm.Page.getAttribute("pcl_financeapproval").getValue() != undefined) {
financeApproval = Xrm.Page.getAttribute("pcl_financeapproval").getValue();
}
//Owner field
var ownerName = null;
if (Xrm.Page.getAttribute("ownerid").getValue() != null && Xrm.Page.getAttribute("ownerid").getValue() != undefined) {
ownerName = Xrm.Page.getAttribute("ownerid").getValue()[0].id;
if (ownerName == contextUser) {
isQualifyingUser = true;
}
}
var userRoleName = CheckUserRole();
if (userRoleName == "Technical Officer") {
if (leadStatus == null || isQualifyingUser || leadStatus == 0 || leadStatus == 2 || leadStatus == 4) {
return false;
}
else {
return true;
}
}
else if (userRoleName == "Dy Manager Marketing") {
if ((leadStatus == 1 || leadStatus == 2 || leadStatus == 4) && !isQualifyingUser) {
return false;
}
else {
return true;
}
}
else if (userRoleName == "Vice President") {
if ((leadStatus == 1 || leadStatus == 2 || leadStatus == 4) && !isQualifyingUser) {
return false;
}
else {
return true;
}
}
else if (userRoleName == "Finance Officer") {
if (financeApproval || leadStatus == 2 || leadStatus == 4) {
return false;
}
else {
return true;
}
}
}
Iam writing this code..