How to delete/deactivate record with transform map?

Alberto Comerro
Tera Contributor

I have to import a file with association to user and one group.

When a user is no loger in the the file it must be removed from the servicenow group.

How can I make this with a transform map?

6 REPLIES 6

RC8
Kilo Guru

Hi @Alberto Comerro 

you cannot delete any record by using transform map you have to remove user manually from that group

 

Thanks,

Rakesh

Kartik Sethi
Tera Guru
Tera Guru

Hi @Alberto Comerro,

 

OOTB there is no such option with Transform Maps but you can have a way to implement deletion/deactivation. What you can do is:

  1. Create a onStart Transform Script and initialize an array type variable.
    function onStart(<parameters>) {
    	//Use onStart constructor as provided OOTB
    	var userArr = []; //Array will be populated on onAfter Transform Scripts
    }​

     

  2. Then create onAfter Transform Script and in that transform Script check the status of import if it is successful then push the target record sys_id in the array "userArr"
  3. Finally write a onComplete Transform Script and do a GlideRecord query on User table:
    function onComplete( < parameters > ) {
        //Use the OOTB constructor
        var deactivateUserGr = new GlideRecord('sys_user');
        deactivateUserGr.addEncodedQuery('sys_idNOT IN' + userArr.join(','));
        deactivateUserGr.query();
        while (deactivateUserGr.next()) {
            deactivateUserGr.active = false;
            deactivateUserGr.update();
            //OR
            //deactivateUserGr.deleteRecord();
        }
    }​

     

Please try the above steps and see if it works for you.

 

If my answer is helpful then please do mark my reply as correct/helpful.

 

Regards,

Kartik

Hi Kartik,

 

I would like to try to use your solution. However, I am very script-saavy yet. Can you provide an example script or screenshot for step #2? Please and thank you in advance!

 

-Cyn

Community Alums
Not applicable

Hi Alberto,

My suggestion would be,

1. Create an active field on the target table

2. Create a onBefore transform script to set the active attribute to false on all records

3. During transformation,when record is being updated, set the active attribute to true

4. After completing the transform, you can either write an onComplete transform script or a scheduled job to delete the inactive records that have not been touched during the transform.

 

 

Please mark my answer as Correct & Helpful, if applicable.

Thanks

Sandeep