Need to update Opened By in RITM via REST API

Sheveta
Giga Contributor

Hi,

I have created a REST API which is a POST API, and it will create the RITM data in Requested Items list. With these items I need to pass the Opened by name of the person logged in. When I tried to modify the API by adding the "opened_by" field it takes the default system administrator.

Any leads to resolve this will be highly appreciated.

Thanks

14 REPLIES 14

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Is this scripted rest API or out of the box table API?

How are you sending the values?

Regards

Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur, 

Please find the below code, which I'm using to POST values in SNOW using the REST API.
I want the "opened_by" field on the RITM to be populated with the logged in credentials of third party user who is raising the request.

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
try{
var obj = request.body.data;
var sys = new GlideRecord('sc_cat_item');
sys.get('name',obj.item_name);
gs.info('test cat'+ sys.name);

var cat = sys.sys_id.toString();
gs.info('what is sys id '+ cat);

var a = processCart(cat, obj);
var item = getRequestNum(sys.sys_id.toString());

return {
'number':item
};
}
catch(e){
gs.info('Something wrong with request paramaters' + e);
}

function getRequestNum(sys_id){
try{
var number ='';
var gr = new GlideRecord('sc_req_item');
gr.addQuery('cat_item',sys_id);
gr.orderByDesc('number');
gr.query();
if(gr.next()){

gs.info('Item to return ' + gr.number);

}
else{
gs.info('Item to return does not exists' + gr.number);
}
gs.log('Number is:Sheveta'+number);
return gr.getDisplayValue('number');
}
catch(e){
gs.info('Error in response' + e);
}
}
function processCart(cat, obj){
var a;
try{
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem(cat);
if(obj.item_name == 'Teams Creation')
{
cart.setVariable(item, 'team_name' , obj.team_name);
cart.setVariable(item, 'team_owner' , obj.team_owner);
cart.setVariable(item, 'team_description' , obj.team_description);
cart.setVariable(item, 'team_duration' , obj.team_duration);
cart.setVariable(item, 'access_type' , obj.access_type);
cart.setVariable(item, 'opened_by' , obj.opened_by);
}
var rc = cart.placeOrder();
ak = ('rc' + 'Info of cart');

}
catch(e){
a = ('log cart error '+ e);
}
return a;
}


})(request, response);

 

Thanks

Hi,

So this is scripted rest API

also you are using obj.opened_by while setting the value.

Did you check what value is coming into that?

gs.info('API Opened By is: ' + obj.opened_by);

cart.setVariable(item, 'opened_by' , obj.opened_by); 

What does it mean third party user?

I believe you must have shared a single user's credentials for basic authentication of the API so do you want to populate it with that user

if yes then directly hard code it in the script

Regards

Ankur

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi,

Actually the scenario is this that the user isn't creating the RITM from SNOW portal, the user submits the request from a SharePoint site (I need the user who has logged in this site) and this API is being called to create the RITM.

Thanks