- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 02:45 AM - edited 01-06-2025 02:47 AM
Hi Experts,
I have created a portal widget.
on which we have custom buttons like "close", "Edit" and "void and create new"
HTML
<div class="panel panel-default">
<div class="panel-body" style="display: flex; gap: 10px; align-items: center;">
<button type="button" class="btn btn-default"
ng-repeat="action in data.actions"
ng-click="c.uiAction(action, c.data.table, c.data.sys_id, c.data.number)">
{{action}}
</button>
</div>
</div>
Client Script:
api.controller = function(spModal) {
/* widget controller */
var c = this;
// Function to open the page as a modal popup
c.uiAction = function(actionName, tableName, sysID, recordNumber) {
console.log('Button clicked:', actionName);
console.log('Table:', tableName);
console.log('Sys ID:', sysID);
console.log('Number:', recordNumber);
if (actionName == "Close") {
spModal.open({
title: 'Consent Form',
message: 'Once closed, this form cannot be used again.',
buttons: [{
label: '✔ ${Confirm}',
primary: true
},
{
label: '✘ ${Cancel}',
cancel: true
}
]
}).then(function() {
c.agree = 'yes';
c.data.state = '3';
c.server.update().then(function() {
c.data.state = '';
});
});
}
if (actionName == "Edit") {
spModal.open({
title: 'Consent Form',
message: 'Are you sure you want to edit the form?',
}).then(function() {
c.agree = 'yes';
c.data.state = '10';
c.server.update().then(function() {
c.data.state = '';
});
});
}
if (actionName == "Void and create new") {
c.data.action = actionName;
spModal.open({
title: 'Consent form',
message: "Are you sure you wish to void this and create a copy?",
}).then(function() {
c.agree = 'yes';
});
}
};
};
server script:
(function() {
// Get table & sys_id
var tableName = $sp.getParameter('table');
var sys_id = $sp.getParameter('sys_id');
var record = new GlideRecord(tableName);
if (record.get(sys_id)) {
number = record.getValue('number');
}
// Check if we are updating the state
if (input && input.state) { // for state is close and edit
var consent = new GlideRecord(tableName);
consent.addQuery('sys_id', sys_id);
consent.query();
if (consent.next()) {
consent.state = input.state;
consent.update();
gs.addInfoMessage("Record Updated");
}
}
var actions = [];
// Map of button names to their conditions
var actionConditions = {
'Close': function(current) {
return current.state == '1';
},
'Edit': function(current) {
return current.state == '1';
},
'Void and create new': function(current) {
return current.state == '1';
} // Add more conditions for new buttons.
};
var current = new GlideRecord(tableName);
if (current.get(sys_id)) {
var grUIAction = new GlideRecord('sys_ui_action');
grUIAction.addQuery('table', tableName);
grUIAction.addQuery('client', true);
grUIAction.addQuery('active', true);
grUIAction.orderBy('name');
grUIAction.query();
while (grUIAction.next()) {
var userHasRole = false;
// Check if the user has the required role
var grRole = new GlideRecord('sys_ui_action_role');
grRole.addQuery('sys_ui_action', grUIAction.sys_id);
grRole.query();
while (grRole.next()) {
if (gs.hasRole(grRole.sys_user_role.name)) {
userHasRole = true;
break;
}
}
if (userHasRole) {
var actionName = grUIAction.getValue('name');
// Check if a condition exists for this action and evaluate it
if (actionConditions[actionName]) {
if (actionConditions[actionName](current)) {
actions.push(actionName);
}
} else {
// No specific condition, include the action
actions.push(actionName);
}
}
}
}
data.actions = actions;
data.table = tableName;
data.sys_id = sys_id;
data.number = number;
function voidAndCreateNewRecord(sysID, tableName) {
var current = new GlideRecord(tableName);
if (current.get(sysID)) {
current.state = '8'; // Void state
current.update();
}
// Create a new record as a copy of the current record
var newRecord = new GlideRecord(tableName);
newRecord.initialize();
newRecord.setWorkflow(false);
newRecord.state = '2'; // In progress
newRecord.active = true;
newRecord.assigned_to = gs.getUserID();
newRecord.insert();
}
})();
when void and create new button is clicked popup will appear when confirmed it has to update the record and create new record and the page should reload to the newly created record
i tried c.server.get but it did not work.
any solution is much appreciated.
Thankyou
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 03:36 AM
can you try this?
Client controller
api.controller = function(spModal, $window) {
/* widget controller */
var c = this;
// Function to open the page as a modal popup
c.uiAction = function(actionName, tableName, sysID, recordNumber) {
console.log('Button clicked:', actionName);
console.log('Table:', tableName);
console.log('Sys ID:', sysID);
console.log('Number:', recordNumber);
if (actionName == "Close") {
spModal.open({
title: 'Consent Form',
message: 'Once closed, this form cannot be used again.',
buttons: [{
label: '✔ ${Confirm}',
primary: true
},
{
label: '✘ ${Cancel}',
cancel: true
}
]
}).then(function() {
c.agree = 'yes';
c.data.state = '3';
c.server.update().then(function() {
c.data.state = '';
});
});
}
if (actionName == "Edit") {
spModal.open({
title: 'Consent Form',
message: 'Are you sure you want to edit the form?',
}).then(function() {
c.agree = 'yes';
c.data.state = '10';
c.server.update().then(function() {
c.data.state = '';
});
});
}
if (actionName == "Void and create new") {
spModal.open({
title: 'Consent form',
message: "Are you sure you wish to void this and create a copy?",
}).then(function() {
c.agree = 'yes';
c.server.get({
action: actionName,
table: tableName,
sys_id: sysID
}).then(function(response) {
if (response.data.new_sys_id) {
$window.location.href = '/nav_to.do?uri=' + tableName + '.do?sys_id=' + response.data.new_sys_id;
}
});
});
}
};
};
Server Side:
(function() {
// Get table & sys_id
var tableName = $sp.getParameter('table');
var sys_id = $sp.getParameter('sys_id');
var record = new GlideRecord(tableName);
if (record.get(sys_id)) {
number = record.getValue('number');
}
// Check if we are updating the state
if (input && input.state) { // for state is close and edit
var consent = new GlideRecord(tableName);
consent.addQuery('sys_id', sys_id);
consent.query();
if (consent.next()) {
consent.state = input.state;
consent.update();
gs.addInfoMessage("Record Updated");
}
}
var actions = [];
// Map of button names to their conditions
var actionConditions = {
'Close': function(current) {
return current.state == '1';
},
'Edit': function(current) {
return current.state == '1';
},
'Void and create new': function(current) {
return current.state == '1';
} // Add more conditions for new buttons.
};
var current = new GlideRecord(tableName);
if (current.get(sys_id)) {
var grUIAction = new GlideRecord('sys_ui_action');
grUIAction.addQuery('table', tableName);
grUIAction.addQuery('client', true);
grUIAction.addQuery('active', true);
grUIAction.orderBy('name');
grUIAction.query();
while (grUIAction.next()) {
var userHasRole = false;
// Check if the user has the required role
var grRole = new GlideRecord('sys_ui_action_role');
grRole.addQuery('sys_ui_action', grUIAction.sys_id);
grRole.query();
while (grRole.next()) {
if (gs.hasRole(grRole.sys_user_role.name)) {
userHasRole = true;
break;
}
}
if (userHasRole) {
var actionName = grUIAction.getValue('name');
// Check if a condition exists for this action and evaluate it
if (actionConditions[actionName]) {
if (actionConditionsactionName) {
actions.push(actionName);
}
} else {
// No specific condition, include the action
actions.push(actionName);
}
}
}
}
data.actions = actions;
data.table = tableName;
data.sys_id = sys_id;
data.number = number;
if (input && input.action == "Void and create new") {
data.new_sys_id = voidAndCreateNewRecord(sys_id, tableName);
}
function voidAndCreateNewRecord(sysID, tableName) {
var current = new GlideRecord(tableName);
if (current.get(sysID)) {
current.state = '8'; // Void state
current.update();
}
// Create a new record as a copy of the current record
var newRecord = new GlideRecord(tableName);
newRecord.initialize();
newRecord.setWorkflow(false);
newRecord.state = '2'; // In progress
newRecord.active = true;
newRecord.assigned_to = gs.getUserID();
var newSysID = newRecord.insert();
return newSysID;
}
})();
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 03:36 AM
can you try this?
Client controller
api.controller = function(spModal, $window) {
/* widget controller */
var c = this;
// Function to open the page as a modal popup
c.uiAction = function(actionName, tableName, sysID, recordNumber) {
console.log('Button clicked:', actionName);
console.log('Table:', tableName);
console.log('Sys ID:', sysID);
console.log('Number:', recordNumber);
if (actionName == "Close") {
spModal.open({
title: 'Consent Form',
message: 'Once closed, this form cannot be used again.',
buttons: [{
label: '✔ ${Confirm}',
primary: true
},
{
label: '✘ ${Cancel}',
cancel: true
}
]
}).then(function() {
c.agree = 'yes';
c.data.state = '3';
c.server.update().then(function() {
c.data.state = '';
});
});
}
if (actionName == "Edit") {
spModal.open({
title: 'Consent Form',
message: 'Are you sure you want to edit the form?',
}).then(function() {
c.agree = 'yes';
c.data.state = '10';
c.server.update().then(function() {
c.data.state = '';
});
});
}
if (actionName == "Void and create new") {
spModal.open({
title: 'Consent form',
message: "Are you sure you wish to void this and create a copy?",
}).then(function() {
c.agree = 'yes';
c.server.get({
action: actionName,
table: tableName,
sys_id: sysID
}).then(function(response) {
if (response.data.new_sys_id) {
$window.location.href = '/nav_to.do?uri=' + tableName + '.do?sys_id=' + response.data.new_sys_id;
}
});
});
}
};
};
Server Side:
(function() {
// Get table & sys_id
var tableName = $sp.getParameter('table');
var sys_id = $sp.getParameter('sys_id');
var record = new GlideRecord(tableName);
if (record.get(sys_id)) {
number = record.getValue('number');
}
// Check if we are updating the state
if (input && input.state) { // for state is close and edit
var consent = new GlideRecord(tableName);
consent.addQuery('sys_id', sys_id);
consent.query();
if (consent.next()) {
consent.state = input.state;
consent.update();
gs.addInfoMessage("Record Updated");
}
}
var actions = [];
// Map of button names to their conditions
var actionConditions = {
'Close': function(current) {
return current.state == '1';
},
'Edit': function(current) {
return current.state == '1';
},
'Void and create new': function(current) {
return current.state == '1';
} // Add more conditions for new buttons.
};
var current = new GlideRecord(tableName);
if (current.get(sys_id)) {
var grUIAction = new GlideRecord('sys_ui_action');
grUIAction.addQuery('table', tableName);
grUIAction.addQuery('client', true);
grUIAction.addQuery('active', true);
grUIAction.orderBy('name');
grUIAction.query();
while (grUIAction.next()) {
var userHasRole = false;
// Check if the user has the required role
var grRole = new GlideRecord('sys_ui_action_role');
grRole.addQuery('sys_ui_action', grUIAction.sys_id);
grRole.query();
while (grRole.next()) {
if (gs.hasRole(grRole.sys_user_role.name)) {
userHasRole = true;
break;
}
}
if (userHasRole) {
var actionName = grUIAction.getValue('name');
// Check if a condition exists for this action and evaluate it
if (actionConditions[actionName]) {
if (actionConditionsactionName) {
actions.push(actionName);
}
} else {
// No specific condition, include the action
actions.push(actionName);
}
}
}
}
data.actions = actions;
data.table = tableName;
data.sys_id = sys_id;
data.number = number;
if (input && input.action == "Void and create new") {
data.new_sys_id = voidAndCreateNewRecord(sys_id, tableName);
}
function voidAndCreateNewRecord(sysID, tableName) {
var current = new GlideRecord(tableName);
if (current.get(sysID)) {
current.state = '8'; // Void state
current.update();
}
// Create a new record as a copy of the current record
var newRecord = new GlideRecord(tableName);
newRecord.initialize();
newRecord.setWorkflow(false);
newRecord.state = '2'; // In progress
newRecord.active = true;
newRecord.assigned_to = gs.getUserID();
var newSysID = newRecord.insert();
return newSysID;
}
})();
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 04:18 AM
Thankyou @Ankur Bawiskar
The solution worked