Brad Bowman
Mega 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

@Community Alums 

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.

View solution in original post

Medi C
Giga Sage

Hi @Community Alums,

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);
    }
}

 

Not applicable

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?

@Community Alums 

did you check if your query is giving correct count?

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();

		gs.info(hrObj.getRowCount());

        hrObj.deleteMultiple();



    } catch (error) {

        // Handle the error if something goes wrong

        gs.error("Error cleaning up HR profiles: " + error.message);

    }

}