Skip to main content

Known issue - clear variable value on ui policy action not working

 variables may still be populated on the generated RITM! Need to check with ServiceNow if this is a known issue > UPDATE: you need to tick the run on RITM and task checkboxes, as well as the cat item

However, another issue occurs whereby variables are blanked when you add to the cart. Referred this back to ServiceNow who came back with this update:

So in order to fix the issue we need to create duplicate UI policy where we need to select "clear the variable value" set to "false" and uncheck the "on load" option from the UI policy. 6. Another UI policy where "clear the variable value" set to "true" and check the "on load" option from the UI policy.

 

====

workaround - uncheck the 'clear variable value' flags (so it also works when adding item to the cart as opposed straight checkout)

onSubmit client script code:

(g_form.isVisible only works in SP)

//--Pop this into an onSubmit client script – must use setValue “” (clearValue doesn’t seem to work) //--loop through variables--as the ui policy action 'clear variable value' not working on the generated RITM
var emptyVariables = []; var populatedVariables = []; if (typeof spModal != 'undefined') { //--loaded from Service Portal var allFields = g_form.getFieldNames(); for (var fieldName in allFields) { var fieldLabel = allFields[fieldName]; var fieldVal_p = g_form.getValue(fieldLabel).toString(); if (fieldVal_p == '' || g_form.isVisible(fieldLabel) == false) { if (fieldVal_p != 'false') { //emptyVariables.push(fieldLabel+': ' + fieldVal_p); g_form.setValue(fieldLabel, ''); } } else { //populatedVariables.push(fieldLabel+': ' + fieldVal_p); } } //alert('emptyVariables: ' + emptyVariables.toString()); //alert('populatedVariables: ' + populatedVariables.toString()); //return false;//--testing only } else { var sRT = g_form.getValue('sn_request_type');
if (sRT != 'Update') { g_form.setValue('sn_group_type_to_update', ''); } if (sRT != "Deactivate") { g_form.setValue('sn_group_type_to_deactivate', ''); } var allVariables = document.getElementById('variable_map').getElementsByTagName('item'); for (var i = 0; i < allVariables.length; i++) { try { var item = allVariables[i]; var itemName = item.getAttribute('qname').toString(); //alert(item.id); var item_el = document.getElementById(g_form.resolveNameMap(itemName)); //alert(itemName + ' ' + window.getComputedStyle(item_el).display); var iWidth = (item_el.offsetWidth); var iHeight = (item_el.offsetHeight); var bProceed = true; if (sRT == 'Deactivate' && itemName == 'sn_group_to_deactivate') { bProceed = false; } if (sRT == 'Update' && itemName == 'sn_group_type_to_update') { bProceed = false; //--do not blank visible reference fields } if (bProceed && iWidth == 0 && iHeight == 0 && g_form.getValue(itemName) != 'true') {//--do not blank checkbox //alert("blanking item " + itemName); g_form.setValue(itemName, ''); } } catch (ex) { } }
}
}

Comments

Popular posts from this blog

URL link in addInfoMessage

var ga=new GlideAjax('gld_HR_ajax'); ga.addParam('sysparm_name', 'checkEmployeeNumber_hrProfile'); ga.addParam('sysparm_hrprofilenumber', g_form.getValue('number')); ga.addParam('sysparm_employeenumber', newValue); ga.getXMLAnswer(function(answer) { if (answer!='undefined' && answer!=''){ var navURL="<a style='text-decoration:underline;color:blue' href=hr_profile.do?sysparm_query=number=" + answer + ">" + answer + "</a><img width='3' src='images/s.gif'/>"; var sMsg='The employee number entered already exists on another HR Profile ' + navURL; //alert(sMsg); g_form.showErrorBox('employee_number', 'error - please check'); g_form.addInfoMessage(sMsg); } });

GlideRecord setValue

setValue(String name, Object value) Sets the specified field to the specified value. Normally a script would do a direct assignment, for example,  gr.category = value . However, if in a script the element name is a variable, then  gr.setValue(elementName, value)  can be used. When setting a value, ensure the data type of the field matches the data type of the value you enter. This method cannot be used on journal fields. If the value parameter is null, the record is not updated, and an error is not thrown https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_GlideRecord-setValue_String_Object