UI action for List view is not working

Prasad49
Tera Contributor

I have tired creating a Ui action for updating records selected in the list view but it's not working, for these records selected records the field value should be updated +1, please help resolve the issue 

 

UI action script

 

function executeAction() {
    // Get the selected Sys IDs from the list view
    var selectedSysIds = g_list.getChecked();

    // Ensure there are selected records
    if (selectedSysIds.length === 0) {
        g_form.addErrorMessage('No records selected.');
        return;
    }
    else{
    alert("selected ids"+ selectedSysIds);
gs.log("selected ids"+ selectedSysIds);
   
    var ga = new GlideAjax('IncrementFieldValueScriptInclude');
    ga.addParam('sysparam_name','incrementFieldValue');
    ga.addParam('sys_ids', selectedSysIds);  // Send selected Sys IDs
    ga.addParam('u_trigger', u_trigger);  // Send field name to increment
    ga.getXMLAnswer(function(response) {
        var message = response.responseXML.documentElement.getAttribute("answer");
        g_form.addInfoMessage(message);
    });
    }
}
 
Script include
 
var IncrementFieldValueScriptInclude = Class.create();
IncrementFieldValueScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
    // Function to be called by the UI Action
    incrementFieldValue: function() {
        // Get the Sys IDs passed from the client-side script
        var sysIds = this.getParameter('sys_ids');
var ids=[];
ids.push(sysIds.split(','));
        //var fieldName = this.getParameter('u_trigger');
       
        var updatedCount = 0;

        // Loop through the Sys IDs and update the field for each record
        for (var i = 0; i < ids.length; i++) {
            var gr = new GlideRecord('u_vendor_master_database');  // Replace with the actual table name
            if (gr.get(ids[i])) {
                 updatedCount = gr.getValue('u_trigger');

                gs.log("number is"+ gr.getUniqueValue());
                // Get the current value of the field
              //  var fieldValue = gr.getValue(fieldName);
               
                // If the field value is empty or null, set it to 0
                // if (fieldValue == null || fieldValue === '') {
                //     fieldValue = 0;
                // }

                // // Ensure the value is numeric
                // if (isNaN(fieldValue)) {
                //     fieldValue = 0;
                // }

                // Increment the field value
                gr.setValue('u_trigger', parseInt(updatedCount) + 1);

                // Update the record in the database
                gr.update();
                updatedCount++;  // Increment the updated record counter
            }
        }

        // Return a message about the number of records updated
        return updatedCount + ' record(s) have been updated successfully.';
    }

});
1 ACCEPTED SOLUTION

Hi @Prasad49 ,

update the ui action script

function executeAction() {
    // Get the selected Sys IDs from the list view
    var selectedSysIds = g_list.getChecked();

    // Ensure there are selected records
    if (selectedSysIds.length === 0) {
        // g_form.addErrorMessage('No records selected.'); // g_form is not available on list
        alert('No records selected.');
        return;
    } else {
        alert("selected ids" + selectedSysIds);
        // gs.log("selected ids" + selectedSysIds); log doesn't workin as this is client side

        var ga = new GlideAjax('IncrementFieldValueScriptInclude');
        ga.addParam('sysparam_name', 'incrementFieldValue');
        ga.addParam('sys_ids', selectedSysIds); // Send selected Sys IDs
        // ga.addParam('u_trigger', u_trigger); // u_trigger is not declared and not required as per your script include
        ga.getXMLAnswer(function(response) {
            // var message = response.responseXML.documentElement.getAttribute("answer"); 
            // g_form.addInfoMessage(message); g_form is not available on list
            alert(response);
            g_list.refresh();
        });
    }
}

 

since you are using the UI action in form also it's better to go with alternative approach with 

uncheck the client on ui action and replace the script

 

alternative  UI action script

var triggerCount = current.getValue('u_trigger');
triggerCount = triggerCount ? parseInt(triggerCount) + 1 : 0;
current.u_trigger = triggerCount;
current.update();

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

