New query against same table

wiltonr
Giga Contributor

Is it possible to query the same table in a script without declaring a new var?

I have a script include where I am querying a custom table and when I get the results need to do specific things; then I want to query the same table with different criteria to do different specific things.   I need to query the same table four times to do somewhat similar things.

Example:

      var bills = new GlideRecord('u_citnet_billing');
    bills.addQuery('u_service_billing_profile.u_fund_to_credit', 'CONTAINS', 'NF');
    bills.addQuery('u_billing_status','Ready for Billing');
    bills.addQuery('u_billing_account',account);
    bills.query();
    while(bills.next()){
    //perform some calculations
    }
   
    Then I want to query the same table but for one difference:
    bills.addQuery('u_service_billing_profile.u_fund_to_credit', 'CONTAINS', 'IN');
    bills.addQuery('u_billing_status','Ready for Billing');
    bills.addQuery('u_billing_account',account);
   

Do I have to declare a new variable as in:

    var bills2 = new GlideRecord('u_citnet_billing');
    bills2.addQuery('u_service_billing_profile.u_fund_to_credit', 'CONTAINS', 'IN');
    bills2.addQuery('u_billing_status','Ready for Billing');
    bills2.addQuery('u_billing_account',account);
    bills2.query();
    while(bills2.next()){
    //perform some calculations
    }
   

Or is there a more efficient way to re-query the same table?

6 REPLIES 6

Of all the solutions offered so far, this 'function' option is the neatest. I wonder, is there scope to use 'STARTSWITH' rather than 'CONTAINS' in the addQuery? If you are able to check for fields which start with IN or NF, the database will be able to leverage an index, which should speed up the response for you.


ProbirDas
Tera Expert

Dear Rhonda,



You don't have to define a separate Glide Record object for a subsequent query provided you won't use the first object later



var bills = new GlideRecord('u_citnet_billing');


  bills.addQuery('u_service_billing_profile.u_fund_to_credit', 'CONTAINS', 'NF');


  bills.addQuery('u_billing_status','Ready for Billing');


  bills.addQuery('u_billing_account',account);


  bills.query();


  while(bills.next()){


  //perform some calculations


  }



bills = new GlideRecord('u_citnet_billing');


bills.addQuery('u_service_billing_profile.u_fund_to_credit', 'CONTAINS', 'IN');


bills.addQuery('u_billing_status','Ready for Billing');


bills.addQuery('u_billing_account',account);


bills.query();


while(bills.next()){


//perform some calculations


}