Adding an if condition to a UI action

matthew_hughes
Kilo Sage

I've got a UI Action under the 'Related Links' called 'Generate/Continue BIA'. What I'm wanting to do is apply some logic so that it checks if the entries in the 'Divisional Application Owner' or 'Technical Application Owner' are active. If either one of the users are inactive and a user clicks on the 'Generate/Continue BIA' link, it should generate an error message 'The BIA cannot be generated/continued until both role holders are active' and keep them on the same page.

find_real_file.png

 

I was just wondering how I would get this applied to the UI action?

1 ACCEPTED SOLUTION

Hi,

update as this

function invokeAssessment() {

	if(g_form.getReference('u_business_owner').active.toString() == 'true' && g_form.getReference('u_custodian').active.toString() == 'true'){
		checkCurrentBia();
	}
	else{
		g_form.addErrorMessage('The BIA cannot be generated/continued until both role holders are active');
	}	
}

function checkCurrentBia(){

	var businessApp = g_form.getUniqueValue();
	//Access the glide ajax script include with the current sys id as a parameter
	var gaAssessment = new GlideAjax('LBGBiaUtils');
	gaAssessment.addParam('sysparm_name', 'currentAssessmentAjax');
	gaAssessment.addParam('sysparm_bus_app',businessApp);
	gaAssessment.getXML(generateAssessment);

	function generateAssessment(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		var data = answer.evalJSON(true);

		if (data.wipBia == 'false') {
			//generate pop up of UI page lbg_bia_dialog
			var displayValue = g_form.getDisplayValue();
			var biaDecisionPage = "lbg_bia_dialog";
			var dialogWindow = new GlideModal(biaDecisionPage, false, "modal-md");
			dialogWindow.setTitle(getMessage("Type of BIA"));
			dialogWindow.setPreference("displayValue", displayValue);
			//sysparm_id is the sys id of the current business app
			dialogWindow.setPreference("sysparm_id", g_form.getUniqueValue());
			dialogWindow.render();
			window.g_parentDialog = dialogWindow;
		} else {
			gsftSubmit(null, g_form.getFormElement(), 'action_bia');
		}
	}
}

if (typeof window == 'undefined')
	openCurrentBia();

