Created
June 10, 2022 09:51
-
-
Save maestrotex/2c9a68f5a5dedf55146a66750fea1559 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var CourseForm = (function () { | |
var SaveMode = { | |
Save: 1, | |
SaveAndClose: 2, | |
SaveAndNew: 59, | |
Autosave: 70 | |
}; | |
//Variable that shows if validation was successfully passed or not | |
var isValidationNeeded = true; | |
function OnSave(executionContext) { | |
//so if there are several save handlers and one of previous already called preventDefault | |
//there is no need to do any validations anymore | |
if (executionContext.getEventArgs().isDefaultPrevented()) { | |
return; | |
} | |
//getting save mode from event | |
var saveMode = executionContext.getEventArgs().getSaveMode(); | |
//if savemode is not one of listed - just quit the execution and let the record to be saved | |
if (saveMode !== SaveMode.Save && | |
saveMode !== SaveMode.SaveAndClose && | |
saveMode !== SaveMode.SaveAndNew && | |
saveMode !== SaveMode.Autosave) { | |
return; | |
} | |
//so if validation was successfully passed - flag is reset | |
//and code just leaves the form alone and allows changes to be saved | |
if (!isValidationNeeded) { | |
isValidationNeeded = true; | |
return; | |
} | |
//getting of the form context from execution context object | |
var formContext = executionContext.getFormContext(); | |
var student = formContext.getAttribute("soft_student").getValue()[0].id; | |
var course = formContext.getAttribute("soft_course").getValue()[0].id; | |
var coursestartdate = formContext.getAttribute("soft_coursestartdate").getValue(); | |
if (student == "" || course == "" || coursestartdate == null) { | |
return; | |
} | |
//preventing of the save operation before async operation is started | |
executionContext.getEventArgs().preventDefault(); | |
var formatteddate = coursestartdate.getFullYear().toString() + "-" + (coursestartdate.getMonth() + 1).toString() + "-" + coursestartdate.getDate().toString(); | |
var fetchXML = "?fetchXml=<fetch mapping='logical'><entity name='soft_studentregisteredcourses'><attribute name='soft_name' /><order attribute='soft_name' descending='false' /><filter type='and'><condition attribute='soft_coursestartdate' operator='on' value='" + formatteddate + "' /><condition attribute='soft_course' operator='eq' uitype='soft_course' value='" + course + "' /><condition attribute='soft_student' operator='eq' uitype='soft_student' value='" + student + "' /><condition attribute='statecode' operator='eq' value='0' /></filter></entity></fetch>"; | |
Xrm.WebApi.retrieveMultipleRecords("soft_studentregisteredcourses", fetchXML).then( | |
function success(results) { | |
//so if there are other records with the same student and course with course start date | |
if (results.entities.length !== 0) { | |
//this message is shown to user only when user caused save, autosave is just blocked | |
if (saveMode !== SaveMode.Autosave) { | |
var recordURL = GetEntityRecordUrl(executionContext, false, true, results.entities[0].soft_studentregisteredcoursesid); | |
var entityName = executionContext.getFormContext().data.entity.getEntityName(); | |
var entityId = results.entities[0].soft_studentregisteredcourseid; | |
//var data = { | |
//EntityName: entityName, | |
//EntityId: entityId | |
//}; | |
//custom parameter that you need in the modal dialog | |
var dialogParameters = { | |
pageType: "webresource",//required | |
webresourceName: "soft_rewardvalidatehtml.html",//Html Webresource that will be shown | |
data:recordURL //optional | |
}; | |
var navigationOptions = { | |
target: 2,//use 1 if you want to open page inline or 2 to open it as dialog | |
width: 400, | |
height: 300, | |
position: 1,//1 to locate dialog in center and 2 to locate it on the side, | |
title: "Validation Error" | |
}; | |
Xrm.Navigation.navigateTo(dialogParameters, navigationOptions).then( | |
function (returnValue) { | |
//returnValue is blank when "Cancel" button is clicked | |
if (!returnValue) { | |
return; | |
} | |
console.log(returnValue); | |
//Add your processing logic here | |
}, | |
function (e) { | |
//put your error handler here | |
}); | |
} | |
} | |
else { | |
//otherwise validation flag is set to "Passed" | |
isValidationNeeded = false; | |
//and save event is called again | |
if(saveMode === SaveMode.Save || | |
saveMode === SaveMode.Autosave) { | |
formContext.data.entity.save(); | |
} | |
else if (saveMode === SaveMode.SaveAndClose) { | |
formContext.data.entity.save("saveandclose"); | |
} | |
else { | |
formContext.data.entity.save("saveandnew"); | |
} | |
} | |
}, | |
function (error) { | |
//if something went wrong - error message is shown to user | |
Xrm.Navigation.openAlertDialog({ text: error.message }); | |
} | |
); | |
} | |
function GetEntityRecordUrl(executionContext, forceClassic = false, newWindow = true, recordId) { | |
var strUIType = 'forceUCI=1'; | |
if (forceClassic == true || forceClassic == 1) | |
strUIType = 'forceClassic=1'; | |
if (newWindow == 0) | |
newWindow = false; | |
var entityName = executionContext.getFormContext().data.entity.getEntityName(); | |
var entityId = recordId; | |
var entityRecordUrl = executionContext.getContext().getClientUrl() + '/main.aspx?' + strUIType; | |
entityRecordUrl += '&newWindow=' + newWindow; | |
entityRecordUrl += '&pagetype=entityrecord&etn=' + entityName + '&id=' + entityId; | |
return entityRecordUrl; | |
} | |
return { | |
OnSave: OnSave | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment