I have a field on the contact called 'NPS Score'. I need a trigger that calculates the median of all contact NPS score's on the account. I have code to calc a median score, I am just not sure how to write the trigger. Someday I need to get to a class!!
List<Integer> testvalues = new List<Integer>();
// Insert some values
testvalues.add(1);
testvalues.add(6);
testvalues.add(2);
testvalues.add(8);
testvalues.add(7);
testvalues.add(2);
Integer sizeOfList = testvalues.size();
system.debug('size of list is '+ sizeOfList);
Integer index = sizeOfList - 1;
system.debug('the index is '+index);
Decimal median = 0.0;
// sort the list first
testvalues.sort();
//Calculate median
if (Math.mod(sizeOfList, 2) == 0) {
median = (testValues[(index-1)/2] + testValues[(index/2)+1])/2;
}else{
median = testvalues[(index+1)/2];
}
system.debug('the median is: '+median);
