Please help with UI Action script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 11:09 PM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 12:09 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 12:24 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 01:11 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 01:27 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 10:20 AM
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.
2. The UI Action has popup message with two functions: OK and Cancel buttons
3. Closing the task if the user selects "Cancel", This function should be trigging:
I'm still encountering difficulties with the codes, please continue to help. Thank you