Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

The trigger should look at all contacts on an account and find the median NPS Score and update the field on the account called Median NPS score.

trigger NPSScore on Contact (before insert) {
     for (Contact contact : trigger.new){
        Set<Id> ContactId = new Set<Id>();
        List<Integer> contactvalues = new List<Integer>();
        for(Contact c:[Select Id, NPS_Rating__c from Contact where NPS_Rating__c != null ])

Integer sizeOfList = contactvalues.size();
Integer index = sizeOfList - 1;
Decimal median = 0.0;

// sort the list first
contactvalues.sort();

//Calculate median
if (Math.mod(sizeOfList, 2) == 0) {
   median = (contactValues[(index-1)/2] + contactValues[(index/2)+1])/2;
}else{
   median = contactvalues[(index+1)/2];
}

system.debug('the median is: '+median);
        account.Median_NPS_Score__c = median;
     }
}
share|improve this question
    
Is this a sub question or just the same with the post on salesforce.stackexchange.com/questions/5992/… You question is the same about how to update parent record from the Trigger on the child object. –  Jia Hu Jan 24 '13 at 1:14
    
What actually does happen? What have you tried? From the looks of things, you aren't selecting out the Account and separately updating it, which you'll probably need to do. I assume you are getting a compile error? –  jkraybill Jan 24 '13 at 4:15
    
Yes, this is a sub question. –  Merry Jan 24 '13 at 6:32
add comment

1 Answer 1

You can try the code below,

`trigger NPSScore on Contact (after insert, after update) {
    //find out Contact list updated or inserted
    Set ContactIds = new Set();
for (Contact con : Trigger.new) { if(con.NPS_Rating__c != null) { ContactIds.add(con.Id); } }

//find out the parent Account list of Contact Set<Id> AccountIds = new Set<Id>(); for(Contact con:[Select AccountId from Contact Where AccountId != null And Id IN:ContactIds ]) { AccountIds.add(con.AccountId); } //for each Account, calculate Median value and insert to the Account List<Account> accountforupdate = new List<Account>(); for(Account acc:[Select Id, (Select NPS_Rating__c from Contacts where NPS_Rating__c != null ) from Account Where Id IN:AccountIds]) { List<Integer> contactvalues = new List<Integer>(); for(Contact con:acc.Contacts) { contactvalues.add(Integer.valueOf(con.NPS_Rating__c)); } Integer sizeOfList = contactvalues.size(); Integer index = sizeOfList - 1; Decimal median = 0.0; // sort the list first contactvalues.sort(); //Calculate median if (Math.mod(sizeOfList, 2) == 0) { median = (contactValues[(index-1)/2] + contactValues[(index/2)+1])/2; }else{ median = contactvalues[(index+1)/2]; } system.debug('the median is: '+median); acc.Median_NPS_Score__c = median; accountforupdate.add(acc); } update accountforupdate;

}`

share|improve this answer
    
Please let me know if you have any questions about the code. It worked for me for insert or update, but I haven't tested it in a batch way. –  Jia Hu Jan 24 '13 at 1:54
    
Jia, I am getting this error: Error: Compile Error: expecting a semi-colon, found 'ContactIds' at line 3 column 8 –  Merry Jan 24 '13 at 6:32
1  
Jia, I figured it out! Yep, and I felt like a superstar. LOL Thanks for your help. I promise to learn more before my next project/question! :D –  Merry Jan 24 '13 at 6:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.