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
Hari nadh babu EluruHari nadh babu Eluru 

Getting Multiple times output

Hi there !
In this batch Apex code, I updated Mr. infront of the name if student is Male and Mrs. infront of the name if student is Female but at in output Mr. or Mrs. is getting multiple times. Please give me the suggestions to remove the multiple times of getting Mr. and Mrs. and get only once.
public class UpdateGender implements database.batchable<sobject> {
    
    public database.querylocator start(database.batchableContext abc){
        string a = 'SELECT Student_Id__c, Name, Gender__c FROM Student__c';
        return database.getquerylocator(a);
    }
    
    public void execute(database.batchableContext def, list<Student__c> x){
        for(Student__c a : x){
            if(a.Gender__c == 'Male'){
                a.Name = 'Mr.'+a.Name;
            }
            else if(a.Gender__c == 'Female'){
                a.Name = 'Mrs.'+a.Name;
            }
        }
        upsert x;
    }
    
    public void finish(database.batchableContext ghi){
        system.debug('Finish::!');
    }
}



The above image is multiple times getting Mr. and Mrs. in output 
Multiple times Mrs.
Multiple times Mr.
Best Answer chosen by Hari nadh babu Eluru
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hari,

Can you check if below code helps you.
 
public class UpdateGender implements database.batchable<sobject> {

        
        public database.querylocator start(database.batchableContext abc){
            string a = 'SELECT  Name, Gender__c FROM Student__c';
            return database.getquerylocator(a);
        }
        
        public void execute(database.batchableContext def, list<Student__c> x){
            for(Student__c a : x){
                if(a.Gender__c == 'Male' && !(a.name.startswith('Mr.'))){
                    a.Name = 'Mr.'+a.Name;
                }
                else if(a.Gender__c == 'Female' && !(a.name.startswith('Mrs.'))){
                    a.Name = 'Mrs.'+a.Name;
                }
            }
            update x;
        }
        
        public void finish(database.batchableContext ghi){
            system.debug('Finish::!');
        }
    }

If this solution helps you, please mark it as best answer.

Thanks,
​​​​​​​

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Are you running the batch multiple times. 

Thanks,
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hari,

Can you check if below code helps you.
 
public class UpdateGender implements database.batchable<sobject> {

        
        public database.querylocator start(database.batchableContext abc){
            string a = 'SELECT  Name, Gender__c FROM Student__c';
            return database.getquerylocator(a);
        }
        
        public void execute(database.batchableContext def, list<Student__c> x){
            for(Student__c a : x){
                if(a.Gender__c == 'Male' && !(a.name.startswith('Mr.'))){
                    a.Name = 'Mr.'+a.Name;
                }
                else if(a.Gender__c == 'Female' && !(a.name.startswith('Mrs.'))){
                    a.Name = 'Mrs.'+a.Name;
                }
            }
            update x;
        }
        
        public void finish(database.batchableContext ghi){
            system.debug('Finish::!');
        }
    }

If this solution helps you, please mark it as best answer.

Thanks,
​​​​​​​
This was selected as the best answer
Hari nadh babu EluruHari nadh babu Eluru
Yes, I'm running batch multiple times.
@Sai Praveen, Thank you very Much !