On domain separated instance, MID Selector sometimes returns sys_id of MID Servers that are not in the same domain as the caller, sometimes it leads to the error "Invalid mid server ID was found by CloudMidSelectionApi "

Ex: User calling MID Selector logic is in Domain A and the logic returns sys_id of MID Server in Domain B.

Steps to Reproduce

Example 1:

  1. Configure Domain Separation
  2. Take 3 users, Admin in global domain, acme user in acme domain and cisco in Cisco domain
  3. Create 2 MID servers in global domain, status Up and Validated
  4. Create 2 MID servers in acme domain, status Up and Validated
  5. Create 2 MID servers in cisco domain, status Up and Validated
  6. Run following script backgoudn as each domain user
var app = null; 
var capabilities = null;
var context = null;
var midSelector = new SNC.MidSelector();
var midservers =, null, null, null);
for(var i=0; i< midservers.length; i++)

var gr = new GlideRecord('ecc_agent');

Result and Expectation:
Run as Admin User:
Prints 6 sys ID of MID servers and 6 row count (expected)

Run as Acme user:
Prints 6 sys ID fo MID servers and 4 row count (expected 4 sys IDs and 4 row count)

Run as Cisco User
Prints 6 sys ID fo MID servers and 4 row count (expected 4 sys IDs and 4 row count)


Example 2

  1. Configure Domain Separation
  2. Install and configure two MID Servers, each in a different domain
  3. Configure MID Server user to be in the same domain as one of the above mid servers
  4. Create a scheduled script execution and execute the following script as the above MID Server user:
var app = '';
var capabilities = [{"capability":"Cloud Management"},{"capability":"AWS","value":"us-west-1"}];
var context = '"{\"location_name\":\"us-west-1\",\"capi_impl_name\":\"AWS Compute API\",\"endpoint_url\":null,\"correlation_id\":\"dce44f3cdb0fa704d49e9532ca9619e3\",\"capi_method_name\":\"ListHardwareTypes\",\"capi_interface_name\":\"Compute Interface\",\"targets\":null,\"service_account_id\":\"598329518172\",\"capi_method_parameters\":{\"Project\":\"$(CloudCredential.project_name)\",\"Endpoint\":\"$(CloudCredential.URL)\",\"Identity\":\"$(CloudCredential.access_key)\",\"Credentials\":\"$(CloudCredential.secret_key)\",\"CloudOperation\":\"ListHardwareTypes\",\"Provider\":\"aws-ec2\",\"AccountAliasName\":\"$(CloudCredential.Alias)\"}}"';

var midSelector = new SNC.MidSelector();
var midId = midSelector.selectMid(app, null, capabilities, context);

if (midId){
var agent = new GlideRecord('ecc_agent');
gs.log('Name: ' + + ', status: ' + agent.status + ', started: ' + agent.started + ', sys_id: ' + agent.sys_id);

var mid = new;

Verify in syslog that intermittently, sysid of MID Server in a different domain is returned


This problem has been fixed. If you are able to upgrade, review the Fixed In or Intended Fix Version fields to determine whether any versions have a planned or permanent fix and till that time use the below workaround.

When calling midSelector.SelectMid(...), replace with following,

var midSysIds = [];
var validMidSysIds = [];
var validMid = '';
midSysIds =, null, capabilities, context);
for( var midCount=0; midCount< midSysIds.length; midCount++) {
var midGr = new GlideRecord("ecc_agent");
if(midGr.isValid()) { 
validMidSysIds.push(midSysIds[midCount]); //you can return here from first valid MID to save some on performance. 


if(validMidSysIds.length != 0) {
var randomMid = Math.floor((Math.random() * validMidSysIds.length));
validMid = validMidSysIds[randomMid];
} else
gs.log('SNCTEST no Valid MID server found');

// Now we have everything set up - call the mid selector API to select one mid
return validMid;


Note: Workaround has very little performance overhead. 

Related Problem: PRB1323398

Seen In

There is no data to report.

Intended Fix Version

New York

Fixed In

London Patch 7
Madrid Patch 2

Safe Harbor Statement

This "Intended Fix Version" information is meant to outline ServiceNow's general product direction and should not be relied upon in making a purchasing decision. The information provided here is for information purposes only and may not be incorporated into any contract. It is not a commitment, promise, or legal obligation to deliver any material, code, or functionality. The development, release, and timing of any features or functionality described for our products remains at ServiceNow's sole discretion.

Associated Community Threads

There is no data to report.

Article Information

Last Updated:2019-06-13 04:14:24