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

Hi Chaitanya,

I tried your solution but it did not work i have attached screenshots for your reference. please let me know if any corrections required.

Thanks 

 

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

 

 

alternative  UI action script is working, Thanks marking your response as helpful