Cannot use gs.IsMobile()

rafas_10
Kilo Sage

Hi everyone,

 

I hope you're doing well.

 

I need help regarding how to identify is a HR Case is created via mobile. Basically what I want to achieve is when an HR Case is created via mobile, It would change the source to 'Mobile app'.

 

I was using a business rule where the condition was gs.IsMobile(), but I was with the property 'glide.ui.m_enabled' as 'true' and I need tot keep it as 'false' and I also cannot change the property 'glide.ui.mobile_agents'.

 

Anyone has any ideia how to workaround this?

 

Thanks.

3 REPLIES 3

kalanidhikr
Tera Expert

Hello @rafas_10 

Problem: gs.isMobile() returns false when glide.ui.m_enabled=false, so you can’t rely on it to detect mobile origin.

 

Workaround (accepted in practice): In a Before Insert BR on sn_hr_core_case, read the User-Agent header server‑side and, if it matches mobile patterns, set Source (contact_type) = “Mobile app.” This avoids changing glide.ui.m_enabled or glide.ui.mobile_agents
-------------------------------------------------------------------------------------------

(function executeRule(current, previous) {
  var trx = GlideTransaction.get();
  if (!trx) return;
  var req = trx.getRequest();
  if (!req) return;

 

  var ua = (req.getHeader('User-Agent') || '').toLowerCase();

 

  // Heuristics for mobile / ServiceNow Agent
  var isMobileUA = ua.indexOf('servicenow') > -1 ||  // Agent/Now Mobile often includes "ServiceNow"
                   ua.indexOf('iphone') > -1 ||
                   ua.indexOf('android') > -1 ||
                   ua.indexOf('mobile') > -1;

 

  if (isMobileUA) {
    current.contact_type = 'mobile_app'; // use your choice value for “Mobile app”
  }
})(current, previous);

-----------------------------------------------------------------------------------------
Also Please check below solution this should work


https://www.servicenow.com/community/developer-forum/i-can-t-use-gs-ismobile/m-p/2654317


https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0785111

 

If my response helped mark as helpful and accept the solution

AnkaRaoB
Mega Guru

HI @rafas_10 ,

method gs.isMobile() only works when the property glide.ui.m_enabled = true.

If this property is disabled (and cannot be changed), gs.isMobile() will always return false.

So, it cannot be used to detect cases created from the ServiceNow Mobile App in this scenario.

 Recommended Workaround (Best Practice)

When a record is created from the Mobile App, the request contains a special User-Agent value such as:

ServiceNowMobile

NowMobile

ServiceNow Mobile

You can detect this using a Before Insert Business Rule.

 Sample Business Rule

Table: sn_hr_core_case

When: Before Insert

(function executeRule(current, previous) {

    var userAgent = gs.getSession().getClientData('user_agent') + '';

    if (userAgent.toLowerCase().includes('servicenowmobile') ||

        userAgent.toLowerCase().includes('nowmobile') ||

        userAgent.toLowerCase().includes('servicenow mobile')) {

        current.source = 'Mobile app';

    }

})();

 

If this helps you then mark it as helpful and accept it as solution.

Ankur Bawiskar
Tera Patron

@rafas_10 

I doubt this is feasible to achieve

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader