script not executing on manual executing

si21
Tera Guru

 

Hi experts,

We have below scheduled job created to run daily and when we click on execute now manually, none of the expected records getting deleted.

Please guide me what went wrong

function cleanUpContingentHRProfiles() {
  try {
    // Cleanup all HR profiles created for Contingent workers
    var encodedstr1 = "user.u_worker_typeSTARTSWITHcontingent";
    var hrObj = new GlideRecord("sn_hr_core_profile");
    hrObj.addEncodedQuery(encodedstr1);
    hrObj.deleteMultiple();

  } catch (error) {
    // Handle the error if something goes wrong
    gs.error("Error cleaning up HR profiles: " + error.message);
  }
}

 TIA

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@si21 

you forgot to invoke that function if the above script is the actual script

add this line as well to actually query

cleanUpContingentHRProfiles();

function cleanUpContingentHRProfiles() {
    try {
        // Cleanup all HR profiles created for Contingent workers
        var encodedstr1 = "user.u_worker_typeSTARTSWITHcontingent";
        var hrObj = new GlideRecord("sn_hr_core_profile");
        hrObj.addEncodedQuery(encodedstr1);
        hrObj.query();
        hrObj.deleteMultiple();

    } catch (error) {
        // Handle the error if something goes wrong
        gs.error("Error cleaning up HR profiles: " + error.message);
    }
}

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

8 REPLIES 8

Brad Bowman
Kilo Patron
Kilo Patron

You're not actually executing the instantiation of the GlideRecord, so not returning any records.  It's usually better to use deleteRecord instead of Multiple

function cleanUpContingentHRProfiles() {
  try {
    // Cleanup all HR profiles created for Contingent workers
    var encodedstr1 = "user.u_worker_typeSTARTSWITHcontingent";
    var hrObj = new GlideRecord("sn_hr_core_profile");
    hrObj.addEncodedQuery(encodedstr1);
    hrObj.query();
    while (hrObj.next()) {
        hrObj.deleteRecord();
    }
  } catch (error) {
    // Handle the error if something goes wrong
    gs.error("Error cleaning up HR profiles: " + error.message);
  }
}

Ankur Bawiskar
Tera Patron
Tera Patron

@si21 

you forgot to invoke that function if the above script is the actual script

add this line as well to actually query

cleanUpContingentHRProfiles();

function cleanUpContingentHRProfiles() {
    try {
        // Cleanup all HR profiles created for Contingent workers
        var encodedstr1 = "user.u_worker_typeSTARTSWITHcontingent";
        var hrObj = new GlideRecord("sn_hr_core_profile");
        hrObj.addEncodedQuery(encodedstr1);
        hrObj.query();
        hrObj.deleteMultiple();

    } catch (error) {
        // Handle the error if something goes wrong
        gs.error("Error cleaning up HR profiles: " + error.message);
    }
}

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Medi C
Giga Sage

Hi @si21,

Please use:

cleanUpContingentHRProfiles();

function cleanUpContingentHRProfiles() {
    try {
        // Cleanup all HR profiles created for Contingent workers
        var encodedstr1 = "user.u_worker_typeSTARTSWITHcontingent";
        var hrObj = new GlideRecord("sn_hr_core_profile");
        hrObj.addEncodedQuery(encodedstr1);
        hrObj.query();
        hrObj.deleteMultiple();

    } catch (error) {
        // Handle the error if something goes wrong
        gs.error("Error cleaning up HR profiles: " + error.message);
    }
}

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

si21
Tera Guru

HI @Ankur Bawiskar ,  thanks for the quick reply, I tried the code you have provided but still none of records got deleted in dev.

Is it because I have created this job in Global scope and action is on HR table?

I have also created the same script in HR scope and it worked well.

 

Also can we know which HR profiles deleted in logs if we need to check?