Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Advanced user criteria for potal business

mariasilva
Tera Contributor

I need to create a user criteria that meets the following criteria: The Contract table is ast_contract, the Contact table is customer_contact, and the Account table is customer_account. This should be an advanced user criteria with a script to check the current contact in the portal, considering the contacts in the Contact tab of the customer_account table, query all ACTIVE contracts for the account they are part of, and then query, depending on whether they are AMS criteria, the sys_id of the contract model found in the sn_b2b_portal.glide.sc.portal.ams_contract_models property. If the glidequery contains next, that is, there is at least one record, return true; otherwise, return false.

I created this code, but it's not working. Could someone please help me?

 

(function() {
    var contact = gs.getUser().getID();
    var account = new GlideRecord('customer_account');
    account.addQuery('contact', contact);
    account.query();

    if (account.next()) {
        var contract = new GlideRecord('ast_contract');
        contract.addQuery('account', account.sys_id);
        contract.addActiveQuery();

        var maintModels = gs.getProperty('sn_b2b_portal.glide.sc.portal.maintenance_contract_models').split(',');
        contract.addQuery('contract_model', 'IN', maintModels);
        contract.query();

        return contract.hasNext();
    }
    return false;
2 REPLIES 2

kaushal_snow
Giga Sage

Hi @mariasilva ,

 

Good day !!

Try out this code and try checking  or validate that sn_b2b_portal.glide.sc.portal.ams_contract_models property exists and holds only valid sys_ids..

 

(function () {
  
  var userSID = gs.getUserID();

  var accounts = new GlideQuery('customer_contact')
    .where('user', userSID)
    .where('active', true)
    .select('account')
    .toArray();

  if (!accounts.length)
    return false; 

  var propVal = gs.getProperty('sn_b2b_portal.glide.sc.portal.ams_contract_models') || '';
  var models = propVal.split(',')
    .map(function (s) { return s.trim(); })
    .filter(function (s) { return s && s.length === 32; });

  if (!models.length)
    return false; 

  // For each associated account
  for (var ai = 0; ai < accounts.length; ai++) {
    var acctId = accounts[ai].account;
    var c = new GlideQuery('ast_contract')
      .where('account', acctId)
      .where('active', true)
      .where('contract_model', 'IN', models)
      .select('sys_id')
      .next();
    if (c)
      return true; 
  }

  return false;
})();

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

It is displaying this message in the portal and not showing the topic: ErrorGlideQuery undefined, maybe missing global qualifier