This widget could not be displayed.
This widget could not be displayed.

CartJS API

Adja Bineta
Tera Contributor

Hello
Has anyone used the CartJS API?

I'm currently working on a requirement to import RITM records using the CartJS API.

I've already created a transform map and started scripting, but I need to populate specific fields on the RITM, such as State and Requested For.

Has anyone done something similar or can share tips on how to set those values using CartJS?

Below is the script I'm currently working with:

 

var cart = new sn_sc.CartJS();

var newItem = {
"sysparm_id": "REPLACE_WITH_ITEM_SYS_ID",
"sysparm_quantity": "1",
"variables": {

'variable1': 'value1',
'variable2': 'value2',


}
};

gs.log("New Item: " + JSON.stringify(newItem));

var cartDetails = cart.addToCart(newItem);
gs.log("Cart details: " + JSON.stringify(cartDetails));

// Checkout
var checkoutInfo = cart.checkoutCart();
gs.log("Checkout info: " + JSON.stringify(checkoutInfo));

1 REPLY 1

Deepak Shaerma
Kilo Sage

Hi @Adja Bineta 

We have used it in one of our requirement, i am sharing you the function that we have used in our onBefore transform script: Check it once if it will helps you,

 function raiseRequestUsingCartJS(userGR, source) {
        var requestJson = {
            "sysparm_id": "fa793f1n5k7654kbk47b4k91bfdb", //Catalog item
            "sysparm_quantity": "1",
            "variables": {
                'u_requested_for': userGR.sys_id.toString(),
                'u_employee_id': userGR.employee_number.toString(),
                'u_email': userGR.email.toString(),
                'u_requirement_business_justification': source.u_requirement.toString(),
                'u_valid_till': source.u_valid_till.toString()
            }
        };

        requestJson["variables"][pan] = true;

        var cart = new sn_sc.CartJS(gs.generateGUID());

        var cartDetails = cart.addToCart(requestJson);
        var rc = cart.checkoutCart();
        if (JSUtil.notNil(rc.request_id)) {
            // Updating the RITM value in the source record
            var grRITM = new GlideRecord('sc_req_item');
            if (grRITM.get('request', rc.request_id)) {
                source.u_ritm_sysid = grRITM.sys_id.toString();
            }
        }
    }

 

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma