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
Tera 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

try this

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', JSON.stringify(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);
}

Regards
Ankur

@fwgfs 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

I had more errors than just this, but it's worth to say that before using the variables in the json, they must be declared in the script just like this:

 

var environment = current.variables.environment.toString();
var team = current.variables.team.toString();
var vm_type = current.variables.vm_type.toString();
var vm_number = current.variables.vm_number.toString();
var vm_size = current.variables.vm_size.toString();
var data_diks_additional = current.variables.data_diks_additional.toString();
var data_diks_additional_number = current.variables.data_diks_additional_number.toString();


var reqBody = {
	"templateParameters": {
		"environment": environment,
		"team": team,
		"vm_type": vm_type,
		"vm_number": vm_number,
		"vm_size": vm_size,
		"data_diks_additional": data_diks_additional,
		"data_diks_additional_number": data_diks_additional_number
	}
};

Truly thankful with all you for your help 🙂

Ram Dhangar
Tera Contributor

Even I am facing the same problem I have a rest message created which accepts some query parameter with the same logic I want to use the ServiceNow catalog variable as input please reply to me ............

 

I have attached the query parameter ss and catalog form