SetValue not working in Portal

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2024 09:48 AM - edited 05-03-2024 09:50 AM
Hi All,
I had a requirement to set value of the field from client script.
function onChange(control, oldValue, newValue, isLoading)
{
if(isLoading || newValue != '')
{
return;
}
var techType = g_form.getValue("u_tech");
if(techType == "Hardware")
{
var HWQuestion1 = g_form.getValue("u_hw_q");
if(HWQuestion1 == "")
{
return;
}
else if(HWQuestion1 == "Yes")
{
g_form.setValue("u_impact_assessment", "High");
}
else
{
g_form.setValue("u_impact_assessment", "medium");
}
}
the field u_impact_assessment is a select box field
The above code is working in catalog item view page
but not working in portal.
The impact assessment field is not populating the value.
The catalog client script is onChange and UI type is also set to all
But still it is not working in portal
Anyone can please help e on this
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2024 01:28 PM - edited 05-03-2024 01:48 PM
Try making sure your setValue has value and display value.
Example: g_form.setValue("u_impact_assessment", "medium", "Medium") //You have to verify the value and display value as it is in your system.
Edit: Additional I noticed that the beginning if statement is not the same as is in all on change client scripts is different on yours. It should look like.
if (isLoading || newValue === '') {
return;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2024 10:08 AM
I have tried this setValue but still it is not working ,I have multiple if else statement.
It is working for others when there is change but it is not working for the first selection
eg: tech_category for this question it is not setting the value
var HWQuestion2 = g_form.getValue("tech_category");
if(HWQuestion2 == "")
{
g_form.setValue("u_impact_assessment", "Medium","Medium");
}
else if(HWQuestion2 == "Yes")
{
g_form.setValue("u_impact_assessment", "High","High");
}
else if(HWQuestion2 == "No")
{
// HW question 3
var HWQuestion3 = g_form.getValue("u_supplier");
if(HWQuestion3 == "")
{
g_form.setValue("u_impact_assessment", "Medium","Medium");
}
else if(HWQuestion3 == "Yes")
{
g_form.setValue("u_impact_assessment", "High","High");
}
else if(HWQuestion3 == "No")
{
// HW question 4
var HWQuestion4 = g_form.getValue("u_hardware_reqmandate");
if(HWQuestion4 == "")
{
g_form.setValue("u_impact_assessment", "Medium","Medium");
}
else if(HWQuestion4 == "Yes")
{
g_form.setValue("u_impact_assessment", "High","High");
}
Please help me on this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2024 10:53 AM
Have you confirmed that the value for High is High. Typically when you do impact the value is a number not text.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2024 02:27 PM
It sounds like you're encountering a common scenario where client-side scripts designed for a catalog item don't function as intended within a Service Portal. Here's a breakdown of why this happens and the steps to fix it:
1. Client Scripts in Portals vs. ServiceNow Core UI
- Separate Environments: Service Portals function somewhat independently from the core ServiceNow UI. They have their own ways of handling client-side scripting.
- g_form Limitation: The g_form object commonly used in client scripts is often not fully available or functions differently in the Service Portal context.
2. Solutions
Here are a few different ways to work around this:
Service Portal Widget:
- Create a Service Portal widget.
- Use AngularJS within this widget to access fields and modify them using the ServiceNow REST API or GlideRecord. This approach provides more control and flexibility within the Service Portal context.
Modified Client Script with Compatibility:
- Adjust the client script, replacing g_form with compatible methods. Here's a possible approach:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') { // Note: You might need to adjust '===' return; }
var techType = g_form.getValue("u_tech"); if (techType === "Hardware") {
// Note: You might need to adjust '==='
var HWQuestion1 = g_form.getValue("u_hw_q"); if (HWQuestion1 === "") {
// Note: You might need to adjust '===' return;
}
else if (HWQuestion1 === "Yes") { // Note: You might need to adjust '===' setImpactAssessmentField('High'); }
else { setImpactAssessmentField('Medium'); } } }
function setImpactAssessmentField(value) { // If g_scratchpad is available, try using it, otherwise explore alternatives if (typeof g_scratchpad !== 'undefined') {
g_scratchpad.impact_assessment = value;
}
else { // Placeholder for alternative methods: // 1. GlideAjax to call a server-side script // 2. AngularJS within a Service Portal widget and ServiceNow REST API
} }
3. Debugging
- Browser Console: Check your browser's developer console (F12) for JavaScript errors.
- Logging: If possible, add logging within your scripts to trace execution and identify any issues in the Service Portal environment.