0

For the below code I got this error on line 31 i.e upsert z;

global class updatezone implements Database.Batchable < sObject > , Database.stateful {

  string query;
  string z;

    //Start Method....   
    global Database.querylocator start(Database.BatchableContext bc) {
        query = 'SELECT id,No_of_Agents__c,No_of_Female_Agents__c,No_of_Male_Agents__c,Agents_40_years_and_above__c,Agents_below_40_years__c,X1_star_Agents__c,X2_star_Agents__c,X3_star_Agents__c,X4_star_Agents__c,X5_star_Agents__c' +
            ' FROM Zone__C ';
        return Database.getQueryLocator(query);
    }
    //Execute Method....
    global void execute(Database.BatchableContext bc, List < Zone__C > scope) {

        for (zone__c z: [SELECT Id, Name__C, No_of_Agents__c, No_of_Female_Agents__c, No_of_Male_Agents__c, Agents_40_years_and_above__c, Agents_below_40_years__c, X1_star_Agents__c, X2_star_Agents__c, X3_star_Agents__c, X4_star_Agents__c, X5_star_Agents__c FROM
                Zone__C ]) 
 {
            z.No_of_Agents__C = [SELECT count() FROM Agent__C WHERE Zones__C =: z.Name__C];
            z.Agents_below_40_years__C = [SELECT count() FROM Agent__C WHERE Age__C <= 40 AND Zones__C =: z.Name__C];
            z.Agents_40_years_and_above__c = [SELECT count() FROM Agent__C WHERE Age__C > 40 AND Zones__C =: z.Name__C];
            z.No_of_Male_Agents__C = [SELECT count() FROM Agent__C WHERE Gender__C =: 'Male' AND Zones__C =: z.Name__C ]; 
            z.No_of_Female_Agents__C = [SELECT count() FROM Agent__C WHERE Gender__C =: 'Female'AND Zones__C =: z.Name__C];               
            z.X1_star_Agents__c = [SELECT count() FROM Agent__C WHERE Ranking_Stars__C = 1 AND Zones__C =: z.Name__C];
            z.X2_star_Agents__c = [SELECT count() FROM Agent__C WHERE Ranking_Stars__C = 2 AND Zones__C =: z.Name__C];
            z.X3_star_Agents__c = [SELECT count() FROM Agent__C WHERE Ranking_Stars__C = 3 AND Zones__C =: z.Name__C];
            z.X4_star_Agents__c = [SELECT count() FROM Agent__C WHERE Ranking_Stars__C = 4 AND Zones__C =: z.Name__C];
            z.X5_star_Agents__c = [SELECT count() FROM Agent__C WHERE Ranking_Stars__C = 5 AND Zones__C =: z.Name__C];
        }

            upsert z;

    }
//Finish Method....
    public void finish(Database.BatchableContext bc) {
        Id job = bc.getJobId();
    }
}

1 Answer 1

3

You have two z variables; one inside a for loop, the other inside the class. Inside the for loop, z is a Zone__c, but outside of the loop, z is a String (line 4). Were you to remove that line, you'd get an error about z not existing. Aside from the "query-inside-a-loop" problem, you'll need to change your code so that you have a list to store the records you're processing:

Zone__c[] updates = new Zone__c[0];
for(Zone__c z: ...) {
  ...
  updates.add(z);
}
update updates;

You should be using update here, not upsert, because you're definitely modifying existing records.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .