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

Tony Chatfield1
Kilo Patron

Hi, unfortunately your post contains no clear details of your issue or your debugging, and the community can only guess at what's going on based on the limited information provided.

 

What are the results of your debugging.
Is the received parameter 'state' value correct?

 - try logging in_state and typeof in_state, after in_state is instantiated\request.queryParams.state is assigned to it.

If in_state has the expected value, is it in the case\format expected by your switch statement?

Otherwise,
I can run your code in a PDI background window using a hard coded value for 'in_state',

testing that there is expected output from Incident table in the array 'answer' after your while loop via

 

var test = JSON.stringify(answer[0]);
gs.info(test);

 

The only potential issue I can see is that the values you have mapped in your switch, are not all OOB state values for Incident and so unless you are using custom values in your instance the output for some 'in_state' values may be unexpected.

 

 

Hi, 

 

As mentioned in my post,  I want to pass the String values (New, In Progress etc.) to the State to fetch all the Incidents accordingly. 

In the DB the values of the State field is stored as Integer (New as 1, In Progress as 2, etc.). So when I pass the Integer value to the State field without the Switch statement then it is working fine. But I want to pass the String Values to the State field. To fulfill this I am seeking help from the Community.

 

Hope this clarifies to you. 

Hi, unfortunately your response does nothing more than repeat the limited details that you have already provided, with no further clarification of potential issues raised, or any additional diagnostics that would help identify your issue

.
As I indicated in my last post, your code works based on the details you have supplied; If a string 'state' is passed to the switch, matched Incidents are returned as an array of objects by your GlideQuery.

The only possible issue I see was mentioned above, and that is not all of your 'state' strings map to OOB Incident state values. Have you confirmed that the typeof and data content of inc_state matches your expectations?

The community cannot fix something that isn't broken, and we cannot understand what your issue is if you are not undertaking diagnostics and providing feedback.

 

Hi, 

Unfortunately, you came up with some assumptions without trying what I mentioned in the post and in my reply.

 

You should be aware that State is the Choice field in the Incident Mgmt form which is storing values as Integers. So if I pass the Integer value it is working fine so I didn't use Switch statement. This is what I tried to explain you again. But my Client wants to pass the String values (New, In Progress, Pending etc) to the State field not the Integer values (1,2,3...).

 

I want to design a solution where Client can pass the String values to the State filed in the Incident Mgmt form and that value can be read as Integer which Incident Mgmt forms understand.

 

Please don't represent yourself as a whole Community. If Community members feel to answer my question they will..