Metric help needed - need to capture when work_notes updated by a member of a specific team

dave_edgar
Mega Guru

having a brain malfunction

Need to capture, in metric definitions, the user that has added a work note AND the user is a member of a specific team

HELP!!!

1 ACCEPTED SOLUTION

dave_edgar
Mega Guru

we have a weiner...



With help from @mohammed.ali at Hi Support we have matched the user updating to users in the group members table.   We now have a winning script:



var s = current.task;   //Incident being updated


var t = current.sys_updated_by; //user adding work note



var p = new GlideRecord ('sys_user');


var b;


p.addQuery('user_name',t);


p.query();


while (p.next()){


  b = p.sys_id;


}



var m = new GlideRecord ('sys_user_grmember');


m.addQuery('user',b);


m.addQuery('group.name','STARTSWITH', 'Your team name prefix here');


m.query();


if (m.next()){u=true;}


else u=false;



gs.log('MTRC ref number here: '+u);



if (u == true){


  gs.log('Execute script createMetric: MTRC ref number here '+ '. Work Note added by:   '+ t + ' a member of Your team name prefix here.');


      createMetric();


}


else{


  gs.log('Do NOT execute script createMetric: MTRC ref number here '+ '. Work Note added by:   '+ t + ' NOT a member of Your team name prefix here.');


}



function createMetric() {


  var mi = new MetricInstance(definition, current);


  var gr = mi.getNewRecord();


  gr.start = current.sys_updated_on;


  gr.end = current.sys_updated_on;


  gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());


  gr.value = t;


  gr.calculation_complete = true;


  gr.insert();


}


View solution in original post

14 REPLIES 14

ah ha, got it



//     if (mi.metricExists())


//         return;



comment this out and working as needed, brill  


so, can not get the users full name as sys_updated_by is not a reference field in incident, so it will have to do.   Therefore we are left with this script working as needed:



var s = current.task;   //Incident being updated


var t = current.sys_updated_by; //user adding work note


if (gs.getUser().isMemberOf("UK&ROI - Level 2 - Corporate"));


createMetric();


function createMetric() {


  var mi = new MetricInstance(definition, current);


  var gr = mi.getNewRecord();


  gr.start = current.sys_updated_on;


  gr.end = current.sys_updated_on;


  gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());


gr.value = t;


  gr.calculation_complete = true;


  gr.insert();


}


Why do you prefer working with


var t = current.sys_updated_by; //user adding work note



instead of



var t = gs.getUser(); ?


bcoz the users name is not brought back when using gs.getUser();


I'd love to be able to use it this so I can get the full user name but it just return 'system'.


grr it looks like the isMemberOf isn't working now, it's actually creating an record for everyone adding a work note, neither of these seem to work



if (gs.getUser().isMemberOf("UK&ROI - Level 2 - Corporate"));


if (gs.getUser().isMemberOf("5f5d736f37a2c200dd95db9643990e34"));