+ Start a Discussion
Karthikeyan ChandranKarthikeyan Chandran 

Need help on to get least count records from a list.

Hi Team,

here my code,
 
Integer SpecRepCount                      = Integer.valueOf(Label.Constant_PQCapMaxCount);
        SpecRepList           = [SELECT Id, Name,Shift__c, Email__c, Next_Pickup_Counter__c,QC_Rep__c from Spec_Build__c WHERE Available__c = true AND Next_Pickup_Counter__c < :SpecRepCount ORDER BY Next_Pickup_Counter__c ASC LIMIT 1];
        if(!SpecRepList.isEmpty()) 
        {
            cas.Spec_Build_Rep__c                 = SpecRepList[0].Email__c;
            SpecRepList[0].Next_Pickup_Counter__c = SpecRepList[0].Next_Pickup_Counter__c + 1;
            specBuildRepName = SpecRepList[0].Name;
            
           
            //Arrange Map
            Spec_Build__c specBuild = new Spec_Build__c();
            specBuild.id            = SpecRepList[0].id;
            specBuild.Next_Pickup_Counter__c = SpecRepList[0].Next_Pickup_Counter__c;
            
            //Map
            mapNextCounter = new Map<Id,Spec_Build__c> ();
            mapNextCounter.put(specBuild.id,specBuild);
            
            //SpecRepList.add(specBuild);
            //update specBuild;
            //SpecRepList.add(SpecRepList[0]);
        } 
        else 
        {                    
            cas.Spec_Build_Rep__c             = Label.SpecBuild_Manager_Email;
            //specBuildRepName = Label.SpecBuild_Manager_Email;                                  
        }

We have to quey the least count from this object 'Spec_Build__c' and  the "Next_Pickup_Counter__c" should increase +1.

This is not working here.

Can any one help me to fix this?

Thanks

 
Nayana KNayana K
List<AggregateResult> lstAR = [	SELECT Id, MIN(Next_Pickup_Counter__c)
								FROM Spec_Build__c
								WHERE Next_Pickup_Counter__c != NULL
								AND Available__c = true AND Next_Pickup_Counter__c <: SpecRepCount
								GROUP By Id
								LIMIT 1];
							
if(!lstAR.isEmpty())
{
	Id idSpecBld = lstAR[0].get('Id')(Id);
	SpecRepList = [SELECT Id, Name,Shift__c, Email__c, Next_Pickup_Counter__c,QC_Rep__c FROM Spec_Build__c WHERE Id =: idSpecBld];
	if(!SpecRepList.isEmpty()) 
	{
		cas.Spec_Build_Rep__c                 = SpecRepList[0].Email__c;
		SpecRepList[0].Next_Pickup_Counter__c = SpecRepList[0].Next_Pickup_Counter__c + 1;
		specBuildRepName = SpecRepList[0].Name;
		
	   
		//Arrange Map
		Spec_Build__c specBuild = new Spec_Build__c();
		specBuild.id            = SpecRepList[0].id;
		specBuild.Next_Pickup_Counter__c = SpecRepList[0].Next_Pickup_Counter__c;
		
		//Map
		mapNextCounter = new Map<Id,Spec_Build__c> ();
		mapNextCounter.put(specBuild.id,specBuild);
		
		//SpecRepList.add(specBuild);
		//update specBuild;
		//SpecRepList.add(SpecRepList[0]);
	} 
	else 
	{                    
		cas.Spec_Build_Rep__c             = Label.SpecBuild_Manager_Email;
		//specBuildRepName = Label.SpecBuild_Manager_Email;                                  
	}
}


I think you are looking for above code.....


If 
Id idSpecBld = lstAR[0].get('Id')(Id);
line doesn't work then change it to :
Id idSpecBld = Id.valueOf(lstAR[0].get('Id'));

and try again.