How to increase count in OnChange client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2024 09:29 PM
Hi All,
after opening the Incident Record if user try to change category value from one value to another value without saving th record , for each change count has to increase.
Example : On INC Record Category : Network, After Record Open,
Without saving the value in record if user keeps changing Category software to hardware, hardware to another value...etc for each time changing the value count has to increase.
after saving record , if we reopen the record again count has to come start from Zero.
i tried below script
Onchange :
var count=0;
if( newValue !=""){
if(count==0){
g_form.addInfoMessage("First change");
}else {
g_form.addInfoMessage("Not First change");
}
count++;
but here issue is for me it 's always coming "First Change" info message only.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2024 09:49 PM
It seems like the issue with your script is that the count variable is declared and initialized within the onchange script, which means every time the script executes, it resets count to zero. To achieve the desired functionality, you need to persist the count value across onchange events. You can use a hidden field on the form to store and retrieve the count value.
Create a Hidden Field: Add a hidden field to your Incident form to store the count value. Let's call it "change_count".
Modify the OnChange Script:
// Get the current count value from the hidden field
var count = parseInt(g_form.getValue('change_count')) || 0;
// Check if the category value has changed
if (newValue != "" && newValue != g_form.getOriginalValue('category')) {
if (count == 0) {
g_form.addInfoMessage("First change");
} else {
g_form.addInfoMessage("Not First change");
}
// Increment the count and update the hidden field
count++;
g_form.setValue('change_count', count.toString());
} else {
// If the category value hasn't changed, reset the count to 0
g_form.setValue('change_count', '0');
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 05:56 AM
Hi @Maddysunil code not working
1.getOriginalValue() function not existed error coming.
2. I don't want to create new field, Can we do without creating new field ?
3. it is always showing zero.
4. where we are not saving record, after loading record we are back to back changing value up and down, down to up, that changing we have to calculate .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 06:39 AM
I think you will need to create a custom field for counting, I try with below script on PDI, it's working
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (newValue != oldValue) {
var count = g_form.getValue('u_count'); //u_count is custom integer field on the form
count++;
g_form.setValue('u_count',count);
}
if (count == 1) {
alert("hi");
}
if (count == 2) {
alert("hello");
}
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2024 09:57 PM - edited 04-16-2024 09:59 PM
Hi
You can also utilize the global NOW object like
For when the form renders you create a global count variable for the browser to use and increase for each changes to the category field
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
NOW.count = 0;
return;
}
alert(NOW.count);
NOW.count++;
}