How do I autopopulate the Entity field with the information of the opened_by user's department tag.

ChuanYanF
Tera Guru

Dear experts, 

 

I would like to ask how should I autopopulate the entity field in my risk event form with the opened_by user's department details? So currently my entity is defined as department in this scenario. For example, my entity name is IT and the department tagged it also IT. I have defined the user's department and the entity's department, so i want to link them using this tagging. I have tried to create a onLoad client script, and a script include to try and get their linkage using their respectives departments. But I think my script might be wrong since it does not reflect the name of the department that is supposed to show. 
Script include:

var GetMatchingGRCProfile = Class.create();
GetMatchingGRCProfile.prototype = Object.extendsObject(
  // <— qualify the base class in scoped apps
  global.AbstractAjaxProcessor, {
  
  getProfileForDepartment: function() {
    var deptId = this.getParameter('sysparm_department_id');
    if (!deptId)
      return '';
      
    var gr = new GlideRecord('sn_grc_profile');
    gr.addQuery('department', deptId);
    gr.setLimit(1);
    gr.query();
    if (gr.next())
      return gr.getUniqueValue();
      
    return '';
  },

  type: 'GetMatchingGRCProfile'  // recommended for debugging
});

 Client script:

function onLoad() {
  var openedBy = g_form.getValue('opened_by');
  if (openedBy && !g_form.getValue('primary_profile')) {
    g_form.getReference('opened_by', function(user) {
      if (user.department) {
        var ga = new GlideAjax('global.GetMatchingGRCProfile');
        ga.addParam('sysparm_name', 'getProfileForDepartment');
        ga.addParam('sysparm_department_id', user.department);
        ga.getXMLAnswer(function(response) {
          if (response)
            g_form.setValue('primary_profile', response);
        });
      }
    });
  }
}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ChuanYanF 

I hope you have debugged if script include is getting called or not.

With that in my assumption

the field on sn_grc_profile which holds department is cmn_department and not department

So update this query

            gr.addQuery('cmn_department', deptId);
 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@ChuanYanF 

I hope you have debugged if script include is getting called or not.

With that in my assumption

the field on sn_grc_profile which holds department is cmn_department and not department

So update this query

            gr.addQuery('cmn_department', deptId);
 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

J Siva
Tera Sage

Hi @ChuanYanF 
You need to modify your script include as below to retrieve only the department-type entities

 getProfileForDepartment: function() {
        var deptId = this.getParameter('sysparm_department_id');
        if (!deptId)
            return '';

        var gr = new GlideRecord('sn_grc_profile');
        gr.addQuery('applies_to', deptId); //UPDATED THE FIELD AS "APPLIES TO"
		gr.addEncodedQuery('profile_class.name=Department'); //ADDED THIS TO GET ONLY THE ENTITY TYPE OF CLASS DEPARTMENT
        gr.setLimit(1);
        gr.query();
        if (gr.next())
            return gr.getUniqueValue();

        return '';
    },

Hope this helps.

Regards,
Siva