How to create and update records in custom table when a record is created and updated in other custom table ?

Sai Pavan1
Tera Contributor

Hi Community,

I have a requirement where - 'If a record gets inserted / Updated in a custom table then same respective record should be created / updated in another custom table'. The Two tables have same set of fields. What script needs to be used in this type of scenario ?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Sai Pavan 

Considering you have Table A as source and Table B(custom table) and Table B has reference field pointing to Table A (this reference field will help you knowing which record of Table B to update)

you need After Insert & Update BR on Table A

Script like this: Enhance it further as per your case

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

	// Add your code here
	if(current.operation() == 'insert'){
		var gr = new GlideRecord("tableB");
		gr.initialize();
		gr.u_fieldA = current.u_fieldA;
		gr.u_fieldB = current.u_fieldB;
		gr.u_referenceField = current.sys_id;
		gr.insert();
	}
	else if(current.operation() == 'update'){
		var rec = new GlideRecord('tableB');
		rec.addQuery("u_referenceField", current.sys_id);
		rec.query();
		if(rec.next()){
			rec.u_fieldA = current.u_fieldA;
			rec.u_fieldB = current.u_fieldB;
			rec.update();
		}
	}

})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

10 REPLIES 10

Thank you @Ankur Bawiskar 

I have used your script and

1. It is creating a record in table B whenever there is a record created in Table A.

2. It is updating same record in table B whenever there is an update in Table A.  But Similarly, if we make an update in record in table B it should update the same record in table A as well. 

if(current.operation() == 'update'){
		var rec = new GlideRecord('tableB');
		rec.addQuery("u_referenceField", current.sys_id);
		rec.query();
		if(rec.next()){
			rec.u_fieldA = current.u_fieldA;
			rec.u_fieldB = current.u_fieldB;
			rec.update();
		}
	}

I have used the above script on the after update business rule on Table B. (Note - The Two Tables have the reference fields) 

Hi,

then this should work fine

if(current.operation() == 'update'){
    var rec = new GlideRecord('tableA');
    rec.addQuery("sys_id", current.u_referenceField);
    rec.query();
    if(rec.next()){
        rec.u_fieldA = current.u_fieldB;
        rec.update();
    }
}

Please ensure this doesn't go into loop since you are also having after update BR on table A

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hello @Sai Pavan 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Mahendra RC
Mega Sage

Hello Sai Pavan,

You can write a after insert/update BR on your table in which the record is created and write something like below code:

(function executeRule(current, previous /*null when async*/) {
		var tableObject = new GlideRecord('<custom_table_2>');
		tableObject.addQuery("your_reference_field", current.getUniqueValue());
		tableObject.query();
		if(tableObject.hasNext()){
			tableObject.next();
			tableObject.your_field_1 = current.getValue("your_field_1");
			tableObject.your_field_2 = current.getValue("your_field_2");
			tableObject.update();
		} else {
			tableObject.initialize();
			tableObject.your_field_1 = current.getValue("your_field_1");
			tableObject.your_field_2 = current.getValue("your_field_2");;
			tableObject.your_field_3 = current.getValue("your_field_3");;
			tableObject.insert();
		}
	}
})(current, previous);

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

 

Hello @Sai Pavan 

Just wanted to check with you, if the my above response answered your question. If yes, then please do close this thread/question by marking the appropriate response as correct.

If you still need any further help or guidance on this then please update those on this question.

Thanks