RESTMessageV2 set MID Server by capability

Robert Beeman
Kilo Sage

When using scripted REST messages, is it possible to have it use the default MID server or to specify a capability if you have multiple MID servers. The setMIDServer method description sounds like I need to specify a specific MID server by name.

1 ACCEPTED SOLUTION

Hi Robert,



Then may be something like this would be useful for you



var gr =   new GlideRecord('ecc_agent_capability_m2m');


gr.addQuery("capability", "REST");


gr.query();


while(gr.next()) {


  if(gr.agent.status == "Up") {


  gs.log("Available Mid Servers" + gr.agent.name);


  }


}



If you are using Rest message in a client script then you might have to create a script include make a ajax call for the above script.


You can return the first agent name that is active and that should take care. You might have to add the rest capability to the mid server for this code to work.



Hope it helps.


View solution in original post

5 REPLIES 5

venkatiyer1
Giga Guru

Hi Robert,



You can do both. So say you have a script which is something like below



sm = new sn_ws.RESTMessageV2("Rest Message Name", "Method");



// Rest Message Name - If you search in navigator by Rest Message Outbound you can define a new one there with all the information that you require like endpoint url, authentication and use midserver too.



In the Use midserver we can assign the default midserver.


// Method would be post, get , put etc...


response =   sm.execute(); //to get response



If you want to do it by capability you can also do it based on a certain parameter or ip address etc..


Hi Venkat,



Thank you for the reply. I'm not using a REST message record (like # 3 in link below) because this integration has too many moving parts, but I am using the setMIDServer() method (like #5). It says I need to specify a mid server name (string), but I would like it to send it to any available mid server with the REST capability instead of specifying a single machine.



Scripting Outbound REST - ServiceNow Wiki


Hi Robert,



Then may be something like this would be useful for you



var gr =   new GlideRecord('ecc_agent_capability_m2m');


gr.addQuery("capability", "REST");


gr.query();


while(gr.next()) {


  if(gr.agent.status == "Up") {


  gs.log("Available Mid Servers" + gr.agent.name);


  }


}



If you are using Rest message in a client script then you might have to create a script include make a ajax call for the above script.


You can return the first agent name that is active and that should take care. You might have to add the rest capability to the mid server for this code to work.



Hope it helps.


Hi Venkat,



Thank you. I was thinking that there might be a RESTMessageV2 method that selected by capability, but with your suggestion I could select one by capability that is up, set its name to a variable, and use the setMIDServer() method with that variable.