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

Slava Savitsky
Giga Sage

In your two scripts, you are using different sys_ids in the cart.addItem() function call. Can that be the cause of the problem?

Hi @Slava Savitsky Sir, no that not causes the issue.

OlaN
Giga Sage
Giga Sage

Hi,

 

Some pointers,

1. You should not need to use the GlideGuid method, this is to generate a sysid for a record, which is automatically done by the Cart method.

2. In your transform script you're not checking if the imei or phone number contains any values. That should be done to make sure the query returns expected results.

Your query contains an encoded query states that both the IMEI and Phone numer should not be empty, and after that you add a query with input on phone number and imei, which could be empty, so then no record would be found, because the querys are in conflict.

3. If the scripting part of the cart creation and setting variables are troublesome, you could instead swap out that part and call a subflow that does the same thing. (if you are interested in this, ask, and I'll provide a deeper explanation and screen shots on this).

4. If you simply need to know if there is a record or not in a query, don't use the while-loop, instead use an if-statement, and use the method .hasNext() (and combine it with .setLimit(1) for optimal performance)

 

@OlaN sir thank you for the valuable insights, could you pls help to modify the script.