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 

Batch apex how to update discount in records

Hi there !
Write a batch apex to update the records in student custom sobject as if the student is male give the discount of 10% and if the student is female give the discount of 20%
Best Answer chosen by Hari nadh babu Eluru
CharuDuttCharuDutt
Hii Hari
Try Below Batch
public class DiscountProcessor implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT id,Gender,Amount,Discount FROM Student__c');
    }
    public void execute(Database.BatchableContext bc, List<Student__c> records){
        for(Student__c led : records){
           
            if(led.Gender == 'Male'){
                     led.Discount = led.Amount - (led.Amount * 0.1);

     			system.debug ('The Net Amount After Discount is for male ' + led.Discount);
            }else if(led.Gender == 'Female'){
                  led.Discount= led.Amount - (led.Amount * 0.2);

     			system.debug ('The Net Amount After Discount is for female ' + led.Discount);
            }
        }
        update records;
    }
    public void finish(Database.BatchableContext bc){
        Id job = bc.getJobId();
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Hari
Try Below Batch
public class DiscountProcessor implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT id,Gender,Amount,Discount FROM Student__c');
    }
    public void execute(Database.BatchableContext bc, List<Student__c> records){
        for(Student__c led : records){
           
            if(led.Gender == 'Male'){
                     led.Discount = led.Amount - (led.Amount * 0.1);

     			system.debug ('The Net Amount After Discount is for male ' + led.Discount);
            }else if(led.Gender == 'Female'){
                  led.Discount= led.Amount - (led.Amount * 0.2);

     			system.debug ('The Net Amount After Discount is for female ' + led.Discount);
            }
        }
        update records;
    }
    public void finish(Database.BatchableContext bc){
        Id job = bc.getJobId();
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
Hari nadh babu EluruHari nadh babu Eluru
@CharuDutt Thank you !