function openCurrentBia() {
	var util = new LBGBiaUtils();
	var data = util.currentAssessment(current.getValue('sys_id'));
	var urlArray = [];
	urlArray.push(data.environmentUrl);
	urlArray.push(data.biaInstance);
	urlArray.push(data.biaMetricType);
	var instanceUrl = gs.getMessage('lbg.assessment.redirect.url' , urlArray);
	action.setRedirectURL(instanceUrl);
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

16 REPLIES 16

Hi @Ankur Bawiskar  We can't convert to Active. Could we have an if statement within the UI Action?

Hi,

It makes no sense showing the UI action if either of that user is inactive.

It would annoy user if they see the button but they see the error when they click it.

So it makes sense to show the button when both the users are active

OR

if you still want to show error message then use this

if(current.u_divisional_application_owner.toString() == 'true' && current.u_technical_application_owner.toString() == 'true'){
	// your existing code
}
else{
	gs.addErrorMessage('The BIA cannot be generated/continued until both role holders are active');
	current.setAbortAction(true);
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar 

I've applied your amendments to my code:

if(current.u_business_owner.active == true && current.u_custodian.active == true){
function invokeAssessment() {
    checkCurrentBia();
}

/*if (current.u_business_owner.active.toString() != "true" || current.u_custodian.active.toString() != "true") {
    gs.addErrorMessage('The BIA cannot be generated/continued until both role holders are active');
    current.setAbortAction(true);
}*/

function checkCurrentBia(){
    
    var businessApp = g_form.getUniqueValue();
    //Access the glide ajax script include with the current sys id as a parameter
    var gaAssessment = new GlideAjax('LBGBiaUtils');
    gaAssessment.addParam('sysparm_name', 'currentAssessmentAjax');
    gaAssessment.addParam('sysparm_bus_app',businessApp);
    gaAssessment.getXML(generateAssessment);
    
    function generateAssessment(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var data = answer.evalJSON(true);
        
        if (data.wipBia == 'false') {
            
            biaType();
            
        } else {
            
            gsftSubmit(null, g_form.getFormElement(), 'action_bia');
            
        }
    }
    
}

if (typeof window == 'undefined')
   openCurrentBia();


function openCurrentBia() {
    var util = new LBGBiaUtils();
    var data = util.currentAssessment(current.getValue('sys_id'));
    var urlArray = [];
            
    urlArray.push(data.environmentUrl);
    urlArray.push(data.biaInstance);
    urlArray.push(data.biaMetricType);
    var instanceUrl = gs.getMessage('lbg.assessment.redirect.url' , urlArray);
    action.setRedirectURL(instanceUrl);
}

 


function biaType(){
    
    //generate pop up of UI page lbg_bia_dialog
    var displayValue = g_form.getDisplayValue();
    var biaDecisionPage = "lbg_bia_dialog";
    var dialogWindow = new GlideModal(biaDecisionPage, false, "modal-md");
    dialogWindow.setTitle(getMessage("Type of BIA"));
    dialogWindow.setPreference("displayValue", displayValue);
    //sysparm_id is the sys id of the current business app
    dialogWindow.setPreference("sysparm_id", g_form.getUniqueValue());
    dialogWindow.render();
    window.g_parentDialog = dialogWindow;
    
}
}
else{
    gs.addErrorMessage('The BIA cannot be generated/continued until both role holders are active');
    current.setAbortAction(true);
}

 

However, when I hover over the beginning of each function, message appears "Move function declaration to program root."

 

When I click on the 'Generate/Continue BIA', link, nothing loads.

Hi,

update as this

function invokeAssessment() {

	if(g_form.getReference('u_business_owner').active.toString() == 'true' && g_form.getReference('u_custodian').active.toString() == 'true'){
		checkCurrentBia();
	}
	else{
		g_form.addErrorMessage('The BIA cannot be generated/continued until both role holders are active');
	}	
}

function checkCurrentBia(){

	var businessApp = g_form.getUniqueValue();
	//Access the glide ajax script include with the current sys id as a parameter
	var gaAssessment = new GlideAjax('LBGBiaUtils');
	gaAssessment.addParam('sysparm_name', 'currentAssessmentAjax');
	gaAssessment.addParam('sysparm_bus_app',businessApp);
	gaAssessment.getXML(generateAssessment);

	function generateAssessment(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		var data = answer.evalJSON(true);

		if (data.wipBia == 'false') {
			//generate pop up of UI page lbg_bia_dialog
			var displayValue = g_form.getDisplayValue();
			var biaDecisionPage = "lbg_bia_dialog";
			var dialogWindow = new GlideModal(biaDecisionPage, false, "modal-md");
			dialogWindow.setTitle(getMessage("Type of BIA"));
			dialogWindow.setPreference("displayValue", displayValue);
			//sysparm_id is the sys id of the current business app
			dialogWindow.setPreference("sysparm_id", g_form.getUniqueValue());
			dialogWindow.render();
			window.g_parentDialog = dialogWindow;
		} else {
			gsftSubmit(null, g_form.getFormElement(), 'action_bia');
		}
	}
}

if (typeof window == 'undefined')
	openCurrentBia();

function openCurrentBia() {
	var util = new LBGBiaUtils();
	var data = util.currentAssessment(current.getValue('sys_id'));
	var urlArray = [];
	urlArray.push(data.environmentUrl);
	urlArray.push(data.biaInstance);
	urlArray.push(data.biaMetricType);
	var instanceUrl = gs.getMessage('lbg.assessment.redirect.url' , urlArray);
	action.setRedirectURL(instanceUrl);
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks @Ankur Bawiskar 

 

I've added the following code to my script and it works:

function invokeAssessment() {

	if(g_form.getReference('u_business_owner').active.toString() == 'true' && g_form.getReference('u_custodian').active.toString() == 'true'){
		checkCurrentBia();
	}
	else{
		g_form.addErrorMessage('The BIA cannot be generated/continued until both role holders are active');
	}	
}