Save sc_task record with incomplete mandatory fields | getElementById failing

Evan McElfresh
Giga Guru

I have been struggling with this for a few months now. I set it aside for some time and now I am revisiting and hoping the community can help me get to a resolution.

 

For reference, I am starting with the solution in this thread. However, when I execute this function, I get a NULL response from the variable_map element and the script fails.

Below is my current UI Action settings and script, the HTML div, and the console error output I receive:

EvanMcElfresh_0-1740674049664.png

 

 

//Client-side 'onclick' function
function u_saveRecordClient() {
    //ignore mandatory fields
    var state = g_form.getValue('state');

    console.log("UI Action details | State: " + state);

    //we can save without filling in all mandatory fields and variables if the task is not closed or closing
    if (g_form.getValue('state') != 3) {
        console.log("UI Action details | If statement 1 reached: TRUE");
        g_form.checkMandatory = false; //ignore mandatory fields
        try {
            console.log("Trying to get allVariables");

            //now ignore any mandatory variables
            var allVariables = document.getElementById('variable_map').getElementsByTagName('item');
            console.log("UI Action details | allVariables: " + allVariables);

            for (var i = 0; i < allVariables.length; i++) {
                var item = allVariables[i];
                g_form.setMandatory('variables.' + item.getAttribute('qname').toString(), false);
            }
        } catch (err) {
            console.log("Encountered error: " + err);
        }
    }

    //Call the UI Action and skip the 'onclick' function
    //this will execute the code below
    gsftSubmit(null, g_form.getFormElement(), 'sysverb_update_and_stay'); //MUST call the 'Action name' set in this UI Action
}

//Code that runs on the server, without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
    u_saveRecordServer();

function u_saveRecordServer() {
    action.setRedirectURL(current); //come back to the same record
    current.update();
}

 

 

EvanMcElfresh_0-1740673762832.png

 

 

 

<div id="variable_map" style="visibility:hidden;display:none">
    <item qname="service_provider" id="57d6447a87120610d851c259dabb351e"></item>
    <item qname="etf" id="b43ed07a87920610d851c259dabb35cd"></item>
    <item qname="lte" id="235ed87a87920610d851c259dabb35eb"></item>
    <item qname="device_details" id="3c9f85228716c210d851c259dabb3548"></item>
    <item qname="cost_center" id="f1de54ba87920610d851c259dabb35bc"></item>
</div>

 

 

 

Encountered error: TypeError: Cannot read properties of null (reading 'getElementById')

 

 @Steven Parker and @Jim Coyne have been very involved in this topic.

@Brad Bowman  for visibility 

Please mark this response as correct and/or helpful if it assisted you with your question.
1 ACCEPTED SOLUTION

Chaitanya ILCR
Mega Patron

Hi @Evan McElfresh ,

try setting Isolate Script of UI action to false

ChaitanyaILCR_0-1740677960500.png

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

View solution in original post

11 REPLIES 11

Brad Bowman
Kilo Patron
Kilo Patron

Bonus points for using Dark mode.  Did you add the Isolate script field to your UI Action form or otherwise confirm that it is unchecked/false?  Are you using this in the native UI or Service Portal / ESC / ...?

I just tried the Isolate Script method based on @Chaitanya ILCR 's recommendation. My reply above has the results.

 

At the moment I am attempting to save the sc_task in the Native UI. I will also need it to work in Service Operations Workspace, but I thought I would get it working in the native UI first.

Please mark this response as correct and/or helpful if it assisted you with your question.

I'll see if I can get Jim's solution to work, or a similar error in a current version PDI, but I don't think this will work in SOW.  A use case I've solved for (in the native UI, but should also work elsewhere) is to not make variables mandatory until the task is attempted to be closed - so in the variable definitions or a UI Policy to override, the variables are not mandatory at the Catalog Task level, then where needed create an onSubmit Catalog Client Script that checks if the state value is 3/Closed Complete or whatever, then check if the short description is something in particular if you need to differentiate between Catalog Tasks.  Use setMandatory to show the red asterisks, then for user-friendliness you can check if each variable is populated and show an alert or msg before returning false to stop the submit.

function onSubmit() {
	if (g_form.getValue('state') == 3) { //Closed Complete
        if (g_form.getValue('short_description') == 'Allocate IP Address) {
			g_form.setMandatory('v_registration_date', true);
			if (g_form.getValue('v_registration_date') == '') {
				g_form.addErrorMessage('Registration date required');
				return false;
			}
        }
	}
}

 

Jim's script worked fine for me in Xanadu. I just tried the Save one.  Are these variables mandatory by definition, or with a UI Policy or Client Script on catalog tasks? If you populate the mandatory variables, does the form still Save OK not using this UI Action?

They are only made mandatory by UI Policy. If the variables are populated, it saves just fine.

Please mark this response as correct and/or helpful if it assisted you with your question.