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

@si21 

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

    }

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

@si21 

you are trying to delete HR profile but from which scope?

Global scope?

if yes then you should get cross scope issue for delete

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

If you want to log which records are deleted, then add something like:

gs.info('cleanUpContingentHRProfiles script: deleting profile for user: ' + hrObj.user);

In the 'While()' loop before the delete() in Brad's script.  the you can use the Script Log Statements module and add a filter condition: "Message", "start withs", "cleanUpContigentHRProfiles: ". 

Sandeep Rajput
Tera Patron
Tera Patron

@si21 This could be related to cross application scope access? Either create the schedule job in the HR scope or check if there any Application restricted caller access records are in (Requested/Invalidated/No allowed) state and set them allowed to let the script execute.

 

Also, instead of doing this deletion via a script, you can choose to use a Delete job https://www.servicenow.com/docs/bundle/yokohama-platform-administration/page/administer/managing-dat... to delete such records.