How to create automatically records in one table using another table data?

Shubhashis
Kilo Contributor

I have two tables in my app-Table A and Table B.

Table A is an user table ,where user can put their data and also update that in future. The fields are - Account name, Input days and Onsite/offshore.

Table B is a read-only table and not seen by the user. The fields are - Account name, Input days, Onsite/offshore, month and median rate.

So, when any records is created in Table A ,at the same time that record will be created in Table B with same account name, same input days and same onsite/offshore. But no duplicate record should not be there. The unique combination is-Account Name and onsite/offshore. The user only can update the table A by edit the input days and same change should be reflect in table B also.

So, please help me with BR for this requirement.

1 ACCEPTED SOLUTION

Please use below rectified code. 

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

// Add your code here
var table2= new GlideRecord('x_snc_aob_test1_dashboard_baseline_input');
table2.addQuery('account_name',current.account_name);
table2.addQuery('onsite_offshore',current.onsite_offshore);
table2.query();
if(table2.next()){
table2.baseline_input=current.baseline_input;
table2.update();
}
else{
var table2a= new GlideRecord('x_snc_aob_test1_dashboard_baseline_input');
table2a.initialize();
table2a.account_name=current.account_name;
table2a.onsite_offshore=current.onsite_offshore;
table2a.baseline_input=current.baseline_input;
table2a.insert();
}

})(current, previous);

Please mark the answer as correct and helpful.

Hope this answers will be helpful.
Please mark the answer as helpful and correct.

Best Regards,
Rajat Choudhary

View solution in original post

11 REPLIES 11

Rajat_Choudhary
Tera Expert

Hi Shubhashis,

You can create a after insert/updtae business rule.with condition input days changes.

In this business rule you need to query the combination of Accname+onsite/offshore in the other readonly table.
and insert/update the record accordingly.

Also you can set the index fields to use combination of fields as unique key.

find_real_file.png

-->You can use below code for reference.

var table2= new <table2>;
table2.addQuery('accname',current.accname);
table2.addQuery('onsite/offshore',current.onsite/offshore);
table2.query();
if(table2.next()){
table2.inputdays=current.inputdays;
table2.updtae();
}else{
var table2a= new <table2>;
table2a.initialize();
table2a.accname=current.accname;
table2a.onsite/offshore=current.accname;
table2a.inputdays=current.inputdays;
table2a.insert();
}

 

Hope this will be helpful.

Please mark answer as correct if solution is feasible.

Hope this answers will be helpful.
Please mark the answer as helpful and correct.

Best Regards,
Rajat Choudhary

Shubhashis
Kilo Contributor

so, when input days of any record is updated in table 1,it will also be updated table 2 or it will create a new record?

 

We need to add a check in our script if the accountname and onsite/offshore is already present,if yes update the record with new input days else create a new record.

you can add one more condition in business rule to check if input days is changed.

var table2= new <table2>;
table2.addQuery('accname',current.accname);
table2.addQuery('onsite/offshore',current.onsite/offshore);
table2.query();
if(table2.next()){
table2.inputdays=current.inputdays;
table2.updtae();
}else{
var table2a= new <table2>;
table2a.initialize();
table2a.accname=current.accname;
table2a.onsite/offshore=current.accname;
table2a.inputdays=current.inputdays;
table2a.insert();
}

Hope this answers will be helpful.
Please mark the answer as helpful and correct.

Best Regards,
Rajat Choudhary

Here table 2 means the User input table and table 2a means the Read only table. Right?