Auto creation of a Request when Eligible for Refresh flagged

Shan14
Tera Expert

Hello Experts,

 

SAM-Calculate Asset Refresh Eligibility which runs weekly (OOB) check if the useful life is exceeded or not. If exceeded, if flags the "Eligible for Refresh " field to yes. 

My question is, is there any way that we can created REQ/ RITM on behalf of the owner of the asset when the "Eligible for Refresh " flagged as true after the job and the RITM runs through it's own flow. 

 

If this is doable, kindly advise me on how we can do this. Thank you.  

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

HI @Shan14 ,
I trust you are doin great.
This can e achived by using the scheduled job along with the cart API

// Query assets where 'Eligible for Refresh' is true
var assetGR = new GlideRecord('alm_asset'); // Assuming 'alm_asset' is the table name
assetGR.addQuery('eligible_for_refresh', true);
assetGR.query();

while (assetGR.next()) {
    // Check if a REQ/RITM already exists for this asset to avoid duplicates
    var ritmGR = new GlideRecord('sc_req_item');
    ritmGR.addQuery('asset', assetGR.sys_id);
    ritmGR.query();
    
    if (!ritmGR.next()) { // If no RITM exists for this asset
        // Create a new REQ/RITM for the asset owner
        var cart = new Cart("newCart", assetGR.owned_by.toString()); // Assuming 'owned_by' is the field storing the owner's sys_id
        var item = cart.addItem('YOUR_CATALOG_ITEM_SYSID'); // Replace with your catalog item sys_id
        
        // Set any necessary variables for the catalog item
        cart.setVariable(item, 'variable_name1', 'value123');
        cart.setVariable(item, 'variable_name2', 'value123');
        
        var req = cart.placeOrder();
        gs.info("Created REQ: " + req.number + " for Asset: " + assetGR.asset_tag);
    }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

4 REPLIES 4

Voona Rohila
Kilo Patron
Kilo Patron

Hi @Shan14 

Sample code to raise the request from script:

var cart = new Cart("newCart", "user_sysid");
var item= cart.addItem('give_item_sysid'); //map item sysid here
cart.setVariable(item,'variable_name1,'value123');
cart.setVariable(item,'variable_name2','value123');
var req_id = cart.placeOrder();
gs.info(req_id.number);

Use this accordingly in your logic. 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Thank you so much.

Amit Gujarathi
Giga Sage
Giga Sage

HI @Shan14 ,
I trust you are doin great.
This can e achived by using the scheduled job along with the cart API

// Query assets where 'Eligible for Refresh' is true
var assetGR = new GlideRecord('alm_asset'); // Assuming 'alm_asset' is the table name
assetGR.addQuery('eligible_for_refresh', true);
assetGR.query();

while (assetGR.next()) {
    // Check if a REQ/RITM already exists for this asset to avoid duplicates
    var ritmGR = new GlideRecord('sc_req_item');
    ritmGR.addQuery('asset', assetGR.sys_id);
    ritmGR.query();
    
    if (!ritmGR.next()) { // If no RITM exists for this asset
        // Create a new REQ/RITM for the asset owner
        var cart = new Cart("newCart", assetGR.owned_by.toString()); // Assuming 'owned_by' is the field storing the owner's sys_id
        var item = cart.addItem('YOUR_CATALOG_ITEM_SYSID'); // Replace with your catalog item sys_id
        
        // Set any necessary variables for the catalog item
        cart.setVariable(item, 'variable_name1', 'value123');
        cart.setVariable(item, 'variable_name2', 'value123');
        
        var req = cart.placeOrder();
        gs.info("Created REQ: " + req.number + " for Asset: " + assetGR.asset_tag);
    }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Thank you So much.

btw, The table is alm_hardware which has the Eligible for Refresh field