Get auto number for record just inserted with GlideRecord

scwillson
Mega Guru

I am just wondering if there is a quick and easy command to use to get the 'number' field of a record just created from a glide insert.

I have created a custom application involving a many to many relation ship table. From an inbound action I create a record in a table that has auto numbering turned on (call it "ED123") . That same inbound action also pulls data from the email to identify and create a separate record on a separate table (call it "PT456"). I want to them take those two records, and create a record on the m2m relationship table.

The part of this that I am having trouble with is that the auto assigned "ED" number isn't generated until after the insert() command. Is there a way to still query it a 'current' after insert? or do I just have to build a query to identify the most recently created?

Here's my Inbound Action:

//set email parts to variables

var body = email.body_text;

var emSubject = email.subject;

var sender = gs.getUser();

var ed = current.number //This is the part that doesn't work for me, because the record's 'number' hasn't been generated yet

//identify specific REGEX

var PT = body.match(/PT\d{8}/);

//create PT record

var gr = new GlideRecord('x_tekm_edata_pt_list');

gr.pt = PT.toString();

gr.insert();

//place text in fields, and create new eData record

current.ptcmid = PT.toString();

current.original_email = "Subject: " + emSubject + "\nBody: " + body;

current.requestor = sender.getDisplayName();

current.insert();

//create relationship record

var rel = new GlideRecord('x_tekm_edata_m2m_pt_lists_edata_tables');

rel.pt_list = PT.toString();

rel.edata_table = ed;   //This is where it would put the ED number, but when the variable was declared, the number didn't exist yet.

rel.insert();

1 ACCEPTED SOLUTION

ccajohnson
Kilo Sage

In some UI Actions that create records on another table, they declare a variable when the current record is inserted. This will capture the inserted record as an object that you can use in other parts of the script.




I am guessing that the edata_table is a reference field. If that is the case, then you can leverage the sys_id of the record that was inserted. Here is your example changed to leverage this:




//set email parts to variables  


var body = email.body_text;  


var emSubject = email.subject;  


var sender = gs.getUser();  


//var ed = current.number //Commented out since we do not want to caputre this yet.


 


//identify specific REGEX  


var PT = body.match(/PT\d{8}/);  


 


//create PT record  


var gr = new GlideRecord('x_tekm_edata_pt_list');  


gr.pt = PT.toString();  


gr.insert();  


 


//place text in fields, and create new eData record  


current.ptcmid = PT.toString();  


current.original_email = "Subject: " + emSubject + "\nBody: " + body;  


current.requestor = sender.getDisplayName();  


var edID = current.insert();   //Changed to use edID to capture the current inserted object


 


//create relationship record  


var rel = new GlideRecord('x_tekm_edata_m2m_pt_lists_edata_tables');  


rel.pt_list = PT.toString();  


rel.edata_table = edID.sys_id;   //Guessing this is a reference field and thus needs the unique identifier defined.


rel.insert();  



Let me know if you are more successful.


View solution in original post

3 REPLIES 3

ccajohnson
Kilo Sage

In some UI Actions that create records on another table, they declare a variable when the current record is inserted. This will capture the inserted record as an object that you can use in other parts of the script.




I am guessing that the edata_table is a reference field. If that is the case, then you can leverage the sys_id of the record that was inserted. Here is your example changed to leverage this:




//set email parts to variables  


var body = email.body_text;  


var emSubject = email.subject;  


var sender = gs.getUser();  


//var ed = current.number //Commented out since we do not want to caputre this yet.


 


//identify specific REGEX  


var PT = body.match(/PT\d{8}/);  


 


//create PT record  


var gr = new GlideRecord('x_tekm_edata_pt_list');  


gr.pt = PT.toString();  


gr.insert();  


 


//place text in fields, and create new eData record  


current.ptcmid = PT.toString();  


current.original_email = "Subject: " + emSubject + "\nBody: " + body;  


current.requestor = sender.getDisplayName();  


var edID = current.insert();   //Changed to use edID to capture the current inserted object


 


//create relationship record  


var rel = new GlideRecord('x_tekm_edata_m2m_pt_lists_edata_tables');  


rel.pt_list = PT.toString();  


rel.edata_table = edID.sys_id;   //Guessing this is a reference field and thus needs the unique identifier defined.


rel.insert();  



Let me know if you are more successful.


Thanks for the input Christopher! I was able to get it to work with your suggestion, with just a minor tweak; adding the 'sys_id' onto the variable wasn't necessary. I also had to change my declaration of the PT field on the relationship table. So it ended up as:



//create PT record


var gr = new GlideRecord('x_tekm_edata_pt_list');


gr.pt = PT.toString();


var ptID = gr.insert();



//get current


var edID = current.insert();



//create relationship record


var rel = new GlideRecord('x_tekm_edata_m2m_pt_lists_edata_tables');


rel.pt_list = ptID;


rel.edata_table = edID;


rel.insert();


SME
Giga Guru

Not sure whether it will work or not but have you tried using function :- "getNextObjNumberPadded()"