Catalog intergration via REST API (Requested_for) not populate

attanhes
Tera Guru

I am testing the catalog REST API integration and struggling with setting up a reference field (Requested_for) on the catalog item.  When request created through the integration requested_for is not getting populated. Other variable fetch into the request as expected.

 

{ 
'sysparm_id': 'ca9a27141bbcc2102a2eedb7b04bcb0f',
'sysparm_quantity': '1',
'variables': {
'v_person_leaving_fn' : 'Test_11',
'v_person_leaving_sn' : 'Test_456',
'v_line_manager' : 'Justin Speed',
'v_offboarding_employment_type' : 'operations',
'requested_for' : 'Damien Reed',
'v_date_leaving' : '25/03/2024'
}
}

 

 

attanhes_2-1709610317052.png

Please tell me how to fix this issue.

 

 

1 ACCEPTED SOLUTION

attanhes
Tera Guru

Thanks everyone. I have created Scripted Rest API for my requirements, and it works perfectly. Now I can fully control all the fields through script.

I am adding my code here in case if anyone want to take advantage.

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var reqbody = request.body.dataString;

    try {
        var parser = new global.JSON();
        var parsedData = parser.decode(reqbody);

        var FirstName = parsedData.v_person_leaving_fn;
        var linemanager = parsedData.v_line_manager;
        var SurName = parsedData.v_person_leaving_sn;
		var Dateleaving = parsedData.v_date_leaving;
		var EmpType = parsedData.v_offboarding_employment_type;

        
        // Log the parsed data
        gs.log("Parsed Data: " + JSON.stringify(parsedData));
		
		var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId);

        // Query the sys_user table to get the requested for user details
        var grUser = new GlideRecord('sys_user');
        //grUser.addQuery('user_name', linemanager); // Assuming linemanager is the username
		grUser.addQuery('name', linemanager); // Assuming linemanager is the username
        grUser.query();
        if (grUser.next()) {
            var linemanagerSysId = grUser.sys_id.toString(); // Get the sys_id of the user
        }

        var item = cart.addItem('ca9a27141bbcc2102a2eedb7b04bcb0f', 1);

        // Set the variables on the request item form
		cart.setVariable(item, "v_person_leaving_fn", FirstName);
		cart.setVariable(item, "v_person_leaving_sn", SurName);
        cart.setVariable(item, "v_line_manager", linemanagerSysId); // Set the sys_id of the user
        cart.setVariable(item, "v_date_leaving", Dateleaving);
		cart.setVariable(item, "v_offboarding_employment_type", EmpType);
       

        var rc = cart.placeOrder();
        var reqNumber = rc.number;

        // Update the "Requested For" field on the REQ record
        var reqRecord = new GlideRecord('sc_request');
        if (reqRecord.get('number', reqNumber)) {
            reqRecord.setValue('requested_for', linemanagerSysId); // Set the sys_id of the user
            reqRecord.update();
        }

	// Log the request number
        gs.log("Request Number: " + reqNumber);

        // Fetch the RITM number for response
        var ritm = new GlideRecord('sc_req_item');
        ritm.get('request.number', reqNumber);
        var ritmNumber = ritm.number;

        gs.info("RITM Number: " + ritmNumber);
        //gs.log("Test"); // Add any additional logging here if needed

        var res = {};
        res["status"] = "Success";
        res["requestNumber"] = reqNumber;
        res["requestItemNumber"] = ritmNumber;
        response.setBody(res);
    } catch (ex) {
        // Log any errors
        gs.error("Error: " + ex.message);

        var res = {};
        res["status"] = "Error";
        res["message"] = ex.message;
        response.setBody(JSON.stringify(res));
        response.setStatus(500);
    }

})(request, response);
	

 

 Jason data format:

 

{
//"sysparm_id": "ca9a27141bbcc2102a2eedb7b04bcb0f",
"sysparm_quantity": "1",
  "v_line_manager": "Damien Reed",
  "v_person_leaving_fn": "Andy",
  "v_person_leaving_sn": "Rock",
  "v_offboarding_employment_type" : "operations",
  "v_date_leaving": "2024-03-06"
}

 

View solution in original post

8 REPLIES 8

Vikram11
ServiceNow Employee
ServiceNow Employee

@attanhes you might wanna pass the sys_id of the user record which should solve the issue

Sumanth16
Kilo Patron

Hi @attanhes ,

 

The value in a reference field/variable is actually the sys_id of the record being referenced, so I would try passing that instead of the display name of the group

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

attanhes
Tera Guru

Yes, this works perfectly when I pass the sys_id. But this requested_for is dynamic and it’s different case by case. The other system sending data in above json format. 

Hi @attanhes ,

 

Add this two lines:

var grUser = new GlideRecord('sys_user');
  grUser.get('user_name',requestedFor);

 

Please refer to below thread:

https://www.servicenow.com/community/developer-forum/how-to-populate-the-requested-for-display-value...

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda