Not able pickup oldValue for confirm dialog box

Joyal Robert
Tera Contributor
I created a catalog client script for onchange script for variable "u_technical_help_sub_category"

function
onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var descriptionValue = g_form.getValue('u_description_html');
    if (descriptionValue) {
        var previousValue = oldValue;
     
        if ((newValue === 'Issue' || newValue === 'Access' || newValue === 'Information Request' || newValue === 'Documentation' || newValue === 'Feature Request')) {
            // Ask for user confirmation before clearing the description
            var userConfirmation = confirm('Selecting the Sub-Category will clear the description. Do you want to proceed?');

            if (userConfirmation) {
                // If the user confirms, set the new subcategory value
                g_form.setValue('u_technical_help_sub_category', newValue);
            } else {
                // If the user cancels, revert back to the old subcategory and restore the description
                g_form.setValue('u_technical_help_sub_category', previousValue);
                g_form.addInfoMessage("Reverted to old value: " + previousValue);
                g_form.setValue('u_description_html', descriptionValue); // Restore the description value
            }
        }
    }
}

But it is not capturing the old value whatever choice before the selection of newValue . It is always capturing the none value in the portal
We need to capture the previous value while cancelling the popup

Regards,
Joyal Robert
9 REPLIES 9

To be honest, I do not understand the "restore the description value". What is it supposed to restore? And the message says that it would clear the description, but that's not reflected in the script. Is something else changing the description? 

@Ankur Bawiskar 
yes, oldValue means the value that was last saved to database and not the value before the most recent change

Scenario:
In landing page the value for 'u_technical_help_sub_category' is 'None'. I changed the choice to 'Issue', there is no popup due to description is empty. when i again changed to another choice such as 'Documentation', it will ask the pop up. If i cancel it should map/update the 'u_technical_help_sub_category' with the value 'Issue' and description should be same as the earlier template before the pop up.

Juhi Poddar
Kilo Patron

Hello @Joyal Robert 

There are few things to note:

  • When setting a variable value within an onChange client script, it will trigger the script again. Use a flag to prevent recursion.
  • Ensure the newValue you're comparing (e.g., 'Issue') corresponds to the backend value. For example, in the Incident table, values like 1, 2, 3 represent Low, Medium, and High.
  • (newValue == 1 || newValue == 2 || newValue == 3)
Here is the updated onChange Client script

 

var isProcessingChange = false;  // Flag to prevent re-triggering

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '' || isProcessingChange) {
        return;  // Skip if the form is loading, new value is empty, or already processing
    }

    var descriptionValue = g_form.getValue('u_description_html');  
    g_form.addInfoMessage("Description Value: " + descriptionValue); 

    if (descriptionValue) {
        // Capture the previous value of the subcategory field
        var previousValue = g_form.getValue('u_technical_help_sub_category');
        g_form.addInfoMessage("Previous Sub-Category: " + previousValue);
        
        // Check if the new value is one of the specified subcategory types
        if (newValue === 'Issue' || newValue === 'Access' || newValue === 'Information Request' || newValue === 'Documentation' || newValue === 'Feature Request') {
            g_form.addInfoMessage("New Sub-Category matches condition: " + newValue);  
            
            // Ask for user confirmation before clearing the description
            var userConfirmation = confirm('Selecting the Sub-Category will clear the description. Do you want to proceed?');
            g_form.addInfoMessage("User Confirmation: " + userConfirmation);  
            
            if (userConfirmation) {
                // If the user confirms, set the new subcategory value and clear the description
                isProcessingChange = true;  // Set flag to prevent recursion
                g_form.addInfoMessage("User confirmed. Setting new sub-category and clearing description.");  
                g_form.setValue('u_technical_help_sub_category', newValue);
                g_form.setValue('u_description_html', '');  // Optionally, clear the description if needed
                isProcessingChange = false;  // Reset flag after processing
            } else {
                // If the user cancels, revert back to the old subcategory value and restore the description
                isProcessingChange = true;  // Set flag to prevent recursion
                g_form.setValue('u_technical_help_sub_category', previousValue);
                g_form.addInfoMessage("Reverted to old value: " + previousValue);  
                g_form.setValue('u_description_html', descriptionValue);  // Restore the description value
                isProcessingChange = false;  // Reset flag after processing
            }
        }
    }
}
​

 

Something similar is tested on my PDI and it is working for me.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

@Juhi Poddar 

 

I checked with your script, it seems the oldValue and newValue is showing same and description is not capturing the previous one, the template is also changing

Scenario:
In landing page the value for 'u_technical_help_sub_category' is 'None'. I changed the choice to 'Issue', there is no popup due to description is empty. when i again changed to another choice such as 'Documentation', it will ask the pop up. If i cancel it should map/update the 'u_technical_help_sub_category' with the value 'Issue' and description should be same as the earlier template before the pop up.

Abhay Kumar1
Giga Sage

@Joyal Robert The issue you're facing with capturing the previous value of u_technical_help_sub_category when the user cancels the selection can be addressed by properly capturing and storing the value before making any changes to the form.

In your current script, you're trying to capture the previous value using oldValue, but since the form hasn't fully loaded yet or is being manipulated asynchronously, oldValue might not be as expected. One solution is to store the old value explicitly before the confirm() prompt.

Hope this will help you.