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
VisithraVisithra 

hi, i need to get my max salary from the custom object(employye__c)from give passing parameter called position__c if any postion given in parameter a max salary should be get.

public static void maxSalaryOFPositions(string position){
    List<Employee__c> existingEmployees = [SELECT Id, Name, Position__c, Salary__c FROM Employee__c];
     integer MaxSalary= existingEmployees[0].Salary__c;
    for(Employee__c emp:existingEmployees){
        
        if((emp.Position__c==position)&&(emp[1].Salary__c>MaxSalary)){
            MaxSalary=emp[1].Salary__c;
        }
        
    }system.debug('The max salary of'+position+ 'and its salary is'+MaxSalary);
        
   
       
}

pls help me with my code and here is my code. am getting 2 errors as illegal assignemnt from Decimal to integer and Expression must be list type:Employee__c
 
Best Answer chosen by Visithra
TobyKutlerTobyKutler

public static void maxSalaryOFPositions(string position){
List<Employee__c> existingEmployees = [SELECT Id, Name, Position__c, Salary__c FROM Employee__c];      Decimal MaxSalary= 0;    
 for(Employee__c emp :existingEmployees){                  
if((emp.Position__c==position)&&(emp.Salary__c>MaxSalary)){ 
MaxSalary= emp.Salary__c;         
      } 
   } 
system.debug('The max salary of'+position+ 'and its salary is'+MaxSalary);              
}


This should run solve your errors. The illegal assignment from decimal to integer is because the Salary__c is a decimal and you are setting it to the MaxSalary value which you instantiated as an Integer. The record is because you treat the "emp" variable as a List when it is a single record. You would not want to use 1 either since that would always be the second element in the list. If you need to access a certain index in the loop you should use syntax like 
 
for(Integer i =0; i < existingEmployees.size() ; i++){
//then access an element by index like 
        if((existingEmployees[i].Position__c==position)&&(existingEmployees[i].Salary__c>MaxSalary)){  
          MaxSalary= emp.Salary__c;        
 } }

​​​​​​​