+ Start a Discussion
SFDC 18SFDC 18 

Unable to compile the code due to below errors

Hi,

I wrote a batch to update the fields, i got the below errors for my code

User-added image
Code
global class updatezone implements Database.Batchable<sObject>,Database.stateful{
    
    //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);
    }
    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;
    }
    public void finish(Database.BatchableContext bc){
         Id job= bc.getJobId();
         }
}

 
Best Answer chosen by SFDC 18
bhanu_prakashbhanu_prakash
Hi SFDC,
Mark as best answer, If it resloves !!
try to paste below code 
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);
    }
    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;
    }
    public void finish(Database.BatchableContext bc) {
        Id job = bc.getJobId();
    }
}


Mark as resloved if it helps :) :)
Thanks, 
Bhanu Prakash
visit ForceLearn.com   
​ 
 

All Answers

bhanu_prakashbhanu_prakash
Hi SFDC,
Mark as best answer, If it resloves !!
try to paste below code 
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);
    }
    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;
    }
    public void finish(Database.BatchableContext bc) {
        Id job = bc.getJobId();
    }
}


Mark as resloved if it helps :) :)
Thanks, 
Bhanu Prakash
visit ForceLearn.com   
​ 
 
This was selected as the best answer
SFDC 18SFDC 18
Hi , Thanks for the response. I got the below error on line 31 DML requires SObject or SObject list type: String