+ Start a Discussion
SFDC 18SFDC 18 

Update a field with count using apex classs

Hi, 
I need to update a field called No of Agents with the count of agents in that zone (North, South, East, West). The zone field is on agent object and no of agents is on zone object. I have written the below logic.

I'm stuck with the if clause part. Can anyone help me with this?

Name is a field on zone object with picklist values of zone

On No of Agents field,
public class NoofAgents {
    List<Zone> zonestoupdate = [SELECT Id,Zones__C FROM Agent__C];  
    for(Zone z:zonestoupdate)       
    {       
        Integer NoofAgents = [SELECT count() FROM Agent__C WHERE Zones__C == North];
        if (Name__c == North)
        {
            
        }
    }        
}

it should show the count of agents in that zone

 
Best Answer chosen by SFDC 18
Bala Gangadhar VadlamuriBala Gangadhar Vadlamuri
Sai,
 
public class NoofAgents {
    
    public NoofAgents(){
  		
       
        for(zone__c z : [SELECT Id,Name,NoofAgents__c FROM Zone__C]){
           
            z.NoofAgents__c = [SELECT count() FROM Agent__C WHERE Zone__C =: z.Name];
            upsert z;
        	}
      
            
        }
                         
 
}



This Code will update for all the zones.

Choose this as best answer if you like.

Regards,
Bala

All Answers

Bala Gangadhar VadlamuriBala Gangadhar Vadlamuri
Try this way
public class NoofAgents {
    public list<zone__c> zone1 = [SELECT Id,Zone__C,NoofAgents__c FROM zone__C];
    public NoofAgents(){
  		
        Integer NoofAgents = [SELECT count() FROM Agent__C WHERE Zone__C = 'North'];
        for(zone__c z: zonestoupdate){
            if(z.Zone__c=='North'){
            z.NoofAgents__c = NoofAgents;
        	}
        }
        upsert z;
 
}
}

Regards,
Bala​
 
SFDC 18SFDC 18
Thanks for the response

I got compile errors like variable doesn't exist when i have executed the code
SFDC 18SFDC 18
Also, I need to update the count on no of agents from the south, east as well. In that case, should I write another if condition for my code?
Bala Gangadhar VadlamuriBala Gangadhar Vadlamuri
public class NoofAgents {
    
    public NoofAgents(){
  		
        Integer NoofAgents = [SELECT count() FROM Agent__C WHERE Zone__C = 'North'];
        
        for(zone__c z : [SELECT Id,Name,NoofAgents__c FROM Zone__C  WHERE Zone__C = 'North']){
            if(z.Name == 'North'){
            z.NoofAgents__c = NoofAgents;
        	}
            
            upsert z;
        }
        
 
}
}


Try this for North. but for other regions will post you simplified code after some time. You can write if for south as well but let us try to automate.

Regards,
Bala 
SFDC 18SFDC 18
Thanks, Bala. This code is working

Regards,

Sai
Bala Gangadhar VadlamuriBala Gangadhar Vadlamuri
Sai,
 
public class NoofAgents {
    
    public NoofAgents(){
  		
       
        for(zone__c z : [SELECT Id,Name,NoofAgents__c FROM Zone__C]){
           
            z.NoofAgents__c = [SELECT count() FROM Agent__C WHERE Zone__C =: z.Name];
            upsert z;
        	}
      
            
        }
                         
 
}



This Code will update for all the zones.

Choose this as best answer if you like.

Regards,
Bala
This was selected as the best answer
SFDC 18SFDC 18
Ok. Thank You Regards, Sai
SFDC 18SFDC 18
How to avoid variable doesn't exist: z error?

I frequently face this kind of errors in my code


 
Bala Gangadhar VadlamuriBala Gangadhar Vadlamuri
Sai,
In the previoue error "variable doesn't exist: z error" because we declared a variable in for loop and upsert out side the loop. once i moved inside the its solved. 

when ever you use a variable it is declared with in the code block or if you use public the you can access any where in the code. 

For example if you give integer a; by defaule its private and you can not use it out side the code block {} of out side the method. 
SFDC 18SFDC 18
Ok got it. Thanks for the response Bala

Regards,

Poojitha