We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Need to create the scripted rest api for requested item with attachment

keshav77
Tera Contributor

Hello everyone, 

 

Could you please help me to create the Scripted rest api  where I want to create one requested item of particular catalog with  add an attachment kindly share me the steps to do this?

Regards, 

Keshav

2 REPLIES 2

JenniferRah
Mega Sage

Your code will look something like this:

    var catItem = '6f4c677387f94ed0a4914229dabb3526'; //sys_id of your catalog item
    gs.include('validators');

        var cart = new sn_sc.CartJS(gs.generateGUID()); //Create cart
        var item = {
            'sysparm_id': catItem,
            'sysparm_quantity': '1',
            'variables': {
                'yourvariablename': 'yourvariablevalue'
            }
        };
        cart.addToCart(item);
        var checkoutInfo = cart.checkoutCart();

        var grRITM = new GlideRecord('sc_req_item');
        grRITM.addQuery('request', checkoutInfo.request_id);
        grRITM.query();
        if (grRITM.next()) {
            GlideSysAttachment.copy('tablewhereattachmentis', 'sys_idofrecordwithattachment', 'sc_req_item', grRITM.sys_id); 
            grRITM.update();
        }

 

The line that says GlideSysAttachment.copy is where the attachment gets added. You would need to fill that in with the location of your attachment.

 

I hope that helps!

I just realized you said you wanted a scripted REST API. What I gave you is what we use in an inbound action. If there's a way to receive an email rather than an API call, you could use something like what I have above. 

 

If it must be a REST call, you will have to create one call to submit the catalog item and send them back a RITM number and/or sys_id. Then they will probably have to do a second call to attach the attachment(s).