Update ID field in department from User-Import Transform maps

Nisha30
Kilo Sage

HI,

We are loading User data via transform maps to sys_user table. However the department field also is mapped to department field in user table. (source_department)

SInce the choice is set Create=TRUE for Department, it is creating Departments in cmn_department table.

However if that happens we want to update the ID field in cmn_department table with some static value to identify what came via import . (since we also manually upload departments )

Thanks

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Nisha30 

in that case don't use field map for department. please use onBefore transform script

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

    // Add your code here
    var sourceDepartment = source.u_department_name;
    var gr = new GlideRecord("cmn_department");
    gr.addQuery("name", sourceDepartment); // query with name if department name is coming in staging table
    gr.query();
    if (gr.next()) {
        target.department = gr.getUniqueValue();
    } else {
		// not found then create
        gr.initialize();
        gr.name = sourceDepartment;
		// populate other fields if you wish
        gr.id = 'Some Unique ID';
        gr.insert();
        target.department = gr.getUniqueValue();
    }

})(source, map, log, target);

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

19 REPLIES 19

Hi @Ankur Bawiskar  the same script what you suggested as onBefore

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

    // Add your code here
    var sourceDepartment = source.u_department;
    log.info('<<<target dept onbefore 26'+ sourceDepartment );
    var gr = new GlideRecord("cmn_department");
    gr.addQuery("name", sourceDepartment); // query with name if department name is coming in staging table
    gr.query();
    if (gr.next()) {
        target.department = gr.getUniqueValue();
    } else {
        // not found then create
        gr.initialize();
        gr.name = sourceDepartment;
        // populate other fields if you wish
        gr.id = 'beforescript';
        gr.insert();
        target.department = gr.getUniqueValue();
    }

})(source, map, log, target);

 

@Nisha30 

so gr.id line is not working?

Did you try creating new department from background script and populate some random value in that id field?
I was able to insert with some random value, so it should work fine for you.

AnkurBawiskar_0-1756142233696.png

 

I believe I answered your question and you can take it further from here based on your experience and developer skills.

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

HI @Ankur Bawiskar 

yes the ID field is not populating in cmn_department rest all okay.

The IF else condition is not executing . Tried putting logs but no logs in either IF loop /ELSE loop

Thanks

@Nisha30 

did you try running background script and see?

I was able to set id field via script.

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

Shivambitanwar1
Tera Contributor

Hi Nisha,

 

You need to create a onChoiceCreate Transform script for this :

https://www.servicenow.com/docs/bundle/zurich-integrate-applications/page/script/server-scripting/re...


then you can use something like if( name == 'your deparment field name ') then target.department.id = 'yourvalue '. this is a pseudo code which you need to adapt. I have added alink to docs that is more detailed on how onchoice create works and objects available for use.

//Shivambi