Please help with UI Action script.

Jessica28
Tera Guru

Hello,

I am using UI action function to call server side code to update records (Only for test purposes).

Is it possible to use the "else" statement to call different server side code. If not, please provide suggestions.

Thank you

 

Here is the example code:

function confirmUpdate() {
    var answer = confirm('This will update all records! Are you sure?');
        if (answer) {
            gsftSumit(null, g_from.getFormElement(), 'updateConfirm');
        } else {
            //Need to trigger other server side code

        }
    }
    if (typeof window == 'undefined') {
        current.work_notes = "All records updated";
        current.update();
        action.setRediirectURL(current);
    }
14 REPLIES 14

@Maddysunil 

Here is the Business Rule code.  How do I incorporate in your suggested code?

var getSysId = current.getUniqueValue();
//duplicate the RITM
newSysId = current.insert();

//Copy attachments
var attach = new GlideSysAttachment();
attach.copy('sc_req_item', originalSysId, 'sc_req_item', newSysId);
action.setRedirectURL(current);

 

@Jessica28 

I think you can write your BR code in script include and you can call it from UI action function

'otherUpdateAction'. Below is the updated code you can use:

UI action:

 

function confirmUpdate() {
    var answer = confirm('This will update all records! Are you sure?');
    if (answer) {
        gsftSubmit(null, g_form.getFormElement(), 'updateConfirm');
    } else {
        // Trigger other server-side action using AJAX
        var ga = new GlideAjax('MyUpdateHandlerScript');
        ga.addParam('sysparm_name', 'otherUpdateAction');
        ga.getXMLAnswer(function(response) {
            // Handle the response here
            if (response === 'success') {
                alert('Other update action successful');
            } else {
                alert('Other update action failed');
            }
        });
    }
}

// Now, create a separate Script Include or server-side script to handle the 'otherUpdateAction'

 

Script include :

 

duplicateRITMAndCopyAttachments: function(originalSysId) {
        var newSysId;
        
        // Duplicate the RITM
        var newRITM = new GlideRecord('sc_req_item');
        if (newRITM.initialize() && newRITM.insert()) {
            newSysId = newRITM.getUniqueValue();
            
            // Copy attachments
            var attach = new GlideSysAttachment();
            attach.copy('sc_req_item', originalSysId, 'sc_req_item', newSysId);
            
            return newSysId;
        } else {
            gs.info("Failed to duplicate RITM");
            return null;
        }
    },
    
    otherUpdateAction: function() {
        var originalSysId = gs.getCurrentRecord().getUniqueValue();
        var newSysId = this.duplicateRITMAndCopyAttachments(originalSysId);
        if (newSysId) {
            gs.addInfoMessage("Other update action successful");
            return 'success';
        } else {
            gs.addErrorMessage("Other update action failed");
            return 'failure';
        }
    },
    

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

 

@Maddysunil 

Thank you for continuing to help. This is amazing how you could completed the code less than 10 minutes?

 

It will takes sometime to me to understand the code.  I screwed it up somewhere in the code, because I did not know what I was doing.  Could you please help review and make correction.

 

Here is the UI Action code:

function confirmUpdate() {
    var answer = confirm('This will update all records! Are you sure?');
    if (answer) {
        gsftSubmit(null, g_form.getFormElement(), 'updateConfirm');
    }
    // Trigger other server-side action using AJAX
    var ga = new GlideAjax('MyUpdateHandlerScript');
    ga.addParam('sysparm_name', 'otherUpdateAction');
    ga.getXMLAnswer(function(response) {
        // Handle the response here
        if (response === 'success') {
            alert('Other update action successful');
        } else {
            alert('Other update action failed');
        }
    });
}

//server side script
if (typeof window == 'undefined')
    updateTask();

function updateTask() {
    current.state = 3;
    current.update();
    action.setRedirectURL(current);
}
 
 
Here is the Script Include:
var MyUpdateHandlerScript = Class.create();
MyUpdateHandlerScript.prototype = {
    initialize: function() {
    },

    type: 'MyUpdateHandlerScript'
    duplicateRITMAndCopyAttachments: function(originalSysId) {
        var newSysId;
       
        // Duplicate the RITM
        var newRITM = new GlideRecord('sc_req_item');
        if (newRITM.initialize() && newRITM.insert()) {
            newSysId = newRITM.getUniqueValue();
           
            // Copy attachments
            var attach = new GlideSysAttachment();
            attach.copy('sc_req_item', originalSysId, 'sc_req_item', newSysId);
           
            return newSysId;
        } else {
            gs.info("Failed to duplicate RITM");
            return null;
        }
    },
   
    otherUpdateAction: function() {
        var originalSysId = gs.getCurrentRecord().getUniqueValue();
        var newSysId = this.duplicateRITMAndCopyAttachments(originalSysId);
        if (newSysId) {
            gs.addInfoMessage("Other update action successful");
            return 'success';
        } else {
            gs.addErrorMessage("Other update action failed");
            return 'failure';
        }
    },
};
 

 

