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

Murthy Ch
Giga Sage

Hi Sai Pavan,

You can achieve using after insert update business rule.

 

Thanks,

Murthy

Thanks,
Murthy

Can you please help me with the script that needs to be used in business rule ?

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

@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