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

@Jessica28 , You cannot Call BR directly in UI Action , if you want to Run script which has on Business just Bring the Code and Enter into Server Side Script in UI Action 

 

Suggestion 2

You can design Script Include and Include your BR code In it and you can call into UI Action with following Syntax

 

new ScriptIncludename().function(); //Pass your SI name and Functionname 
Please mark this as helpful and accept as a solution if this resolves your Ask.
Regards,

Shyamkumar

shikhatyagi
Kilo Sage

Hi @Jessica28 

 

Better to call script include function instead of calling server side code under if else loop.

You can see the below OOB code for your reference.

 

/sys_ui_action.do?sys_id=f7b7a87a0f69111048d04abec4767e1a

 

Please Mark this Helpful and Accepted Solution if it solves your issue.

 

Thanks & Regards,

Shikha Tyagi

Hi @shikhatyagi 

Thank you for your suggestion.  However, I did not understand how to use script include function to call server side code.

Hi @Jessica28 

 

Please use below code in client callable UI action.

 

function confirmUpdate() {
    var ans = confirm('This will update all records! Are you sure?');
       var ga = new GlideAjax('IncidentUpdates');
        ga.addParam('sysparm_name','updaterecords');
        ga.addParam('sysparm_sys_id',g_form.getUniqueValue());
        ga.addParam('sysparm_sure', ans);
        ga.getXMLAnswer(responseCallBack);


function responseCallBack(response) {
               alert("You have selected "+ response +" to update records");
        }
}
 
Script Include: client callable
updaterecords:function(){
var ans = this.getParameter("sysparm_sure");
var id=this.getParameter("sysparm_sys_id");
gs.info("shikha "+ans+id)
var gr=new GlideRecord("incident");
gr.addQuery("sys_id",id);
gr.query();
if(gr.next()){
if(ans=='true')
{
    gr.comments=" Records updated successfully";
    gr.update();
    return true;
}
else{
    gr.comments="No need to update records";
    gr.update();
    return false;
}
}

},
 
 

Please Mark this Helpful and Accepted Solution if it solves your issue.

 

Thanks & Regards,

Shikha Tyagi

 

Maddysunil
Kilo Sage

@Jessica28 

Yes, you can indeed have different server-side actions based on the conditions in your client-side script. However, you can't directly call a server-side script from the client-side "else" block. Instead, you would typically use an AJAX call to invoke a specific Script Include or another server-side script.

 

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'); // Replace 'MyUpdateHandlerScript' with the name of your Script Include or server-side script
        ga.addParam('sysparm_name', 'otherUpdateAction'); // Define the specific action you want to trigger
        ga.getXMLAnswer(function(response) {
            // Handle the response here if needed
            alert(response); // For example, alert the response from the server-side action
        });
    }
}

 

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

 

Thanks