@Jessica28 

Thanks, Your UI action code should trigger the server-side action only if the user confirms the update. Also, ensure that the GlideAjax call is within the 'if' block. Also In your Script Include, there is a syntax error. The 'type' property should be followed by a comma, and you have missed the comma. Also, ensure that the method definitions are separated by commas.

UI action:

 

function confirmUpdate() {
    var answer = confirm('This will update all records! Are you sure?');
    if (answer) {
        gsftSubmit(null, g_form.getFormElement(), 'updateConfirm');
    } else {
        // Trigger other server-side action using AJAX
        var ga = new GlideAjax('MyUpdateHandlerScript');
        ga.addParam('sysparm_name', 'otherUpdateAction');
        ga.getXMLAnswer(function(response) {
            // Handle the response here
            if (response === 'success') {
                alert('Other update action successful');
            } else {
                alert('Other update action failed');
            }
        });
    }
}

 

Script Include:

 

var MyUpdateHandlerScript = Class.create();
MyUpdateHandlerScript.prototype = {
    initialize: function() {
    },

    type: 'MyUpdateHandlerScript', // Add a comma here

    duplicateRITMAndCopyAttachments: function(originalSysId) {
        // Method implementation
    },

    otherUpdateAction: function() {
        // Method implementation
    }
};

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Hi @Maddysunil 

I sincerely apologize for the delayed response. I worked until 3:00 AM trying to make the code function, yet I'm still encountering difficulties. When I clicked on the "OK" button,  I'm getting " Other update action failed"

 

1. The objective is to create a UI Action button in the Catalog Task.

Jessica28_0-1711040150369.png

 

2. The UI Action has popup message with two functions:  OK and Cancel buttons 

Jessica28_1-1711040382928.png

 

3. Closing the task if the user selects "Cancel",  This function should be trigging:

function closeTask(){
    g_form.setValue('state', 3);
    //Call the UI Action and skip the 'onclick' function
    gsftSubmit(null, g_form.getFormElement(), 'close_sc_task');
}
//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
   updateTask();
function updateTask(){
current.state = 3;
current.update();
}
 
4. Creating a copy of the RITM if selected "OK". This function should be firing:
(function executeRule(current, previous /*null when async*/ ) {

    var originalSysId = current.getUniqueValue();
    current.setValue('number', '');
    //duplicate the RITM
    newSysId = current.insert();
 
    var mtomGR = new GlideRecord('sc_item_option_mtom');
    mtomGR.addQuery('request_item=' + originalSysId);
    mtomGR.query();
    while (mtomGR.next()) {
        var optionGR = new GlideRecord('sc_item_option');
        optionGR.get(mtomGR.sc_item_option.getValue());
        var newOptionSysId = optionGR.insert();
        var newMtomGR = new GlideRecord('sc_item_option_mtom');
        newMtomGR.initialize();
        newMtomGR.setValue('request_item', newSysId);
        newMtomGR.setValue('sc_item_option', newOptionSysId);
        newMtomGR.insert();
    }
    var mvrsGR = new GlideRecord('sc_multi_row_question_answer');
    mvrsGR.addEncodedQuery('parent_id=' + originalSysId);
    mvrsGR.query();
    while (mvrsGR.next()) {
        var optionMvrsGR = new GlideRecord('sc_item_option');
        optionMvrsGR.get(mvrsGR.sc_item_option.getValue());
        var optionMvrsSysId = optionMvrsGR.insert();
        mvrsGR.setValue('parent_id', newSysId);
        mvrsGR.setValue('sc_item_option', optionMvrsSysId);
        mvrsGR.insert();
    }
    // Copy attachmenth
    var attach = new GlideSysAttachment();
    attach.copy('sc_req_item', originalSysId, 'sc_req_item', newSysId);
    action.setRedirectURL(current);

})(current, previous);
 

 

I'm still encountering difficulties with the codes, please continue to help.  Thank you