Foreach in gliderecord?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2022 01:31 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2022 01:58 PM
Add 'gs.info("The number of records from the query is " + agreementdetail.getRowCount() + ".");
before the while statement to see how may records are returned.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2022 02:12 PM - edited 10-26-2022 02:18 PM
Thanks ! so once I find the number of records how do I traverse the loop is where I am confused.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2022 04:28 PM
Your 'while loop' looks good: "while(agreementdetail.next()) {". If there are multiple records, then all must be updated. I'm guessing the script you provided is from a business rule. But you're updating 'current.[table_field]' in the loop. do you expect records in the 'u_agreement_detail' table to be updated? what table is the business rule defined on?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2022 04:53 AM
I am actually doing it on "Save" UI action on the "Agreement' table.
for save on Agreement table, the idea is to get all the agreement details (related to that agreement) and updating two fields on Agreement with one or more rows on agreement table.
Should it be on a business rule instead?