Record Producer not updating an Existing Record

CharlesR1
Kilo Guru

Hello,

I have read a few discussions about this on the community, but none of the scripts seem to work for us. I would really appreciate some help with this if possible.

We have a new staff onboarding/offboarding application that creates a record for the entire employee life cycle of a new member of staff. The onboarding part is generated from a Record Producer that opens a new record on the 'u_onboarding' table and progresses it eventually to a state of 'Current Staff' (state value '6'). As part of the onboarding process, the new employee has a ServiceNow account created automatically for them, and their username from the sys_user table then appears in a reference field on the record (u_user_id). There is also a related list on the sys_user table showing the parent onboarding record.

At this point the record remains unchanged until the staff member is offboarded. The problem we have is with the offboarding record producer. This is a separate form that includes just a reference field (sys_user). There is a reference qualifier to limit this to the current line manager's team members only. On submit, the record producer should find the onboarding form with the selected username corresponding to the u_user_id field, and simply update the state field to '7' (Offboarding). This will then kick off the rest of the process.

However, this does not work at the moment. All it does is open a new blank u_onboarding record, and the target record is not updated. Obviously there is an error in our script, and we have tried a number of variations to no avail.

Script below:

//check to see if a record already exists for the user in the u_onboarding table  
var oofb = producer.nameofemp; //nameofemp is the name of the sys_user variable  
var rec = new GlideRecord('u_onboarding');  
rec.addQuery('u_user_id', oofb);  
rec.query();  
//if we find a record update it  
if (rec.next()) {  
  rec.current.state = '7';  
  rec.update();  
 
  current.setAbortAction(true);  

}  

Any help offered would be very much appreciated.

Thanks,

Charles

1 ACCEPTED SOLUTION

The correct script is as follows:



//check to see if a record already exists for the user in the u_onboarding table

      var oofb = producer.u_user_id; //u_user_id is the name of the sys_user variable, which is mapped to the u_user_id field on the u_onboarding table


      var rec = new GlideRecord('u_onboarding');


      rec.addQuery('u_user_id', oofb.sys_id);


      rec.query();


      //if we find a record update it


      if (rec.next()) {


     


          rec.state = '7';


          rec.update();


         


          current.setAbortAction(true);



producer.redirect = 'u_onboarding.do?sys_id=' + rec.sys_id;  


      }  


View solution in original post

5 REPLIES 5

Subhajit1
Giga Guru

First:-


Instead of rec.current.state = '7';   use rec.state = '7';


2nd:-


I assume that the u_user_id column in the u_onboarding table is a reference column.


Please log the return of the oofb, this should be a sys_id.


Thanks Subhajit,



I have changed the lines as follows, but this now returns 'Record not found'.



rec.addQuery('u_user_id', oofb.sys_id);
  rec.state = '7';


Thanks, Charles


Your nameofemp variable in RP is a Reference or a String?


Also i asked you to check if oofb when being logged is returning a sys_id or not. So no oofb.sys_id. That line stays the way it is.


Ok thanks Subhajit -   the nameofemp is a reference on the sys_user table.



I need to correct something - the script above returns an error 'Record not found' - It was the previous script I tried (below) that returned the new, blank form.



var oofb = new GlideRecord("u_onboarding");


oofb.get(nameofemp);


if (oofb){


oofb.state = '7';


oofb.update();


}


current.setAbortAction(true);



Thanks.