Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var formContext = null;
- var parentLocationId = null;
- var stage1 = "Investigation Request";
- var stage2 = "Terms Of Reference";
- var currentStage = "";
- function onLoadEmailSender(executionContext) {
- var formContext = executionContext.getFormContext();
- if (formContext.getAttribute("regardingobjectid") !== null) {
- var regardingobject = formContext
- .getAttribute("regardingobjectid")
- .getValue();
- if (regardingobject !== null) {
- var objectId = regardingobject[0].id.replace("{", "").replace("}", "");
- Xrm.WebApi.retrieveRecord(
- regardingobject[0].entityType,
- objectId,
- "?$select=_apuk_parentorganisationid_value"
- ).then(
- function success(res) {
- if (res.hasOwnProperty("_apuk_parentorganisationid_value")) {
- Xrm.WebApi.retrieveRecord(
- res[
- "_apuk_parentorganisationid_value@Microsoft.Dynamics.CRM.lookuplogicalname"
- ],
- res._apuk_parentorganisationid_value,
- "?$select=_apuk_emailsender_value"
- ).then(
- function sucess(result) {
- if (result._apuk_emailsender_value !== null) {
- var arrVal = [
- {
- entityType:
- result[
- "_apuk_emailsender_value@Microsoft.Dynamics.CRM.lookuplogicalname"
- ],
- id: result._apuk_emailsender_value,
- name:
- result[
- "_apuk_emailsender_value@OData.Community.Display.V1.FormattedValue"
- ]
- }
- ];
- formContext.getAttribute("from").setValue(arrVal);
- }
- },
- function error(error) {
- console.log(error);
- }
- );
- }
- },
- function error(err) {
- console.log(err);
- }
- );
- }
- }
- }
- function onLoadInvestigation(executionContext) {
- formContext = executionContext.getFormContext();
- var gridContext = formContext.getControl("teammembers");
- var gridContextIssue = formContext.getControl("subgrid_investigatedissues");
- formContext.data.process.addOnStageSelected(handleState);
- if (
- formContext.ui.getFormType() === 1 ||
- formContext.ui.getFormType() === 2
- ) {
- if (formContext.ui.getFormType() === 1) {
- setAskEvaKeywords(formContext);
- }
- if (formContext.getAttribute("ownerid") !== null) {
- var owner = formContext.getAttribute("ownerid").getValue();
- Xrm.WebApi.retrieveRecord(
- "systemuser",
- owner[0].id.replace("{", "").replace("}", ""),
- "?$select=_apuk_parentlocationid_value"
- ).then(
- function success(res) {
- if (res._apuk_parentlocationid_value !== null) {
- var arrVal = [
- {
- entityType:
- res[
- "_apuk_parentlocationid_value@Microsoft.Dynamics.CRM.lookuplogicalname"
- ],
- id: res._apuk_parentlocationid_value,
- name:
- res[
- "_apuk_parentlocationid_value@OData.Community.Display.V1.FormattedValue"
- ]
- }
- ];
- formContext
- .getAttribute("apuk_parentorganisationid")
- .setValue(arrVal);
- setHealthCareType();
- }
- },
- function error(err) {
- console.log(err);
- }
- );
- }
- }
- HanldeInvestiaginEvent(formContext, gridContext, gridContextIssue);
- formContext.data.process.addOnStageChange(function () {
- HanldeInvestiaginEvent(formContext, gridContext, gridContextIssue);
- });
- }
- function HanldeInvestiaginEvent(formContext, gridContext, gridContextIssue) {
- var reportTitle;
- var incidentDesc;
- var bgAndContext;
- var selectedRow;
- var stageName;
- var flag = [false, false, false, false, false];
- if (formContext.getAttribute("apuk_investigationtitle") !== null) {
- reportTitle = formContext
- .getAttribute("apuk_investigationtitle")
- .getValue();
- formContext.getAttribute("apuk_investigationtitle").addOnChange(function () {
- stageName = formContext.data.process.getActiveStage().getName();
- reportTitle = formContext
- .getAttribute("apuk_investigationtitle")
- .getValue();
- if (stageName === "Terms Of Reference") {
- if (reportTitle === null) {
- flag[1] = false;
- formContext.ui.setFormNotification(
- "Report Title is required",
- "ERROR",
- "reporttitlerror"
- );
- checkAllIsClear(flag, formContext);
- } else {
- formContext.ui.clearFormNotification("reporttitlerror");
- flag[1] = true;
- checkAllIsClear(flag, formContext);
- }
- }
- });
- }
- if (formContext.getAttribute("apuk_description") !== null) {
- incidentDesc = formContext.getAttribute("apuk_description").getValue();
- formContext.getAttribute("apuk_description").addOnChange(function () {
- stageName = formContext.data.process.getActiveStage().getName();
- incidentDesc = formContext.getAttribute("apuk_description").getValue();
- if (stageName === "Terms Of Reference") {
- if (incidentDesc === null) {
- flag[2] = false;
- formContext.ui.setFormNotification(
- "Incident Description is required",
- "ERROR",
- "incidentdeserror"
- );
- checkAllIsClear(flag, formContext);
- } else {
- formContext.ui.clearFormNotification("incidentdeserror");
- flag[2] = true;
- checkAllIsClear(flag, formContext);
- }
- }
- });
- }
- if (formContext.getAttribute("apuk_backgroundcontext") !== null) {
- bgAndContext = formContext
- .getAttribute("apuk_backgroundcontext")
- .getValue();
- formContext.getAttribute("apuk_backgroundcontext").addOnChange(function () {
- stageName = formContext.data.process.getActiveStage().getName();
- bgAndContext = formContext
- .getAttribute("apuk_backgroundcontext")
- .getValue();
- if (stageName === "Terms Of Reference") {
- if (bgAndContext === null) {
- flag[3] = false;
- formContext.ui.setFormNotification(
- "Background and Context is required",
- "ERROR",
- "bgandcontexterr"
- );
- checkAllIsClear(flag, formContext);
- } else {
- formContext.ui.clearFormNotification("bgandcontexterr");
- flag[3] = true;
- checkAllIsClear(flag, formContext);
- }
- }
- });
- }
- stageName = formContext.data.process.getActiveStage().getName();
- selectedRow = gridContext.getGrid().getTotalRecordCount();
- var numberOfIssueRow = gridContextIssue.getGrid().getTotalRecordCount();
- if (stageName === "Terms Of Reference") {
- var fetchTeamMember =
- '<fetch top="5">' +
- '<entity name="apuk_investigationteammember">' +
- '<attribute name="apuk_name" />' +
- "<filter>" +
- '<condition attribute="apuk_investigationid" operator="eq" value="' +
- formContext.data.entity.getId() +
- '" />' +
- '<condition attribute="statecode" operator="eq" value="0" />' +
- "</filter>" +
- "</entity>" +
- "</fetch>";
- fetchTeamMember = "?fetchXml=" + encodeURIComponent(fetchTeamMember);
- Xrm.WebApi.retrieveMultipleRecords(
- "apuk_investigationteammember",
- fetchTeamMember
- ).then(
- function (result) {
- if (result.entities.length !== 0) {
- formContext.ui.clearFormNotification("teamerror");
- flag[0] = true;
- } else {
- formContext.ui.setFormNotification(
- "Investigation members are required",
- "ERROR",
- "teamerror"
- );
- flag[0] = false;
- }
- },
- function (error) {
- console.log(error);
- }
- );
- var fetchIssue =
- '<fetch top="5">' +
- '<entity name="apuk_investigatedissue">' +
- '<attribute name="apuk_name" />' +
- "<filter>" +
- '<condition attribute="apuk_investigationid" operator="eq" value="' +
- formContext.data.entity.getId() +
- '" />' +
- '<condition attribute="statecode" operator="eq" value="0" />' +
- "</filter>" +
- "</entity>" +
- "</fetch>";
- fetchIssue = "?fetchXml=" + encodeURIComponent(fetchIssue);
- Xrm.WebApi.retrieveMultipleRecords(
- "apuk_investigatedissue",
- fetchIssue
- ).then(
- function (result) {
- debugger;
- if (result.entities.length !== 0) {
- formContext.ui.clearFormNotification("issuerror");
- flag[4] = true;
- } else {
- formContext.ui.setFormNotification(
- "Issues to be Investigated is required",
- "ERROR",
- "issuerror"
- );
- flag[4] = false;
- }
- },
- function (error) {
- console.log(error);
- }
- );
- if (reportTitle === null) {
- formContext.ui.setFormNotification(
- "Report Title is required",
- "ERROR",
- "reporttitlerror"
- );
- flag[1] = false;
- } else {
- formContext.ui.clearFormNotification("reporttitlerror");
- flag[1] = true;
- }
- if (incidentDesc === null) {
- formContext.ui.setFormNotification(
- "Incident Description is required",
- "ERROR",
- "incidentdeserror"
- );
- flag[2] = false;
- } else {
- formContext.ui.clearFormNotification("incidentdeserror");
- flag[2] = true;
- }
- if (bgAndContext === null) {
- formContext.ui.setFormNotification(
- "Background and Context is required",
- "ERROR",
- "bgandcontexterr"
- );
- flag[3] = false;
- } else {
- formContext.ui.clearFormNotification("bgandcontexterr");
- flag[3] = true;
- }
- // setTimeout(function () {
- // checkAllIsClear(flag, formContext);
- // }, 1000);
- }
- gridContext.addOnLoad(function () {
- stageName = formContext.data.process.getActiveStage().getName();
- selectedRow = gridContext.getGrid().getTotalRecordCount();
- if (stageName === "Terms Of Reference") {
- if (selectedRow !== 0) {
- formContext.ui.clearFormNotification("teamerror");
- flag[0] = true;
- checkAllIsClear(flag, formContext);
- } else {
- formContext.ui.setFormNotification(
- "Investigation members are required",
- "ERROR",
- "teamerror"
- );
- flag[0] = false;
- checkAllIsClear(flag, formContext);
- }
- }
- });
- gridContextIssue.addOnLoad(function () {
- debugger;
- stageName = formContext.data.process.getActiveStage().getName();
- numberOfIssueRow = gridContextIssue.getGrid().getTotalRecordCount();
- if (stageName === "Terms Of Reference") {
- if (numberOfIssueRow !== 0) {
- formContext.ui.clearFormNotification("issuerror");
- flag[4] = true;
- checkAllIsClear(flag, formContext);
- } else {
- formContext.ui.setFormNotification(
- "Issues to be Investigated is required",
- "ERROR",
- "issuerror"
- );
- flag[4] = false;
- checkAllIsClear(flag, formContext);
- }
- }
- });
- }
- function checkAllIsClear(flag, formContext) {
- for (var i = 0; i < flag.length; i++) {
- if (flag[i] === false) {
- //formContext.ui.process.setVisible(false);
- formContext.data.save().then(function(){
- formContext.data.process.setStatus("aborted");
- }, function(err, message){
- console.log(message);
- });
- break;
- } else if (i === flag.length - 1) {
- //formContext.ui.process.setVisible(true);
- formContext.data.save().then(function () {
- formContext.data.process.setStatus("active");
- }, function (err, message) {
- console.log(message);
- });
- }
- }
- }
- function setAskEvaKeywords(formContext) {
- var fetchXML =
- "<fetch>" +
- "<entity name='apuk_askevakeywords' >" +
- "<attribute name='apuk_name' />" +
- "<attribute name='apuk_processstage' />" +
- "<filter>" +
- "<condition attribute='apuk_processstage' operator='eq' value='200000000' />" +
- "</filter>" +
- "</entity>" +
- "</fetch>";
- fetchXML = "?fetchXml=" + encodeURI(fetchXML);
- console.log("apuk_askevakeywords");
- Xrm.WebApi.retrieveMultipleRecords("apuk_askevakeywords", fetchXML, 1).then(
- function success(result) {
- for (var i = 0; i < result.entities.length; i++) {
- console.log("apuk_askevakeywords");
- var lookup = [
- {
- entityType: "apuk_askevakeywords",
- id: result.entities[i].apuk_askevakeywordsid,
- name: result.entities[i].apuk_name
- }
- ];
- formContext.getAttribute("apuk_askevakeywordsid").setValue(lookup);
- }
- },
- function (error) {
- console.log("error:", error.message);
- }
- );
- }
- function setLocation(executionContext, isOnChange) {
- if (executionContext != null) {
- formContext = executionContext.getFormContext();
- }
- currentStage = formContext.data.process.getSelectedStage().getName();
- var location = formContext.getControl("header_process_apuk_organisationid");
- var location1 = formContext.getControl(
- "header_process_apuk_organisationid_1"
- );
- var parentLocation = formContext.getAttribute("apuk_parentorganisationid");
- if (parentLocation !== null && parentLocation.getValue() != null) {
- parentLocationId = parentLocation.getValue()[0].id;
- if (typeof isOnChange != "undefined") {
- if (location !== null && currentStage == stage1) {
- location.removePreSearch(filterLocation);
- }
- if (location1 !== null && currentStage == stage2) {
- location1.removePreSearch(filterLocation);
- }
- }
- if (currentStage == stage1) {
- location.addPreSearch(filterLocation);
- } else if (currentStage == stage2) {
- location1.addPreSearch(filterLocation);
- }
- } else {
- if (typeof isOnChange != "undefined") {
- location.removePreSearch(filterLocation);
- location1.removePreSearch(filterLocation);
- }
- }
- }
- function filterLocation() {
- var customFilter =
- "<filter><condition attribute = 'parentaccountid' operator = 'eq' value = '" +
- parentLocationId +
- "'/></filter>";
- if (currentStage == stage1) {
- var location = formContext.getControl("header_process_apuk_organisationid");
- location.addCustomFilter(customFilter);
- } else if (currentStage == stage2) {
- var location1 = formContext.getControl(
- "header_process_apuk_organisationid_1"
- );
- location1.addCustomFilter(customFilter);
- }
- }
- function handleState() {
- setLocation();
- }
- function setHealthCareType() {
- if (formContext.ui.getFormType() === 1) {
- var parentLocation = formContext.getAttribute("apuk_parentorganisationid");
- if (parentLocation !== null && parentLocation.getValue() != null) {
- var parentLocationId = parentLocation.getValue()[0].id;
- Xrm.WebApi.retrieveRecord(
- "account",
- parentLocationId.replace("{", "").replace("}", ""),
- "?$select=_apuk_healthcaretypeid_value"
- ).then(
- function success(data) {
- if (data._apuk_healthcaretypeid_value !== null) {
- var lookup = [
- {
- entityType:
- data[
- "_apuk_healthcaretypeid_value@Microsoft.Dynamics.CRM.lookuplogicalname"
- ],
- id: data._apuk_healthcaretypeid_value,
- name:
- data[
- "_apuk_healthcaretypeid_value@OData.Community.Display.V1.FormattedValue"
- ]
- }
- ];
- formContext.getAttribute("apuk_healthcaretypeid").setValue(lookup);
- }
- },
- function error(err) {
- console.log(err);
- }
- );
- }
- }
- }
- function onLoadEvidence(executionContext) {
- formContext = executionContext.getFormContext();
- var gridContext = formContext.getControl("evidenceitems");
- gridContext.addOnLoad(function () {
- var totalRows = gridContext.getGrid().getTotalRecordCount();
- if (totalRows !== 0) {
- formContext.ui.clearFormNotification("evidencerror");
- formContext.ui.process.setVisible(true);
- } else {
- formContext.ui.setFormNotification(
- "Evidence item is required",
- "ERROR",
- "evidencerror"
- );
- formContext.ui.process.setVisible(false);
- }
- });
- }
- function onLoadChronologyAnalysis(executionContext) {
- formContext = executionContext.getFormContext();
- var flag = [false, false, false];
- var overallConclusion;
- sessionStorage.setItem("flagArr", JSON.stringify(flag));
- if (formContext.getAttribute("apuk_overallconclusion") !== null) {
- overallConclusion = formContext
- .getAttribute("apuk_overallconclusion")
- .getValue();
- formContext.getAttribute("apuk_overallconclusion").addOnChange(function () {
- overallConclusion = formContext
- .getAttribute("apuk_overallconclusion")
- .getValue();
- if (overallConclusion === null) {
- flag = JSON.parse(sessionStorage.getItem("flagArr"));
- flag[0] = false;
- sessionStorage.setItem("flagArr", JSON.stringify(flag));
- formContext.ui.setFormNotification(
- "Overall Conclusion is required",
- "ERROR",
- "overallerr"
- );
- checkAllIsClear(
- JSON.parse(sessionStorage.getItem("flagArr")),
- formContext
- );
- } else {
- flag = JSON.parse(sessionStorage.getItem("flagArr"));
- flag[0] = true;
- sessionStorage.setItem("flagArr", JSON.stringify(flag));
- formContext.ui.clearFormNotification("overallerr");
- checkAllIsClear(
- JSON.parse(sessionStorage.getItem("flagArr")),
- formContext
- );
- }
- });
- }
- var fetchXml =
- "<fetch top='5'>" +
- "<entity name='apuk_caremanagementproblem'>" +
- "<attribute name='apuk_eventid' />" +
- "<filter>" +
- "<condition attribute='apuk_chronologyanalysisid' operator='eq' value='" +
- formContext.data.entity.getId() +
- "' /></filter><order attribute='createdon' descending='true' /></entity></fetch>";
- fetchXml = "?fetchXml=" + encodeURIComponent(fetchXml);
- Xrm.WebApi.retrieveMultipleRecords(
- "apuk_caremanagementproblem",
- fetchXml
- ).then(
- function (result) {
- if (result.entities.length !== 0) {
- parent.Xrm.Page.ui.clearFormNotification("cmperror");
- flag = JSON.parse(sessionStorage.getItem("flagArr"));
- flag[2] = true;
- sessionStorage.setItem("flagArr", JSON.stringify(flag));
- for (var i = 0; i < flag.length; i++) {
- if (flag[i] === false) {
- parent.parent.Xrm.Page.ui.process.setVisible(false);
- break;
- } else if (i === flag.length - 1) {
- parent.parent.Xrm.Page.ui.process.setVisible(true);
- }
- }
- } else {
- parent.Xrm.Page.ui.setFormNotification(
- "CMP is required",
- "ERROR",
- "cmperror"
- );
- flag = JSON.parse(sessionStorage.getItem("flagArr"));
- flag[2] = false;
- sessionStorage.setItem("flagArr", JSON.stringify(flag));
- for (var i = 0; i < flag.length; i++) {
- if (flag[i] === false) {
- parent.parent.Xrm.Page.ui.process.setVisible(false);
- break;
- } else if (i === flag.length - 1) {
- parent.parent.Xrm.Page.ui.process.setVisible(true);
- }
- }
- }
- },
- function (error) {
- console.log(error);
- }
- );
- if (overallConclusion === null) {
- formContext.ui.setFormNotification(
- "Overall Conclusion is required",
- "ERROR",
- "overallerr"
- );
- flag = JSON.parse(sessionStorage.getItem("flagArr"));
- flag[0] = false;
- sessionStorage.setItem("flagArr", JSON.stringify(flag));
- } else {
- debugger;
- formContext.ui.clearFormNotification("overallerr");
- flag = JSON.parse(sessionStorage.getItem("flagArr"));
- flag[0] = true;
- sessionStorage.setItem("flagArr", JSON.stringify(flag));
- }
- }
- function onLoadQualityImprovemntPlan(executionContext) {
- formContext = executionContext.getFormContext();
- var flag = [false, false];
- sessionStorage.setItem("qipFlag", JSON.stringify(flag));
- var gridContext = formContext.getControl("subgrid_tasks");
- var totalRow = gridContext.getGrid().getTotalRecordCount();
- if (totalRow === 0) {
- formContext.ui.setFormNotification(
- "Quality Improvement Plan is required",
- "ERROR",
- "taskerror"
- );
- flag[0] = false;
- sessionStorage.setItem("qipFlag", JSON.stringify(flag));
- } else {
- flag[0] = true;
- sessionStorage.setItem("qipFlag", JSON.stringify(flag));
- formContext.ui.clearFormNotification("taskerror");
- }
- gridContext.addOnLoad(function () {
- totalRow = gridContext.getGrid().getTotalRecordCount();
- if (totalRow === 0) {
- formContext.ui.setFormNotification(
- "Quality Improvement Plan is required",
- "ERROR",
- "taskerror"
- );
- flag = JSON.parse(sessionStorage.getItem("qipFlag"));
- flag[0] = false;
- sessionStorage.setItem("qipFlag", JSON.stringify(flag));
- checkAllIsClear(flag, formContext);
- } else {
- formContext.ui.clearFormNotification("taskerror");
- flag = JSON.parse(sessionStorage.getItem("qipFlag"));
- flag[0] = true;
- sessionStorage.setItem("qipFlag", JSON.stringify(flag));
- checkAllIsClear(flag, formContext);
- }
- });
- }
Add Comment
Please, Sign In to add comment