How can I get variables from service catalog and pass them in a POST message?

fwgfs
Kilo Contributor

I am new with servicenow and I'm trying to do what I describe below.

I have this service catalog:

find_real_file.png

And I have an outbound rest message that sends the following parameters in the content:

 
{
  "templateParameters": {
    "environment": "test",
    "team": "test",
    "vm_type": "linux" ,
    "vm_number": "1",
    "vm_size": "large",
    "data_diks_additional": "No",
    "vm_password": "test",
    "data_diks_adittional_number": "0",
  }
}

But what I need to do - and I have no idea how to do it - is to get the variables from the service catalog and put them in the content of the parameters. I think I can do it in the workflow; I read in the documentation that I can do something like

In the request:

"environment": ${environment}

In the workflow script:

var environment = current.variables.environment;

Any suggestion about how to do it?

1 ACCEPTED SOLUTION

Sagar Agarwal
Mega Guru

Hi,

 

I think the below script can give you an idea of how you can map variable values to the JSON context.

// you can do this in run script in workflow or from where ever you are executing the REST API
// current is the current context of RITM record
var context = {
	templateParameters: {
		environment: current.variables.environment.toString(),
		team: current.variables.team.toString(),
		vm_type: current.variables.vm_type.toString(),
		vm_number: current.variables.vm_number.toString(),
		vm_size: current.variables.vm_size.toString(),
		data_diks_additional: current.variables.data_disk_additional.toString(),
		vm_password: current.variables.vm_password.toString(),
		data_diks_adittional_number: current.variables.data_disk_add_num.toString(),
	},
};

 

If my answer helped you in any way, please then mark it as helpful.

Kind regards,

Sagar

View solution in original post

8 REPLIES 8

sachin_namjoshi
Kilo Patron
Kilo Patron

You need to use REST API methods to pass variables from your catalog item to a REST API.

Take a look into below

 

https://developer.servicenow.com/dev.do#!/reference/api/rome/server/c_RESTMessageV2API

 

Regards,

Sachin

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

yes you can use workflow run script and use REST Message in script

For those variables you can use variable substitution

OR

you can also use REST Activity in workflow

Regards
Ankur

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

Sagar Agarwal
Mega Guru

Hi,

 

I think the below script can give you an idea of how you can map variable values to the JSON context.

// you can do this in run script in workflow or from where ever you are executing the REST API
// current is the current context of RITM record
var context = {
	templateParameters: {
		environment: current.variables.environment.toString(),
		team: current.variables.team.toString(),
		vm_type: current.variables.vm_type.toString(),
		vm_number: current.variables.vm_number.toString(),
		vm_size: current.variables.vm_size.toString(),
		data_diks_additional: current.variables.data_disk_additional.toString(),
		vm_password: current.variables.vm_password.toString(),
		data_diks_adittional_number: current.variables.data_disk_add_num.toString(),
	},
};

 

If my answer helped you in any way, please then mark it as helpful.

Kind regards,

Sagar

I had tried something like that, but wasn't lucky. So, what I did now was to set a variable in the HTTP content and I overwrite that variable in an script, but I get the error message "Cannot convert null to an object", here is the code:

 

var reqBody = {
	templateParameters: {
		environment: current.variables.environment,
		team: current.variables.team,
		vm_type: current.variables.vm_type,
		vm_number: current.variables.vm_number,
		vm_size: current.variables.vm_size,
		data_diks_additional: current.variables.data_disk_additional,
		data_diks_adittional_number: current.variables.data_disk_add_num
	},
};

try {
	
	var sReqBodyData = JSON.stringify(reqBody);
	
    var r = new sn_ws.RESTMessageV2('Dummy REST API', 'POST');
    r.setStringParameterNoEscape('messageBody', sReqBodyData);

    var response = r.execute();

    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();

    gs.print(JSON.parse(responseBody));
} catch (ex) {
    var message = ex.message;
    gs.print(message);
}