Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Import the user data via transform map,Here i need to assign defaut password and provide roles

KondojuK
Kilo Contributor

I want to Import the user data via transform map,Here i need to assign defaut password and provide roles to users

1 REPLY 1

Deepak Shaerma
Mega Sage

hi @KondojuK 

use on before transform script to assign user password :

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

    if (action == 'insert') {
        target.user_password = "YourDefaultPassword123!"; 
        target.password_needs_reset = true; 
    }

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


for providing roles, use onafter transform script:

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

    var roleToAssign = "itil"; 
    var roleGR = new GlideRecord('sys_user_role');
    if (roleGR.get('name', roleToAssign)) {
        
        var userRoleGR = new GlideRecord('sys_user_has_role');
        userRoleGR.addQuery('user', target.sys_id);
        userRoleGR.addQuery('role', roleGR.sys_id);
        userRoleGR.query();
        if (!userRoleGR.hasNext()) {
            userRoleGR.initialize();
            userRoleGR.user = target.sys_id;
            userRoleGR.role = roleGR.sys_id;
            
            userRoleGR.insert();
        }
    } else {
        log.error("Transform Map Error: Could not find role named " + roleToAssign);
    }

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


Happy to help! ‌‌
To help others in the community find this solution, kindly mark this response as the Correct Answer✔️ ‌‌ and Helpful‌‌ 👍.
Warm Regards,
Deepak Sharma
Community Rising Star 2025