Assistance on populating reference field from Order guide record produced records

miles9
Tera Expert

Hi all, 

 

I hope I could get some nudge in the correct direction on how to resolve my issue.

 

The current situation I have is I have created a custom application that has multiple custom tables.

Three tables are the most important.  In this post they will be referred to as CAR, ONG, and PRO.  The CAR table is the "parent" table.  I have added to both ONG and PRO tables a reference variable that references the CAR table.

 

Upon this I have created 3 record producers that are included in an Order guide.  

 

Once the order guide is submitted, it DOES create the records on the 3 tables correctly.

 

The issue that I am trying to resolve is to "Link" the records together.  After submission I would like the ONG and PRO references to Populate/to have filled the CAR number.  I can obviously do this manually and it works but would like to get this in a script of some sort.

 

I have tried using a business rule but it kept failing at setting the Reference field.  Below is an example of what I tried to do.   I had in my code entries to log in the sys log table.  The error I kept getting was "com.glide.script.RhinoEcmaError: Cannot set property "c_arm_string" of undefined to..."

 

 

    var gr = new GlideRecord('cutom table name child');
    gr.orderByDesc('sys_created_on');
    gr.setLimit(1);
    gr.query();
    if (gr.next()) {
        var carCreatedOn = gr.sys_created_on;
        var ongCreatedOn = current.sys_created_on;
		var test = gr.sys_id;
        if(ongCreatedOn == carCreatedOn){
			var res2 = gr.sys_id;
			current.c_arm = res2;
		} 
    }

 

CARRecord.png

relatedlists.png

protable.png

ongtable.png

pro.png

ong.png

car.png

       

Any help would be great.  Mostly looking for ideas or solution to this.

 

Thank you

1 ACCEPTED SOLUTION

sach1
Tera Guru

As the 3 records are getting created at the same time it would be better to use a after BR. If the above BR you mentioned is after BR then you are missing 'current.update()' in script.

View solution in original post

2 REPLIES 2

sach1
Tera Guru

As the 3 records are getting created at the same time it would be better to use a after BR. If the above BR you mentioned is after BR then you are missing 'current.update()' in script.

Thank you @sach1.  I was able to resolve my issue.

 

I found this thread in the community.

https://www.servicenow.com/community/developer-forum/business-rule-not-setting-values-on-incident/m-...

 

It stated that "After" BRs wouldn't work to set the values.  I think I was under the same mindset that I was to update the values "After" insert of the record, while in reality I am inserting the record upon creation so using the "Before" BR was really what I was supposed to use.