View solution in original post

7 REPLIES 7

Shivalika
Mega Sage

Hello @Prasad49 

 

Please replace the variable names with "sysparm_sys_ids" and "sysparm_u_trigger" . Call it in the same way in script include also. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

sunil maddheshi
Tera Guru

@Prasad49 

 

  • g_list.getChecked() Returns a String:

    • The g_list.getChecked() method returns a comma-separated string of Sys IDs, not an array.

    • You should split it into an array before passing it to GlideAjax.

  • Missing u_trigger Variable:

    • You're using ga.addParam('u_trigger', u_trigger);, but u_trigger is not defined in the UI action script.

  • Incorrect Handling of sysIds:

    • var sysIds = this.getParameter('sys_ids'); returns a string, not an array.

    • var ids = []; ids.push(sysIds.split(',')); incorrectly nests an array inside another array.

  • Looping Issue:

    • for (var i = 0; i < ids.length; i++) is incorrect because ids is a nested array.

    • Instead, use var ids = sysIds.split(','); and loop through ids.

Please try with below updated code:

UI action:

function executeAction() {
    // Get the selected Sys IDs from the list view
    var selectedSysIds = g_list.getChecked();

    // Ensure there are selected records
    if (!selectedSysIds) {
        g_form.addErrorMessage('No records selected.');
        return;
    }

    alert("Selected IDs: " + selectedSysIds);
    gs.log("Selected IDs: " + selectedSysIds);

    var ga = new GlideAjax('IncrementFieldValueScriptInclude');
    ga.addParam('sysparam_name', 'incrementFieldValue');
    ga.addParam('sys_ids', selectedSysIds);  // Send selected Sys IDs
    ga.addParam('u_trigger', 'u_trigger');  // Specify the field to increment (replace if needed)

    ga.getXMLAnswer(function(response) {
        var message = response.responseXML.documentElement.getAttribute("answer");
        g_form.addInfoMessage(message);
    });
}

Script inlcude:

var IncrementFieldValueScriptInclude = Class.create();
IncrementFieldValueScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
    incrementFieldValue: function() {
        // Get the Sys IDs passed from the client-side script
        var sysIds = this.getParameter('sys_ids');
        var fieldName = this.getParameter('u_trigger');

        if (!sysIds || !fieldName) {
            return 'Invalid parameters';
        }

        var ids = sysIds.split(',');
        var updatedCount = 0;

        for (var i = 0; i < ids.length; i++) {
            var gr = new GlideRecord('u_vendor_master_database');  // Replace with your table name
            if (gr.get(ids[i])) {
                var currentValue = parseInt(gr.getValue(fieldName)) || 0;  // Ensure it's numeric
                gr.setValue(fieldName, currentValue + 1);
                gr.update();
                updatedCount++;  
            }
        }

        return updatedCount + ' record(s) have been updated successfully.';
    }

});

Please mark correct/helpful if this helps you in anyway!

 

 

Hi Sunil, 

tired Ui action and script include shared by you but its not updating the trigger field value to +1

 

below ui action script used

function executeAction() {
    // Get the selected Sys IDs from the list view
    var selectedSysIds = g_list.getChecked();

    // Ensure there are selected records
    if (!selectedSysIds) {
        g_form.addErrorMessage('No records selected.');
        return;
    }

    alert("Selected IDs: " + selectedSysIds);
    gs.log("Selected IDs: " + selectedSysIds);

    var ga = new GlideAjax('IncrementFieldValueScriptInclude');
    ga.addParam('sysparam_name', 'incrementFieldValue');
    ga.addParam('sys_ids', selectedSysIds);  // Send selected Sys IDs
    ga.addParam('u_trigger', 'u_trigger');  // Specify the field to increment (replace if needed)

    ga.getXMLAnswer(function(response) {
        var message = response.responseXML.documentElement.getAttribute("answer");
        g_form.addInfoMessage(message);
    });
}
 
Script include
 
