Script to Create REQs RITMs not working

rishabh31
Mega Sage

Dear Team, I have a requirement where we implemented Workspace One UEM Integration (SG-Workspace ONE UEM Devices and Apps) through service graph connectors, and it fetches data through Service Graph connectors and update the table cmdb_ci_handheld_computing 

 

The requirement is to restrict the Insert of any new record and allow only update of existing record by matching Phone Number and IMEI no. of the records fetching through WorkSpace with existing records on cmdb_ci_handheld_computing  table. If Phone Number and IMEI is not matched/found with/in existing records then create  REQs/RITMs for each of such records or also if either of Phone No. or IMEI is found empty in existing records then on that case as well create  REQs/RITMs for each of such records.

 

So I modified the automatically created Robust Transform Engine Entity Mapping whose Source entity is Temp and Target entity is Handheld Computing Device 2, here I checked the 'Is Conditional' and wrote script per logic.

 

 

(function() {
    if (((input.temp.u_record_type === 'device') && (input.temp.device_type_lookup === 'mobile'))) {
        // gs.info("Enter the RTM Script:2" + input.temp.u_imei_1 + "Phone number: " + input.temp.u_phone_number);
        var ci = new GlideRecord("cmdb_ci_handheld_computing");
		ci.addEncodedQuery("phone_numberISNOTEMPTY^imeiISNOTEMPTY");
        ci.addQuery("imei", input.temp.u_imei_1);
        ci.addQuery("phone_number", input.temp.u_phone_number);
        ci.query();
        while (!ci.next()) {
            gs.info("AutomateLogs : Enter the RTM Script:while ");
            var cartId = GlideGuid.generate(null);
			//var cartId = gs.generateGUID();
            var cart = new Cart(cartId);
            var item = cart.addItem("644790991bba8e50dec27599cc4bcb5b", 1); // cat item sys_id
            cart.setVariable(item, "device_type", "iPad");
            cart.setVariable(item, "accesories", "Test");
			cart.setVariable(item, "requested_for", "XYZ");
            var rc = cart.placeOrder();
            gs.info("Print the request number: no match " + rc.number.toString());
            return false;
        }
        return true;

 

rishabh31_0-1718368506253.png

The above script is working fine for update of matching records and restricting the new record insertion in target table when below scheduled job executed

rishabh31_1-1718368889590.png

But unfortunately it is not creating REQs/RITMs for the non matched/not found records or Empty Phone No. or IMEI in target table (cmdb_ci_handheld_computing_device).

 

To verify the above script I wrote below background script with logs:

 

var imei = "56973789400358";
var phone = "11111111111";
if (((imei !== '') && (phone !== ''))) {
    // gs.info("Enter the RTM Script:2" + input.temp.u_imei_1 + "Phone number: " + input.temp.u_phone_number);
    var ci = new GlideRecord("cmdb_ci_handheld_computing");
    ci.addQuery("imei", imei);
    ci.addQuery("phone_number", phone);
    ci.query();
    while (!ci.next()) {
        var cartId = GlideGuid.generate(null);
       var cart = new Cart(cartId);
            var item = cart.addItem("dd36c78a1b87d51063b0a60bbc4bcb25", 1); // cat item sys_id
            cart.setVariable(item, "device_type", "iPad");
            cart.setVariable(item, "accesories", "Test");
            var rc = cart.placeOrder();

        gs.info(" background Print the request number: no match " + rc.number);
    }
    gs.info("Update the CI");
}

 

Since the record matched/found with imei = "56973789400358"; phone = "11111111111" in target table; so it passes and print 'Update the CI', and not creating any REQs/RITMs and when I change either of IMEI or Phone No. then it is creating multiple REQs/RITMs for all Unmatched/Not Found or Empty records and print logs as shown in below screenshot 

rishabh31_0-1718370153244.png

 

Thus it means in background script it is working fine but when I executed in Script then it throws 'Undefined' (See below screenshot) for Unmatched/Empty records and not creating any records but it updates the matching records 

 

rishabh31_2-1718369991145.png

 

Can anyone please help me to resolve this. I will be grateful.

 

Thanks

 

6 REPLIES 6

I cannot write the entire script, because I don't have enough details/background to do so.

But I can write a part of it to help you get started, and maybe you can solve the rest on your own.

 

See below, hope this helps!

// start of by identifing if the phone nr and imei is empty, this should create a RITM as per requirement
if(input.temp.u_phone_number.toString() == '' || input.temp.u_imei_1.toString() == ''){
          var cart = new Cart();
          var item = cart.addItem("644790991bba8e50dec27599cc4bcb5b", 1); // cat item sys_id
          cart.setVariable(item, "device_type", "iPad");
          cart.setVariable(item, "accesories", "Test");
	  cart.setVariable(item, "requested_for", "XYZ");
          var rc = cart.placeOrder();
          gs.info("Print the request number: no match " + rc.getValue('number'));
}
else {
         var ci = new GlideRecord("cmdb_ci_handheld_computing");
         ci.addQuery("imei", input.temp.u_imei_1.toString());
         ci.addQuery("phone_number", input.temp.u_phone_number.toString());
         ci.setLimit(1);
         ci.query();

         // if no matching phone nr and imei is found, create a request
         if (!.ci.hasNext()) {
              var cart = new Cart();
              var item = cart.addItem("644790991bba8e50dec27599cc4bcb5b", 1); // cat item sys_id
              cart.setVariable(item, "device_type", "iPad");
              cart.setVariable(item, "accesories", "Test");
	      cart.setVariable(item, "requested_for", "XYZ");
              var rc = cart.placeOrder();
              gs.info("Print the request number: no match " + rc.getValue('number'));
}

 

Community Alums
Not applicable

Hi @rishabh31 ,

It looks like there is an issue with the encoded query and also the while (!ci.next()) doesnt seems to be correct.
Please find the below updated scripts and validate-

Transform script-

(function() {
    if (((input.temp.u_record_type === 'device') && (input.temp.device_type_lookup === 'mobile'))) {
        // Initialize GlideRecord
        var ci = new GlideRecord("cmdb_ci_handheld_computing");
        
        // Check if either IMEI or Phone Number is empty and create REQs/RITMs
        if (!input.temp.u_imei_1 || !input.temp.u_phone_number) {
            createRequest();
            return false; // stop further processing
        }

        // Add queries to find matching records
        ci.addEncodedQuery("phone_numberISNOTEMPTY^imeiISNOTEMPTY");
        ci.addQuery("imei", input.temp.u_imei_1);
        ci.addQuery("phone_number", input.temp.u_phone_number);
        ci.query();

        if (!ci.hasNext()) {
            // No matching record found, create REQs/RITMs
            createRequest();
            return false; // stop further processing
        }
    }
    return true;

    // Function to create REQs/RITMs
    function createRequest() {
        gs.info("AutomateLogs : Enter the RTM Script: Creating REQ/RITM");
        
        // Initialize Cart API
        var cart = new Cart();
        var item = cart.addItem("644790991bba8e50dec27599cc4bcb5b", 1); // cat item sys_id
        
        // Set variables for the catalog item
        cart.setVariable(item, "device_type", "iPad");
        cart.setVariable(item, "accesories", "Test");
        cart.setVariable(item, "requested_for", "XYZ");
        
        // Place order
        var rc = cart.placeOrder();
        gs.info("Print the request number: no match " + rc.number.toString());
    }
})();

 

Updated background script-

var imei = "56973789400358";
var phone = "11111111111";
var ci = new GlideRecord("cmdb_ci_handheld_computing");

if (!imei || !phone) {
    createRequest();
} else {
    ci.addQuery("imei", imei);
    ci.addQuery("phone_number", phone);
    ci.query();

    if (!ci.hasNext()) {
        createRequest();
    } else {
        gs.info("Update the CI");
    }
}

function createRequest() {
    gs.info("AutomateLogs : Enter the RTM Script: Creating REQ/RITM");

    // Initialize Cart API
    var cart = new Cart();
    var item = cart.addItem("644790991bba8e50dec27599cc4bcb5b", 1); // cat item sys_id

    // Set variables for the catalog item
    cart.setVariable(item, "device_type", "iPad");
    cart.setVariable(item, "accesories", "Test");
    cart.setVariable(item, "requested_for", "XYZ");

    // Place order
    var rc = cart.placeOrder();
    gs.info("Print the request number: no match " + rc.number.toString());
}

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar