Gliderecord has broken my UI Action?

MBarrott
Mega Sage

I wanted to add an additional series to checks to the Create Project UI Action. 

 

I've added this to the Close UI Action and it worked with no issue.

 

Right now pressing the Create Project button does nothing at all, commenting out the new Gliderecord code and it works again. 

 

Can someone tell me what I'm doing wrong?

 

Code below and new code has been highlighted:

 

var gDialog;

function convertDemandToProject() 
{
	var capital = g_form.getValue('capital_budget'); // capture data for capital budget field 
	var operating = g_form.getValue('operational_budget'); // capture data for operating budget field 
	var capitalValues = capital.split(';'); // seperate data by ';' separator into a substring array
	var operatingValues = operating.split(';'); // seperate data by ';' separator into a substring array
	var capitalVal = parseFloat(capitalValues[1]); // capture second array substring from capitalValues
	var operatingVal = parseFloat(operatingValues[1]); // capture second array substring from operatingValues
	var total = capitalVal + operatingVal; // calculate total from capital and operating values
	

	var gr = new GlideRecord('dmn_demand_task'); // NEW GLIDERECORD HERE
	gr.addQuery('parent', current.sys_id); 
	gr.addQuery('state', '!=', 3); 
	gr.addQuery('state', '!=', 4);
	gr.addQuery('state', '!=', 7);
	gr.query();
	var dmTaskCount = gr.getRowCount();

	var gr2 = new GlideRecord('dmn_demand_task'); // NEW GLIDERECORD HERE
	gr2.addQuery('parent', current.sys_id);
	gr2.query();
	var dmTaskCount2 = gr2.getRowCount();
	
	if (g_form.modified) 
	{
		return g_form.addErrorMessage(getMessage('You have not saved all your changes, save the demand record before creating project'));
	} 
	if(g_form.getValue('type') == 'project'&& g_form.getElement("calculation_type") && !g_form.getValue('calculation_type')) 
	{
		return g_form.addErrorMessage(getMessage('Please select a project calculation type under preferences.'));
	}
	
	var ga = new GlideAjax("AjaxCreateRelatedEntityFromDemand");
	ga.addParam("sysparm_name", "checkPmoUpgrade");
	ga.addParam("sysparm_source_table", g_form.getTableName());
	ga.getXML(function(response) {
		var result = response.responseXML.getElementsByTagName("result");
		var isUpdrading = result[0].getAttribute("isUpgrading");
		var isProjectCurrencyEnabled = result[0].getAttribute("isProjectCurrencyEnabled");
		var isMoreThanOneProjectClassExists = result[0].getAttribute("isMultipleProjectClassExists");
		var defaultProjectTable = result[0].getAttribute("defaultProjectTable");

		// check to ensure budget exceeds $0.00 in some capacity
		if(capitalVal <= 0 && operatingVal <= 0) 
		{
			g_form.addErrorMessage('Capital or Operating budget must exceed $0.00'); // display error if budget fails to meet requirements
		}
		else // proceed with regular checks and project function call is budget passes check
		{
			if (isUpdrading === "true") 
			{
				g_form.addErrorMessage(getMessage("Financial Planning Upgrade Job Running. Cannot create Project."));
			} 
			else if (isProjectCurrencyEnabled == "true" || isMoreThanOneProjectClassExists == "true") 
			{			
				if(dmTaskCount2 == 0) // NEW IF CHECK HERE
				{
					gs.addErrorMessage('At least one demand task must be created for projects.');	
					//current.setAbortAction(true); //abort the  action	
				}
				else if(dmTaskCount > 0) // check if there are open demand task // NEW IF CHECK HERE
				{
					gs.addErrorMessage('Cannot close Demand with open tasks.');
					//current.setAbortAction(true); //abort the  action
				}
				else
				{
					gs.addErrorMessage('dmTaskCount2 value is: ' + dmTaskCount2);
					gs.addErrorMessage('dmTaskCount value is: ' + dmTaskCount);
					
					gDialog = new GlideModal('demand_to_project');
					gDialog.setPreference('sysparm_task_id', g_form.getUniqueValue());
					gDialog.setPreference('sysparm_projTable', defaultProjectTable);
					gDialog.setPreference('sysparm_isProjectCurrencyEnabled', isProjectCurrencyEnabled);
					gDialog.setPreference('sysparm_isMoreThanOneProjectTableExists', isMoreThanOneProjectClassExists);
					gDialog.setPreference('on_submit', onSubmit);
					gDialog.setPreference('on_cancel', onCancel);
					gDialog.setTitle(new GwtMessage().getMessage('Create project'));
					gDialog.render();
				}
					
			} 
			else
				onSubmit(defaultProjectTable);
		}
	});
}

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

function redirect() {
    action.setRedirectURL(current);
}

function onCancel() {
    if (gDialog)
        gDialog.destroy();
    return false;
}

function onSubmit(projName) {
    var createProjectAjax = new GlideAjax("AjaxCreateRelatedEntityFromDemand");
    createProjectAjax.addParam("sysparm_name", "createProjectAjax");
    createProjectAjax.addParam("sysparm_sys_id", g_form.getUniqueValue());
    createProjectAjax.addParam("sysparm_projName", projName);
    createProjectAjax.getXML();

    if (gDialog)
        gDialog.destroy();
	g_form.save();

    return false;
}
1 ACCEPTED SOLUTION

The Machine
Kilo Sage

GlideRecord is server side.  It looks like you are mixing server and client code in your client function.  I'd suggest moving your GlideRecord look ups to your Script Include and call that within your GlideAjax that you pass back to the client.

View solution in original post

6 REPLIES 6

The Machine
Kilo Sage

GlideRecord is server side.  It looks like you are mixing server and client code in your client function.  I'd suggest moving your GlideRecord look ups to your Script Include and call that within your GlideAjax that you pass back to the client.

Hi @The Machine

 

Can you give me an example of what you mean? The server side vs client side scripting is a bit confusing to me. 

I built out a new GlideAjax and the same issue occurs. The UI Action button doesn't respond. 

Here's a couple of links that should help you and breaks it down into a smaller example so its a little easier to digest.

For example.  g_form is client side and used in client functions and client scripts.  GlideRecord & gs are server side.  When you try to use these classes client side its as if they don't exist, because technically they don't.  

https://servicenowguru.com/system-ui/ui-actions-system-ui_client-server-code-ui-action/

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0657198