Scripted Rest API: Create RITM and SCTASK

imed1
Tera Contributor

Hello,

 

I'm facing an issue when i try to create my RITM using the Scripted Rest API i have every time the opened_by and resquestd_for fields with geust value ?

 

Note:

- Basic Authentification used (user have all role, admin, security admin, rest.....)

- No ACL configured. 

- The sys_properties: glide.basicauth.required.api is true

Thanks for your help!

1 ACCEPTED SOLUTION

try below

createRequest: function() {

    var cart = new Cart();
    var item = cart.addItem(this.cat_item_id);
    var rc = cart.placeOrder();

	var req = new GlideRecord('sc_request');
	req.get(rc.sys_id);
	req.opened_by = this.opened_by // make sure to pass sys_id of user
	req.requested_for = this.requestor; // make sure to pass sys_id of user
	req.update();
	
    var req_numb = rc.number;
    var gr = new GlideRecord("sc_req_item");
    gr.addQuery("request", rc.sys_id);
    gr.query();
    if (gr.next()) {      
        gr.company = this.company;
        gr.short_description = this.short_description;
        gr.description = this.description;
        gr.cat_item = this.cat_item_id;
        gr.state = this.state;
        gr.active = true;
        gr.opened_by = gr.request.opened_by;
        gr.correlation_id = this.cloud_id;
        gr.state = 0;
		
        try {
            gr.update();
        } catch (exception) {
            this.addError('UNDEFINED', exception.toString());
        }
        this.respObj.status = 'ok';
        this.respObj.number = gr.number;
        this.respObj.sysid = gr.sys_id;
    } else {
        this.addError('UNDEFINED', 'The system encountered an error.');
    }
}

View solution in original post

17 REPLIES 17

Mike Patel
Tera Sage

Can you share the code? on how you are creating it.

imed1
Tera Contributor

Hello;

this is the function used to create my request:

All this variables return value before this function, but when i put my gs.log here i have undifiened example for the "this.requestor"

gs.log("sys_id for this.requestor "+this.requestor);//i have a good value in the log

createRequest: function() {

var cart = new Cart();
var item = cart.addItem(this.cat_item_id);
var rc = cart.placeOrder();

var req_numb = rc.number;
var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", rc.sys_id);
gr.query();
if (gr.next()) {
gs.log("Line 147 sys_id for this.requestor "+this.requestor);//i have undifined in the log
gr.request.requested_for = this.requestor ;//this.requested_for//// i have try also  with an sys_id but is already is guest and in the log i have undifiened
gr.company = this.company;
gr.short_description = this.short_description;
gr.description = this.description;
gr.cat_item = this.cat_item_id;
gr.state = this.state;
gr.active = true;
gr.opened_by = gs.getUserID();// i have try with the sys_id but is already is guest and in the log i have undifiened
gr.correlation_id = this.cloud_id;
gr.state = 0;

try {
gr.update();
} catch (exception) {
this.addError('UNDEFINED', exception.toString());
}
this.respObj.status = 'ok';
this.respObj.number = gr.number;
this.respObj.sysid = gr.sys_id;
} else {
this.addError('UNDEFINED', 'The system encountered an error.');
}
},

try below

createRequest: function() {

    var cart = new Cart();
    var item = cart.addItem(this.cat_item_id);
    var rc = cart.placeOrder();

	var req = new GlideRecord('sc_request');
	req.get(rc.sys_id);
	req.opened_by = this.opened_by // make sure to pass sys_id of user
	req.requested_for = this.requestor; // make sure to pass sys_id of user
	req.update();
	
    var req_numb = rc.number;
    var gr = new GlideRecord("sc_req_item");
    gr.addQuery("request", rc.sys_id);
    gr.query();
    if (gr.next()) {      
        gr.company = this.company;
        gr.short_description = this.short_description;
        gr.description = this.description;
        gr.cat_item = this.cat_item_id;
        gr.state = this.state;
        gr.active = true;
        gr.opened_by = gr.request.opened_by;
        gr.correlation_id = this.cloud_id;
        gr.state = 0;
		
        try {
            gr.update();
        } catch (exception) {
            this.addError('UNDEFINED', exception.toString());
        }
        this.respObj.status = 'ok';
        this.respObj.number = gr.number;
        this.respObj.sysid = gr.sys_id;
    } else {
        this.addError('UNDEFINED', 'The system encountered an error.');
    }
}

imed1
Tera Contributor

Hello,

 

Good, it' swork now for the opened_by and requestor_for but i have already the created_by is guest !

I also added it here: 

req.sys_created_by = this.requestor.getDisplayValue(); // I have a sys_id not display value of user

Have you any idea if it's related to servicenow security policy or other reason?

 

Thanks in advance