Script IF NULL Skip Record

Evan Duran
Kilo Guru

I have a script where I return USER and their VP and insert information into table. I'm having difficulty where script is crashing in both background script and fix scripted likely because of one of three cases.
1. manager doesn't exist on user profile
2. hr profile doesn't exist on user or manager profile
3. cannot find job group of 11

What can I add to my script to just skip record based if this happens?

var gr = new GlideRecord('sn_hr_core_task');
gr.addEncodedQuery('state=18^template=e16ff3d21b244250c1db337cdc4bcb29');
gr.query();  
while(gr.next()) {
           var user = gr.assigned_to;      
           var validJobGroup = false;
           var manager = user.manager; 
           while(manager.u_hr_profile.position.u_job_group != 11) {
            manager = manager.manager;
            if(manager.u_hr_profile.position.u_job_group == 11){
				var pr = new GlideRecord('u_user_vp_info');
				pr.initialize();
				pr.u_user = user.getDisplayValue();
				pr.u_vp = manager.getDisplayValue();
				pr.insert();
            }
    }
}
2 REPLIES 2

DrewW
Mega Sage
Mega Sage

You can check to see if a field is null by simply doing this

if(gr.assigned_to) {
  //Do something if not NULL
} else {
   //Do something if it is NULL
}

//So you can also do
if(gr.assigned_to && gr.assigned_to.manager) {
  //Do work if both have a value.
}

shouldn't something like this work?

 while(manager != null && manager.u_hr_profile != null && manager.u_hr_profile.position.u_job_group != 11) {
            manager = manager.manager;