Passing State as String in the Query Parameter

NehaSNOW
Tera Guru

Hi Team,

 

I have designed a GET method where I want to fetch all the Incidents by passing the Query parameter as State. I am trying to design the Scripted REST API in a way that it should take State as a String and return the result accordingly.

For example, I want to pass the State as New, In Progress etc rather than passing 1,2,3.....

 

I have designed the below Scripted REST API which is not working. I think the system is not accepting the State as String. 

 

Any idea how to achieve this?

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
 
    // implement resource here
var answer = [];
 
var in_state = request.queryParams.state;
var incstate ;
 
  
   switch (in_state) {
    case "New":
      incstate = 1;
      break;
    case "In Progress":
      incstate = 2;
      break;
    case "Pending":
      incstate = 3;
      break;
    case "Resolve":
      incstate = 4;
      break;
    case "Close":
      incstate = 5;
      break;
    case "Cancel":
      incstate = 6;
    }
 
var incGR = new GlideRecord('incident');
    incGR.addQuery('state',incstate);
incGR.query();
 
while(incGR.next()){
 
var incObj ={
"Incident Number":incGR.getValue('number'),
"short description":incGR.getValue('short_description'),
"state":incstate
};
 
answer.push(incObj);
}
response.setBody(answer);
 
})(request, response);
 
 

 

7 REPLIES 7

Hi, as I indicated earlier in this thread, based on the limited information that you have provided your solution works as it was initially configured, although the initial input or values you are mapping may not be correct.


I am not sure how\why my attempts to assist you have resulted in your last post,
but as I have spent my own personal time attempting to assist you in resolution of an issue that you are being paid to deliver I am a little disappointed by your reply.

As you indicated, I do not represent the entire community (and have never claimed too);

But I like to think I am representative of the communities underpinning ethos, in that I provide positive support and guidance when I can. What community members choose to do with my support and feedback is entirely up to the individual.

 

Hopefully another member of the community can provide you with a better outcome.



 

 

Community Alums
Not applicable

Hi @NehaSNOW ,

1. Use-- /{state} in the relative path.

2. replace in_state with this  "var in_state = request.pathParams.state" in the script.

It's working fine for me.

Hi @Community Alums 

 

I tried it and it is working fine with Path Parameter but not with Query Parameter. Wondering why it is behaving like this? Is there any solution?