Im trying to set a True/False flag based on this function so i can set the PreSaveAction to return the correct result. Basically, this function checks the permission group to see if the current logged on user is part f that group, a true or false flag is set so that the save action may return the result of this function but for some reason, it returns undefined. ive added comments by the alerts just for reference.
var g_rtnVal = false;
$(document).ready(function(){
g_rtnVal = ValidateTaskApprover();
alert(g_rtnVal); // returned undefined
});
// this fails at times because its being called on docReady and the page doesnt load in time for AssignedTo to read
function ValidateTaskApprover() {
//var rtnVal = false;
$().SPServices({
operation: "GetGroupCollectionFromUser",
userLoginName: $().SPServices.SPGetCurrentUser(),
async: false,
completefunc: function (xData, Status) {
var strAssignedTo = SPUtility.GetSPField('Assigned To').GetValue();
if ($(xData.responseXML).find("Group[Name='"+strAssignedTo+"']").length == 1) {
var rtnVal = true;
alert("sequence 1: SUCCESS (true)" + rtnVal); //returns true
return rtnVal;
alert("this should not print");
}
else{
alert("sequence 1: FAILURE (false)" + rtnVal);
return rtnVal;
}
}
});
} /* ValidateTaskApprover */
function PreSaveAction() {
var bPreSaveReturn = false;
bPreSaveReturn = ValidateTaskApprover();
alert(bPreSaveReturn); // returns undefined
alert(ValidateTaskApprover()); // returns undefined
return false;
} /* PreSaveAction */
I solved it. All i needed to do was to return the value out of the completefunc callback:
function ValidateTaskApprover() {
var rtnVal = false;
var strAssignedTo = SPUtility.GetSPField('Assigned To').GetValue();
$().SPServices({
operation: "GetGroupCollectionFromUser",
userLoginName: $().SPServices.SPGetCurrentUser(),
async: false,
completefunc: function (xData, Status) {
if ($(xData.responseXML).find("Group[Name='"+strAssignedTo+"']").length == 1) {
rtnVal = true;
}
else{
alert("This task has been assigned to "+strAssignedTo+". You do not have the permissions to Approve it");
}
}
});
return rtnVal
}
function PreSaveAction() {
return ValidateTaskApprover();
}