how we get 1. Create a record in Table2 whenever a record is inserted into Table1. 2. Update Short Description of a particular record of Table2 whenever a record is updated in Table1.

sachin dhek
Kilo Contributor

i am a fresher i am stuck in this problem last 3 days 

1 ACCEPTED SOLUTION

You need to add some query if you want to update existing record.

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

// Add your code here
var gr = new GlideRecord('u_table_1');
gr.addQuery(); // eg. gr.addQuery('fieldName', 'value'); 
//gr.addQuery('active', true);  // eg
gr.query();
//gr.insert();  // use this line if you want to create new record
while(gr.next()){

gs.log(gr.u_table_2); // here use gr.fieldName

gr.fieldName = 'value';
gr.update();  // to update the record matching your query.
 
}

})(current, previous);

 

Note: replace fieldName as per your column/field names in your tables.

 

Thanks,
Anil Lande

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

20 REPLIES 20

thank you sir......

 

Update Short Description of a particular record of Table2 whenever a record is updated in Table1.

this is my code is this right or not?

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

// Add your code here
var rec = new GlideRecord('u_table2');
//rec.addQuery('u_string_3',current.sys_id);
rec.query();
while(rec.next())
{
rec.u_string_3 = current.u_table_1;
rec.update();
}
})(current, previous);

Abhijit4
Mega Sage

Hi Sachin,

For your first scenario, you can create after insert BR on table1 and write a script to insert record in table 2

For your second scenario, you can create after update BR on table1 and write a script  to update short_description in table2.

Let me know if you have any further queries.

Please mark this as Correct or helpful if it helps.

Thanks and Regards,
Abhijit

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

sorry sir, but I can't understand what I do?

shloke04
Kilo Patron

Hi,

Before proceeding with your script, when you are creating both the tables you need to ensure that both the tables are related to each other.

So please follow the steps below to achieve your requirement:

1) Create Table 1 with fields you need like Short Description.

2) Create Table 2 with fields you need again of your choice like for example Short Description. Also in this table make sure to create another Reference field say name it as "Parent Linked" and refer it to Table 1 so that when a record is created in this Table 2 you can link it to Table 1.

3) This will ensure that both tables are linked to each other and when you are updating Short Description or any other field on table 1, you can fetch the respective record of table 2 which need to be updated with similar short description using a After Update Business Rule written on Parent table.

For example, I have tried replicating your scenario and have listed the steps below:

1) Table 1 Created as shown below with Short Description Field:

find_real_file.png

2) Similarly I have created another Table as table 2 with below two fields for now:

a) Short Description

b) Parent Linked referring to table 1 as shown below:

find_real_file.png

3) Now what I have done is created a Relationship so that .child Table in this case it is "Table2" gets shown as a Related list on table 1.

4) Navigate to "Relationship" module under System Definition as shown below:

find_real_file.png

5) Now configure the form as shown below and select your respective tables:

Script for Relationship:

(function refineQuery(current, parent) {

	// Add your code here, such as current.addQuery(field, value);
	parent.addQuery('u_parent_linked',current.sys_id);

})(current, parent);

find_real_file.png

Select the Table Names as shown above and now Navigate to your Table 1 Form and then Right click on the header of the form and select Configure--> Related List as shown below:

find_real_file.png

Now select the Relationship which we have created in above steps as shown below and click on Save button:

find_real_file.png

Output:

find_real_file.png

Table 2 Form screenshot with Table 1 linked as shown below:

find_real_file.png

Now in order to Update Short Description when ever Short Description gets updated in Table 1 and also create Record in Table 2 when ever a record is created in Table1 , you need to write a After Insert and  Update Business Rule on Table 1 and use the script below:

Script:

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

	// Add your code here
	if(current.operation() == 'insert'){
		createRecordTable2(current);
	}else if(current.operation() == 'update'){
		updateRecordtable2(current);
	}
	
	function createRecordTable2(current){
		var gr = new GlideRecord('u_table_2');
		gr.initialize();
		gr.TABLE2_FIELDNAME= current.TABLE1_FIELDNAME; 
		gr.u_parent_linked = current.sys_id; // Replace "u_parent_linked" with the field present on your Table 2 which is referring to Table 1
		gr.insert();
	}
	
	function updateRecordtable2(current){
		var gr1 = new GlideRecord('u_table_2');
		gr1.addQuery('u_parent_linked',current.sys_id);// Replace "u_parent_linked" with the field present on your Table 2 which is referring to Table 1
		gr1.query();
		while(gr1.next()){
			gr1.TABLE2_FIELDNAME= current.TABLE1_FIELDNAME;
			gr1.update();
		}
	}
	
	

})(current, previous);

find_real_file.png

find_real_file.png

Let me know if you are facing an issue here.

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke