REST api is returning different data then UI

babasali
Tera Contributor

Hi guys,

 

I'm using Change Request API to retrieve data about specific Change Requests.

I'm new to SN REST API.

 

I get the data, but it's different than what I see in the UI.

 

For example, In the UI there is a Change Request in state Closed.

When i pull it through REST (i login with the same user in the UI), I get the same ticket but it's state is New.

 

Anybody knows why does this happen and how can I solve it?

Thanks!

4 REPLIES 4

Mohan raj
Mega Sage

Hi @babasali,

Pass the query through endpoint to add filter to that table 

 

For example: 

Mohanraj_0-1689503066652.png

In above screen shot, I have add one query (state is not closed) in sysparm_query field in Rest Explorer API

 

if you want to add the same filter in Script then you below method

request.setQueryParameter('state!=3')

 

If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.

 

Hi,

This is the code I'm using (golang):

func GetSingleChangeRequest(user string, pass string, instanceURL string, sys_id string) (ChangeRequestResponse, error) {

	var response ChangeRequestResponse
	jsonBody := []byte(fmt.Sprintf(`{"sys_id": "%s"}`, sys_id))
	bodyReader := bytes.NewReader(jsonBody)

	requestURL := fmt.Sprintf("%s/api/sn_chg_rest/v1/change?sysparm_query=active=true^ORDERBYnumber", instanceURL)

	req, err := http.NewRequest(http.MethodPost, requestURL, bodyReader)
	req.SetBasicAuth(user, pass)
	if err != nil {
		return response, fmt.Errorf("error making http request: %s\n", err)
	}

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return response, fmt.Errorf("client: could not read response body: %s\n", err)
	}
	defer res.Body.Close()

	fmt.Printf("client: got response!\n")
	fmt.Printf("client: status code: %d\n", res.StatusCode)

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return response, fmt.Errorf("client: could not read response body: %s\n", err)
	}
	// fmt.Printf("client: response body: %s\n", body)

	// Unmarshal the JSON response into a struct
	err = json.Unmarshal(body, &response)
	if err != nil {
		return response, fmt.Errorf("Error unmarshaling JSON: %s", err)
	}

	return response, nil
}

 

Mohan raj
Mega Sage

Hi @babasali,

In your code the below line, has filter is Active is true AND ORDERBY number 

requestURL := fmt.Sprintf("%s/api/sn_chg_rest/v1/change?sysparm_query=active=true^ORDERBYnumber", instanceURL)

 

Add one more condition to get only state is not closed records 

requestURL := fmt.Sprintf("%s/api/sn_chg_rest/v1/change?sysparm_query=active=true^state!=3^ORDERBYnumber", instanceURL)

Please confirm the backend value of the choice closed in instance, In my PDI for closed the value is 3 so that I used state!=3

If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.

Thanks for replying.

I think you misread my question.

I have a ticket A. in the UI it's closed.

In the REST API it's open.

There is a mis synchronization between the data I see in the UI and the data I see through REST API.

 

Why is that happening?