How to get data inside object in a array ?

venugopal s
Tera Contributor

Hi Everyone,

I need to get data inside object as an array for certain requirement.

 

 

Accountable OnwerAccountable Remediation ownernew AROmail recipients (AO + new ARO)mail body
AdammattsatyaAdam,satya,venu,gopalmatt,thompson,aaron
Adamthompsonvenu   
Adamaarongopal   
SmithdevharshSmith,harsh,gupta dev,gill
Smithgillgupta   

 

where Key is Accountable onwer.
@Ankur Bawiskar 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@venugopal s 

sorry didn't get your requirement

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar ,
My requirement is when ever user is ARO field is inactive need to place user's manager (new ARO) need to trigger email based accountable owner in same record. for example, Aaron is user he placed in ARO field on 10 records, for those 10 records accountable owner's will be different, now to need to trigger based on accountable owner and recipients will be new ARO user. In email body will be inactive user. 

Amit Gujarathi
Giga Sage
Giga Sage

Hi @venugopal s ,
I trust you are doing great.
Here's an example of how you can achieve this in ServiceNow using a Business Rule:

// Business Rule: ARO Inactive Email Notification
// Table: Your_Table_Name

(function executeRule(current, previous) {
  // Check if the ARO field has been marked as inactive
  if (current.aro.active === false) {
    // Get the accountable owner and new ARO values
    var accountableOwner = current.accountable_owner;
    var newARO = current.new_aro;

    // Get the email recipients
    var recipients = accountableOwner + "," + newARO;

    // Get the inactive user's name
    var inactiveUser = current.aro.name;

    // Compose the email body
    var mailBody = "Inactive user: " + inactiveUser;

    // Send the email
    gs.eventQueue("email.send", current, recipients, "ARO Inactive Notification", mailBody);
  }
})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hi @Amit Gujarathi ,
created a scheduled job as per requirements, but problem is not able to group accountable owners recipients as per table mentioned above.
Scheduled job:


updateAccountableRemediationOwner();

function updateAccountableRemediationOwner() {
    var accountableOwnerRecords = [];
    var managerData;
    var aroUserName = '';
    var grASR = new GlideRecord("cmdb_ci_service_discovered");
    grASR.addQuery("install_status!=7^u_accountable_remediation_ownerISNOTEMPTY^u_accountable_remediation_owner.active = false");
    //grASR.setLimit(10);
    grASR.query();
    while (grASR.next()) {
        gs.log('venu while starts');
        aroUserName = grASR.u_accountable_remediation_owner.name.toString();
        if (!gs.nil(grASR.u_aro_backup) && grASR.u_aro_backup.toString() == grASR.getValue('u_accountable_remediation_owner')) {
     managerData = gtInactiveManager(grASR.u_aro_backup);
            grASR.u_accountable_remediation_owner = managerData.manager_sys_id;
            grASR.u_aro_backup = '';
        } else if (!gs.nil(grASR.u_aro_backup) && grASR.u_aro_backup.toString() != grASR.getValue('u_accountable_remediation_owner') && (grASR.getElement('u_aro_backup.active').toString() == 'true')) {
            grASR.u_accountable_remediation_owner = grASR.u_aro_backup;
            grASR.u_aro_backup = '';
        } else if (gs.nil(grASR.u_aro_backup) || (grASR.getElement('u_aro_backup.active').toString() == 'false')) {
        managerData = gtInactiveManager(grASR.u_accountable_remediation_owner);
            grASR.u_accountable_remediation_owner = managerData.manager_sys_id;
            grASR.u_aro_backup = '';
        }
        gs.log('venu test');
        grASR.update();
        accountableOwnerRecords.push({
            accountableOwner: grASR.getValue('assigned_to'),
            newARO: grASR.getValue('u_accountable_remediation_owner'),
            aroUserName: aroUserName,
            applicationServiceNumber: grASR.getValue('number')
        }); 
    }
    var obj = {};
    for (var i = 0; i < accountableOwnerRecords.length; i++) {
        if(!Array.isArray(obj[accountableOwnerRecords[i].aroUserName + "," + accountableOwnerRecords[i].accountableOwner])){
            //obj[accountableOwnerRecords[i].inactiveARO]=[];
            //obj[accountableOwnerRecords[i].inactiveARO + "," + accountableOwnerRecords[i].inactiveARO].push(accountableOwnerRecords[i].inactiveARO);
            obj[accountableOwnerRecords[i].aroUserName + "," + accountableOwnerRecords[i].accountableOwner] = [];
            obj[accountableOwnerRecords[i].aroUserName + "," + accountableOwnerRecords[i].accountableOwner].push(accountableOwnerRecords[i].accountableOwner);
            obj[accountableOwnerRecords[i].aroUserName + "," + accountableOwnerRecords[i].accountableOwner].push(accountableOwnerRecords[i].newARO);
            obj['applicationServiceNumber'] = [];
            obj['applicationServiceNumber'].push(accountableOwnerRecords[i].applicationServiceNumber);
        }
        else {
            obj[accountableOwnerRecords[i].aroUserName + "," + accountableOwnerRecords[i].accountableOwner].push(accountableOwnerRecords[i].newARO);
            obj['applicationServiceNumber'].push(accountableOwnerRecords[i].applicationServiceNumber);
        }
    }
    for (var k in obj) {
        if(k !== 'applicationServiceNumber'){
            var new_arr = new ArrayUtil().unique(obj[k]);
            var inactiveARO = k.split(",");
            inactiveARO = inactiveARO[0];
        }else
            var numbers = obj[k];
        if(numbers.length){
            gs.eventQueue('fa.accountable.remediation.owner', grASR, new_arr, JSON.stringify({
                "numbers": numbers,
                "inactiveARO": inactiveARO}));
        }
    }
}
// FA_Tree().getParentNodes();
            //managerData = FA_Tree().getRootNode(grASR.u_accountable_remediation_owner);
                //managerData = FA_Tree().getRootNode(grASR.u_aro_backup);

function gtInactiveManager(user) {
    var manager = '';
    manager = user.manager;
    if (!manager) return;
    if (manager.active) {
        manager = manager.toString();
        var email = user.manager.email.toString();
        return {
            "manager_sys_id": manager,
            "manager_email": email
        };
    } else {
        return gtInactiveManager(manager);
    }
}
and email body will be,
venugopals_0-1686067490058.png

 

Thanks