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

Melghi
Kilo Expert

Create a new metric, put in type "Script calculation" , choos ethe table and choose the field worknotes,


and put a script similar to this :




var s = current.task;


if (gs.getUser().isMemberOf(<name oif the group>);


createMetric();




function createMetric() {


  var mi = new MetricInstance(definition, current);


  if (mi.metricExists())


      return;




  var gr = mi.getNewRecord();


  gr.start = current.sys_created_on;


  gr.end = current.sys_updated_on;


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


  gr.calculation_complete = true;


  gr.insert();


}


Thanks Mohamed, we're nearly there I think... but it only seems to work once per ticket and I also need to pull the users name into the Value field, any ideas.


Hi


to get the name gs.value = gs.getUser().getFullName()


Can share with me the content of your metric so I can check the reason it is running just once?


So this is what my script currently is, i've gone with var t as it was just giving me 'system' but this gives me the userid:



var s = current.task;   //Incident


var t = current.sys_updated_by; //user adding work note is giving the username but it won't give me the full name is I add '.getFullName()' to this object in the below line gr.value = t.getFullName()


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


createMetric();



function createMetric() {


  var mi = new MetricInstance(definition, current);


  if (mi.metricExists())


      return;



  var gr = mi.getNewRecord();


  gr.start = current.sys_updated_on; //date and time work note added


  gr.end = current.sys_updated_on; //date and time work note added


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


gr.value = t; //User who added work note


  gr.calculation_complete = true;


  gr.insert();


}



Screen Shot 2017-01-10 at 20.16.56.png



So this results in:


Screen Shot 2017-01-10 at 20.24.55.png


So this is what I need BUT it will not add extra entires for the same ID/Incident.



I really appreciate your help with this.