SetValue not working in Portal

Community Alums
Not applicable

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

4 REPLIES 4

Brian Lancaster
Tera Sage

Try making sure your setValue has value and display value. 

BrianLancaster_0-1714767985819.png

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;
}

Community Alums
Not applicable

Hi @Brian Lancaster 

 

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

Have you confirmed that the value for High is High. Typically when you do impact the value is a number not text.

Sumit Pandey3
Tera Contributor

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:

    1. Create a Service Portal widget.
    2. 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:

    1. 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.