- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2022 01:30 PM
I am creating a Scripted REST API for the change request. I want to pull from a json file and query fields to create a change request
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2022 02:18 PM
Hello
usually the syntax of glide record is like below
var myObj = new GlideRecord('table_name');
// 2. Build query
myObj.addQuery('field_name','operator','value');
myObj.addQuery('field_name','operator','value');
// 3. Execute query
myObj.query();
while(myObj.next())
{
//Logic you want to execute.
//Use myObj.field_name to reference record fields
}
So in your case it would be like this
It depends on the type of field also if its a reference we need to query with sys_id as reference accepts only sys_ids , if its string type you can query with string ....as in your script services and assignment group are references you need to use sys_ids to compare
Also make sure you are using correct back end names of fields. (services ,short_description)etc;
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var requestBody = request.body.data;
var change = new GlideRecord('change_request');
change.addQuery('services','your services sys_id');
change.addQuery('short_description','your short description');
change.addQuery('description','your description value');
change.addQuery('assignment_group','your assignement group sys_id');
change,query();
if(change.next())
{
//execute your logic
}
})(request, response);
please mark my answer correct if it helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2022 01:32 PM
Hello
Can you please send the JSON, as it completely depends on how you aprse the json to get short description. and description and then query change requests with those values
POST your JSON here and we will be able to help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2022 02:09 PM
Hi, I will need to update my request after getting further information.
How do you create a gliderecord that will populate specified fields on the change request form such as Services, Short Description, Description, and Assignment group.
So far I have :
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var requestBody = request.body.data;
var change = new GlideRecord('change_request');
change.addQuery('services');
change.addQuery('short_description');
change.addQuery('description');
change.addQuery('assignment_group');
})(request, response);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2022 02:18 PM
Hello
usually the syntax of glide record is like below
var myObj = new GlideRecord('table_name');
// 2. Build query
myObj.addQuery('field_name','operator','value');
myObj.addQuery('field_name','operator','value');
// 3. Execute query
myObj.query();
while(myObj.next())
{
//Logic you want to execute.
//Use myObj.field_name to reference record fields
}
So in your case it would be like this
It depends on the type of field also if its a reference we need to query with sys_id as reference accepts only sys_ids , if its string type you can query with string ....as in your script services and assignment group are references you need to use sys_ids to compare
Also make sure you are using correct back end names of fields. (services ,short_description)etc;
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var requestBody = request.body.data;
var change = new GlideRecord('change_request');
change.addQuery('services','your services sys_id');
change.addQuery('short_description','your short description');
change.addQuery('description','your description value');
change.addQuery('assignment_group','your assignement group sys_id');
change,query();
if(change.next())
{
//execute your logic
}
})(request, response);
please mark my answer correct if it helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2022 02:19 PM
Similar to this I found in the community. But I am writing a Scripted REST API.
var changerequest = Class.create();
changerequest.prototype = Object.extendsObject(AbstractAjaxProcessor, {
change: function() {
var chg = this.getParameter('sysparm_chg');
var gr_change = new GlideRecord('change_request');
gr_change.addQuery('sys_id', chg);
gr_change.query();
if (gr_change.next()) {
var user_obj1 = {};
user_obj1.area = gr_change.u_area.toString();
user_obj1.impactedlocation = gr_change.getDisplayValue("u_impacted_location");
user_obj1.service = gr_change.business_service.toString();
user_obj1.shortdec = gr_change.short_description.toString();
user_obj1.startdate = gr_change.start_date.toString();
user_obj1.enddate = gr_change.end_date.toString();
gs.info('user_obj1'+user_obj1);
}
return JSON.stringify(user_obj1);
},
type: 'changerequest'
});