var IncrementFieldValueScriptInclude = Class.create();
IncrementFieldValueScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
    incrementFieldValue: function() {
        // Get the Sys IDs passed from the client-side script
        var sysIds = this.getParameter('sys_ids');
        var fieldName = this.getParameter('u_trigger');

        if (!sysIds || !fieldName) {
            return 'Invalid parameters';
        }

        var ids = sysIds.split(',');
        var updatedCount = 0;

        for (var i = 0; i < ids.length; i++) {
            var gr = new GlideRecord('u_vendor_master_database');  // Replace with your table name
            if (gr.get(ids[i])) {
                var currentValue = parseInt(gr.getValue(fieldName)) || 0;  // Ensure it's numeric
                gr.setValue(fieldName, currentValue + 1);
                gr.update();
                updatedCount++;  
            }
        }

        return updatedCount + ' record(s) have been updated successfully.';
    }

});
please let me know if any corrections required, thanks in advance 

Chaitanya ILCR
Kilo Patron

Hi @Prasad49 ,

I have added my comments next to the line having issues in the UI action and commented them

 

UI action

function executeAction() {
    // Get the selected Sys IDs from the list view
    var selectedSysIds = g_list.getChecked();

    // Ensure there are selected records
    if (selectedSysIds.length === 0) {
        // g_form.addErrorMessage('No records selected.'); // g_form is not available on list
        alert('No records selected.');
        return;
    } else {
        alert("selected ids" + selectedSysIds);
        // gs.log("selected ids" + selectedSysIds); log doesn't workin as this is client side

        var ga = new GlideAjax('IncrementFieldValueScriptInclude');
        ga.addParam('sysparam_name', 'incrementFieldValue');
        ga.addParam('sys_ids', selectedSysIds); // Send selected Sys IDs
        // ga.addParam('u_trigger', u_trigger); // u_trigger is not declared and not required as per your script include
        ga.getXMLAnswer(function(response) {
            var message = response.responseXML.documentElement.getAttribute("answer");
            // g_form.addInfoMessage(message); g_form is not available on list
            alert(message);
            g_list.refresh(); 
        });
    }
}

 Script Include

var IncrementFieldValueScriptInclude = Class.create();
IncrementFieldValueScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    // Function to be called by the UI Action
    incrementFieldValue: function() {
        // Get the Sys IDs passed from the client-side script
        // var sysIds = this.getParameter('sys_ids');
        // var ids = [];
        // ids.push(sysIds.split(','));
		var ids = this.getParameter('sys_ids').split(',');
        //var fieldName = this.getParameter('u_trigger');

        var updatedCount = 0;

        // Loop through the Sys IDs and update the field for each record
        for (var i = 0; i < ids.length; i++) {
            var gr = new GlideRecord('u_vendor_master_database'); // Replace with the actual table name
            if (gr.get(ids[i])) {
                updatedCount = parseInt(gr.getValue('u_trigger') ? gr.getValue('u_trigger') : 0);

                gs.log("number is" + gr.getUniqueValue());
                // Get the current value of the field
                //  var fieldValue = gr.getValue(fieldName);

                // If the field value is empty or null, set it to 0
                // if (fieldValue == null || fieldValue === '') {
                //     fieldValue = 0;
                // }

                // // Ensure the value is numeric
                // if (isNaN(fieldValue)) {
                //     fieldValue = 0;
                // }

                // Increment the field value
                gr.setValue('u_trigger', ++updatedCount);

                // Update the record in the database
                gr.update();
            }
        }

        // Return a message about the number of records updated
        return updatedCount + ' record(s) have been updated successfully.';
    }

});

 

you don't have to use this glideajax approach you can simply have ui action for this with only server and client is not required the system will throw automatically when clicked on the ui action in list if you do not select anything

else you can update the ui action and script include like I have mentioned

 

alternative  UI action script

var triggerCount = current.getValue('u_trigger');
triggerCount = triggerCount ? parseInt(triggerCount) + 1 : 0;
current.u_trigger = triggerCount;
current.update();

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya