Create HR profile or update when user is inserted/Updated

Kachu
Kilo Sage

Hi Community,

I have created Business rule to auto create HR profile when the user record is inserted or updated if there is no HR profile created for the user. below is the business rule .but business rule is not working  Can any one let me know if there are any changes in this code?

Business rule : After create/Update no conditions added  in When to run and Actions. Just Added Advanced script

Thank you.

find_real_file.png

9 REPLIES 9

Dan H
Tera Guru

Hello,

You should try and change:

gr.addQuery('user', user);

to

gr.addQuery('sys_id', user);

 

You also need to put your function and function call inside the 

(function executeRule(current, previous /*null when async*/) 
 {

    //function code
    //call function

 })(current, previous);

 

Also note that its bad practice to use gr as a object name for GlideRecords. A better name would be profileGR in this case. Although it probably wont affect you here, there are some situations in which gr can cause some complicated issues.

Let me know if the above change fixes the issue

If my answer has helped or resolved your query, please mark helpful/solved it based on impact.

 

 

Hi Dan,

Just one correction in the below code mentioned by you

Hello,

You should try and change:

gr.addQuery('user', user);

to

gr.addQuery('sys_id', user);

Highlighted line will not work as user is Gliding  HR Profile table where normal reference to sys_user has the field name "User"

find_real_file.png

 

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

Regards,
Saurabh


Thanks and Regards,

Saurabh Gupta

Saurabh Gupta
Kilo Patron
Kilo Patron

Change your complete business rule code as below

(function executeRule(current, previous /*null when async*/) 
 {
	// Add your code here
checkAndCreatehrProfile(current.getUniqueValue());
	function checkAndCreatehrProfile(user) 
	{
		var gr = new GlideRecord('sn_hr_core_profile');
		gr.addQuery('user', user);
		gr.query();
		if (!gr.next()) 
		{
			var hrProfile = new hr_Profile();
			hrProfile.createProfileFromuser(user);
		}
	}

	})(current, previous);

 

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

Regards,
Saurabh


Thanks and Regards,

Saurabh Gupta

HI can tell me how to test is because when i am creating user directl;y from user table it not creating any HR profile.

find_real_file.png

find_real_file.png