How to make a REST API call through a MID Server?

Smith Johnson
Tera Guru

Hello all,

I have the following REST API call inside my script include.

However, instead of this call, I want to configure a call to the third system through a MID server.

At first I created a new MID Server on my instance with all the needed information (name "myMID")

How can I use this MID server inside my code?

My code is the following:

var r=new sn_ws.RESTMessageV2();

//endpoint
r.setEndpoint("aURL");   //set a url endpoint (third system's endpoint)

r.setHttpMethod("post");  //HTTP method

//set the queryParameters (the first parameter is the name of the parameter that the third system is waiting for)
r.setQueryParameter("id",id);      //id is passed from catalog client script
r.setQueryParameter("t",t); //t is passed from catalog client script

var response=r.execute();    //send the REST API call
var responseBody=response.getBody();   //receive back the response


var answer=JSON.parse(responseBody);

 ...

 

How can send this call from the MID server??

1 ACCEPTED SOLUTION

Hi,

Points below

You are not able to connect to that API from Postman since it is in client's network

1) I configured the MID server on my instance and I put it in the http request tab of an outgoing message. Do I need to make anything else?

- The MID Server should be in client's internal network and you should check by hitting the endpoint of API when you login to MID Server to confirm if mid server can reach. If not get client's network team involved and allow MID Server machine's IP address to be reaching to that client API

2) I guess I need somehow to install the mid server on the client's network? Am I right?

- Yes

3) To make the call, I just need to use the outbound message that uses the mid server?

- Yes; ensure you first test the endpoint from mid server

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

45 REPLIES 45

Hey @Ankur Bawiskar,

I am back with an update.

 

It seems that the mid server configuration works 🙂

Thank you so much for your support.

 

Just a final question before closing this thread.

As I have described, within my record producer I have a field ("A"). On change of field A, a catalog client script is made, which makes in turn a call to a script include. The script include, further, takes the value of field A and makes a rest api call to a third system. If this value exists on the third system, a new record is created within a custom table. Else, an error (alert box) pops up in the record producer that asks the user to enter another value.

My problem is that when the user enters the value on field A, the response is given back in 2 seconds approximately. Within these 2 seconds, the user can submit the form even though an error alert was going to popup.

 

Can I somehow based on your expertise, to receive first the response from the script include within my record producer and based on that to prevent or not the form submission?

Hi,

Since the process is asynchronous you cannot handle it directly.

You can use logic mentioned in links below

Using synchronous GlideAjax in on submit client script for Service Portal

getXMLwait alternative for Service Portal

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Smith Johnson

Let me know if I have answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

As a warning, when validating entry of a field with onChange(), it is also necessary to validate in onSubmit() because a user will be able to submit the form even when there is an error in a field.

From my experience, doing an ajax call in onSubmit() doesn't work too well especially when using async call because the form will get processed before the value is return.

Hey @Hitoshi Ozawa,

this is exactly the issue that I face right now.

The user is able to submit the form even if there is an error in a field, since I am validating entries only onChange client scripts.

Any ideas how can I fix this issue??