- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2024 07:23 PM - edited 11-19-2024 07:29 PM
Hi Everyone,
I have a requirement where we have created one UI action on incident form. When we click on that button then new tab opens with our custom URL. In same UI action script once the pop-up tab opens, then we want to call a POST rest message and want to send email and password to certain API, so that we can login into that custom URL.
function carearWebhostPopup() {
var arUrl = "SITE URL";
var params = 'scrollbars=no,resizable=no,status=no,location=no,toolbar=no,menubar=no,width=1000,height=550,left=180,top=100';
var newWin = top.window.open(arUrl, '_blank', params);
newWin.opener = null;
try {
var r = new sn_ws.RESTMessageV2('custom rest message', 'Post creds');
r.setRequestHeader('Content-Type', 'application/json');
r.setStringParameter('email', 'example@gmail.com');
r.setStringParameter('password', 'pwd123');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
} catch (ex) {
var message = ex.message;
}
}
But this is not working as expected. The pop-up opens but it is not able to login with REST message. Can anyone help me with my code.
And I am not able to see any rest message being called from this UI action in OUTBOUND HTTP logs i.e. rest message is not being able called.
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2024 10:27 PM
@Abhijit Das7 Client Script (UI Action):
function carearWebhostPopup() {
var arUrl = "SITE URL";
var params = 'scrollbars=no,resizable=no,status=no,location=no,toolbar=no,menubar=no,width=1000,height=550,left=180,top=100';
// Open the pop-up
var newWin = top.window.open(arUrl, '_blank', params);
newWin.opener = null;
// Call the server-side script for REST API
var ga = new GlideAjax('LoginRestAPI');
ga.addParam('sysparm_name', 'sendCredentials');
ga.addParam('email', 'example@gmail.com');
ga.addParam('password', 'pwd123'); ga.getXMLAnswer(function(response) {
if (response) {
console.log('Login request sent successfully');
} else {
console.error('Failed to send login request');
}
});
}
Script Include (Server-Side Logic): Create a Script Include named LoginRestAPI with the following content:
var LoginRestAPI = Class.create();
LoginRestAPI.prototype = {
initialize: function() {},
sendCredentials: function() {
try {
var r = new sn_ws.RESTMessageV2('custom rest message', 'Post creds');
r.setRequestHeader('Content-Type', 'application/json');
r.setStringParameter('email', gs.getProperty('email')); r.setStringParameter('password', gs.getProperty('password'));
var response = r.execute();
var httpStatus = response.getStatusCode();
if (httpStatus === 200) {
return 'Success';
} else {
gs.error('Login failed: ' + response.getBody());
return 'Failure';
}
} catch (ex) {
gs.error('Error in REST call: ' + ex.message);
return 'Error';
}
},
type: 'LoginRestAPI'
};
Ensure the REST Message is correctly configured with name custom rest message and HTTP method Post creds.
Use placeholders in the REST Message variables, e.g., {{email}} and {{password}}.And comment log if not needed or feel free to modify as per your requirement.Hope this will help you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2024 09:24 PM
@Abhijit Das7 The issue in your script is that the REST API call is executed on the client side through the browser (via the UI Action's client script context), which does not support the sn_ws.RESTMessageV2 API. This API is a server-side API and cannot be executed in a client-side context.
This API works only on the server-side. Hence, the GlideAjax call you can use to invoke a server-side Script Include to handle the REST API logic.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2024 09:43 PM
If possible can you please provide the code examples for me understand it clearly.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2024 10:27 PM
@Abhijit Das7 Client Script (UI Action):
function carearWebhostPopup() {
var arUrl = "SITE URL";
var params = 'scrollbars=no,resizable=no,status=no,location=no,toolbar=no,menubar=no,width=1000,height=550,left=180,top=100';
// Open the pop-up
var newWin = top.window.open(arUrl, '_blank', params);
newWin.opener = null;
// Call the server-side script for REST API
var ga = new GlideAjax('LoginRestAPI');
ga.addParam('sysparm_name', 'sendCredentials');
ga.addParam('email', 'example@gmail.com');
ga.addParam('password', 'pwd123'); ga.getXMLAnswer(function(response) {
if (response) {
console.log('Login request sent successfully');
} else {
console.error('Failed to send login request');
}
});
}
Script Include (Server-Side Logic): Create a Script Include named LoginRestAPI with the following content:
var LoginRestAPI = Class.create();
LoginRestAPI.prototype = {
initialize: function() {},
sendCredentials: function() {
try {
var r = new sn_ws.RESTMessageV2('custom rest message', 'Post creds');
r.setRequestHeader('Content-Type', 'application/json');
r.setStringParameter('email', gs.getProperty('email')); r.setStringParameter('password', gs.getProperty('password'));
var response = r.execute();
var httpStatus = response.getStatusCode();
if (httpStatus === 200) {
return 'Success';
} else {
gs.error('Login failed: ' + response.getBody());
return 'Failure';
}
} catch (ex) {
gs.error('Error in REST call: ' + ex.message);
return 'Error';
}
},
type: 'LoginRestAPI'
};
Ensure the REST Message is correctly configured with name custom rest message and HTTP method Post creds.
Use placeholders in the REST Message variables, e.g., {{email}} and {{password}}.And comment log if not needed or feel free to modify as per your requirement.Hope this will help you.