function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brooks Johnson 6Brooks Johnson 6 

Convert String to Integer

I am trying to write a method to get the median value on  a set of scores on the Contact object.  I have a list from the Contact object that is looking for one value from a numeric field. But I am getting an error that arithmetic expressions must use numeric arguments. I am assuming this is coming from my trying to do math with my Contact list values?
 
public class CalculateHealthMetrics {
    
    
    public static void getMedianPardotScore(){
        Decimal medianPiScore = 0;
        Integer listSize             = 0;
        Integer index               = listSize -1;
        //get all Pardot Scores in Descending Order
        List<Contact> pardotScores = [SELECT pi__score__c
                                      FROM Contact
                                      ORDER BY pi__score__c DESC];
        listSize = pardotScores.size();
        system.debug('list size =' + listSize);
        pardotScores.sort();
        if(Math.mod(listSize, 2) == 0){
            medianPiScore = (pardotScores[(index -1) / 2] + pardotScores[(index / 2) + 1]) / 2;
        }
        
        
        
    }
    
}

 
Steven NsubugaSteven Nsubuga
public class CalculateHealthMetrics {
    
    
    public static void getMedianPardotScore(){
        Decimal medianPiScore = 0;
        Integer listSize             = 0;
        Integer index               = listSize -1;
        //get all Pardot Scores in Descending Order
        List<Contact> pardotScores = [SELECT pi__score__c
                                      FROM Contact
                                      ORDER BY pi__score__c DESC];
        listSize = pardotScores.size();
        system.debug('list size =' + listSize);
        pardotScores.sort();
        if(Math.mod(listSize, 2) == 0){
            medianPiScore = (pardotScores[(index -1) / 2].pi__score__c + pardotScores[(index / 2) + 1].pi__score__c) / 2;
        }
        
        
        
    }
    
}

 
Abhishek BansalAbhishek Bansal
Hi Brooks,

You are using the complete contact record in your calculation which the reason you are getting the error. I have modified the code as per below:
public class CalculateHealthMetrics {
    
    public static void getMedianPardotScore(){
        Decimal medianPiScore = 0;
        Integer listSize = 0;
        //get all Pardot Scores in Descending Order
        List<Contact> pardotScores = [SELECT pi__score__c
                                      FROM Contact
                                      ORDER BY pi__score__c DESC];
        listSize = pardotScores.size();
		Integer index = listSize -1;
        system.debug('list size =' + listSize);
		
        pardotScores.sort();
        if(Math.mod(listSize, 2) == 0){
            medianPiScore = (pardotScores[(index -1) / 2].pi__score__c + pardotScores[(index / 2) + 1].pi__score__c) / 2;
			//if pi__score__c is not a number type field then comment the above line and uncomment the below line
			//medianPiScore = (Decimal.valueOf(pardotScores[(index -1) / 2].pi__score__c) + Decimal.valueOf(pardotScores[(index / 2) + 1].pi__score__c)) / 2;
        } 
    }
    
}

Please let me know if you face any issues with the code mentioned above.

Thanks,
Abhishek Bansal.