Looking for examples or scenarios where pathparams and queryparams can be used in scripted rest api

Suggy
Giga Sage

Hello....

 

I know how pathparams and queryparams work in scritped rest api. but I feel both are the same or dedundant. ie I think both can be used interchangeable.

 

Can anyone give examples or scenarios where pathparams and queryparams can be used in scripted rest api where each has got its own significance?

6 REPLIES 6

Suggy
Giga Sage

ANYONE?

Rajdeep Ganguly
Mega Guru

Sure, both path parameters and query parameters are used in Scripted REST APIs in ServiceNow, but they serve different purposes and are used in different scenarios. Here's a summary: Path Parameters: - Path parameters are part of the URL path. They are used to identify a specific resource or resources. - They are useful when you want to get a specific record or set of records. For example, if you have a REST API for users, you might have a path like "/users/{userID}" where userID is a path parameter. - Path parameters are mandatory. The API will not work if they are not provided. Query Parameters: - Query parameters are appended to the URL after a '?' symbol. They are used to filter or sort the data that is returned by the API. - They are useful when you want to limit the data that is returned. For example, you might have a query parameter like "?status=active" to only return active users. - Query parameters are optional. The API will still work if they are not provided, but it might return more data than you need. Here's an example of how you might use both in a Scripted REST API in ServiceNow: 1. Create a Scripted REST API with a resource path like "/users/{userID}". 2. In your script, use request.pathParams.userID to get the userID from the path parameter. 3. Use this userID to get the specific user record from the database. 4. Also check if there is a query parameter for the status. You can do this with request.queryParams.status. 5. If the status query parameter is provided, use it to filter the user records that are returned. 6. Return the filtered user records in the response. This way, you can use the path parameter to specify which user records you want, and the query parameter to further filter these records.

sumanta pal
Kilo Guru

Sure, I can provide some examples where path parameters and query parameters have their own significance in a Scripted REST API. Path Parameters: - Path parameters are part of the URL path. They are used to identify a specific resource or resources. For example, if you have a REST API for a library system, you might have a URL like /books/{bookId} where {bookId} is a path parameter representing a specific book. - Path parameters are mandatory. The API will not work if they are not provided. - They are used when the value is required for the server to perform its operation. - They are not easily modifiable by the client as they are part of the URL. Query Parameters: - Query parameters are appended to the URL with a '?' and are usually used to filter the response or to change the response's format. For example, /books?author=John. - Query parameters are optional. The API will still work if they are not provided, but the response might be different. - They are used when the value is optional and can be used to modify the response or request. - They are easily modifiable by the client as they are part of the query string. Here is a summary: - Path parameters are used to identify a specific resource or resources, are mandatory, and are not easily modifiable by the client. - Query parameters are used to filter or change the response, are optional, and are easily modifiable by the client.

Based on your description, it seems like you are trying to apply a project template to a project record during a data import using a transform map script. However, the Gantt chart is not being created in the Planning Console. Here are some possible reasons and solutions: 1. **Check the Template**: Ensure that the project template you are applying has all the necessary tasks and dependencies defined. If the template is empty or not properly configured, the Gantt chart may not display as expected. 2. **Check the Script**: Make sure that the script is correctly written and there are no syntax errors. The script you provided seems to be correct, but ensure that the sys_id '24f3dc361b73ad101336a6cee54bcb00' is valid and corresponds to the correct project template. 3. **Check the Transform Map**: Ensure that the transform map is correctly configured and the script is placed in the correct script field. If you are using an 'onBefore' script, it should be placed in the 'onBefore' script field of the transform map, and similarly for 'onAfter'. 4. **Check the Project Record**: Make sure that the project record is correctly created and saved before navigating to the Planning Console. If the project record is not saved, the Gantt chart may not display. 5. **Check the Planning Console**: Ensure that the Planning Console is correctly configured to display Gantt charts. If there are any configuration issues with the Planning Console, the Gantt chart may not display. 6. **Check ServiceNow Version**: Ensure that your ServiceNow instance is up-to-date. Some features may not work as expected in older versions of ServiceNow. If you have checked all these points and the issue still persists, it might be a good idea to raise a ticket with ServiceNow support for further investigation.