Foreach in gliderecord?

gnewuser
Tera Contributor

I have 2 tables and agreement and agreement_detail table . There is a primarycompany and secondarycompany table in agreement (which is mainly used for display purposes). The agreement_detail table has company , company type  as columns and a reference to the agreement table.

so this is how it looks from the front end

 

So for one agreement in agreement table would have 2 or more rows in the detail table.

 

Agreement table -> Agreement 1, apple, microsoft

Agreement Detail table -> Agreement1(which is a reference to the agreement table), apple,primary

Agreement Detail table -> Agreement1(which is a reference to the agreement table), microsoft,secondary

 

Now the requirement is to update  the primarycompany and secondarycompany field in the agreement table when the companies are getting added to the detail table. The detail table is a related list of the agreement table.

 

var agreementdetail = new GlideRecord('u_agreement_detail');
agreementdetail.addQuery('u_agreement_id',current.sys_id)//this is the current agreement id
agreementdetail.query();
while(agreementdetail.next()){

if (agreementdetail.company_type=='primary')

current.primarycompany = agreementdetail.u_company.getDisplayValue();
else
current.secondarycompany = agreementdetail.u_companygetDisplayValue();
}

 

The above code just updates 1 obviously either the primary or secondary even though there are 2 rows for one agreement.

 

I think I would have to use for each but not sure how to do it with gliderecord.?

 

Thanks in advance

 

 

 

5 REPLIES 5

Bert_c1
Kilo Patron

Yes, a business rule is most common for this, updating a table record's fields based on an update to a field from values in a different table.  The "current" object is available to business rules, the script template (when Advanced is check on the BR definition is:

 

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

	// Add your code here

})(current, previous);

I am guessing the update is not on the 'u_aggreement_details' table, but a different table.  You can set the BR to run "before" and on 'Insert" and "Update", since you are updating the 'current' object.

 

I don't believe UI Actions can query tables, unless AJAX is used. But I'm not a good source for Client Side behavior.