In this batch apex class i want to apply discount to the students. If student is male then we can apply discount 10%. If student is female then we can apply discount 20%

The above is the code that i tried

public class discount_bb implements database.batchable<sobject>{
    public database.querylocator start(database.batchableContext bc){
        string s = 'SELECT Student_Id__c, Name, Gender__c, Fee_paid__c, Discount__c FROM Student__c';
        return database.getquerylocator(s);
    public void execute(database.batchableContext de, list<Student__c> a){
        for(Student__c x:a ){
            if(x.Gender__c == 'Male'){
                x.Discount__c = '10%';
            else if(x.Gender__c == 'Female'){
                x.Discount__c = '20%';
        update a;
    public void finish(database.batchableContext fg){

The fields that i have in custom sobject name student__c is

  • Age__c
  • Courses__c
  • Discount__c
  • Fee_paid__c
  • Gender__c
  • Joining_Date__c
  • Mobile_Number__c
  • Nationality__c
  • Status__c
  • Student_Id__c
  • Today_Date__c

I didn't know what to put in the code. I gave the discount text field 10% and 20% discount, Tell me how i can do with the formula to calculate discount

  • 1
    Hi, and welcome to SFSE! How exactly can we help you? Why don't you just use formula fields? What's the actual requirement here? If you haven't yet, please take a moment to read about How to Ask and take our quick tour. After that, edit your question to clarify what you're trying to achieve, specifically, so we can help you.
    – sfdcfox
    Commented Dec 26, 2021 at 3:47
  • I want to use formula field. Please explain how to calculate discount in formula field Commented Dec 26, 2021 at 3:51
  • If you're new to pretty much any part or feature of Salesforce, the best place to start learning about it is going to be trailhead.salesforce.com
    – Derek F
    Commented Dec 26, 2021 at 13:32

1 Answer 1


There are multiple ways to do it

  1. Use Formula Field
  2. Use workflow (basis your business requirement whether on insert or insert and update)

If you choose to do it with formula field, create a formula field with formula return type as Percent Formula Return Type and click next Click on Advanced Tab on next screen Advanced Formula and enter this CASE(Gender__c,'MALE',10,20)

Save the field

If you choose to do it with Workflow create a Filed on Object of type percent and then create a workflow with evaluation criteria as Created and Every time it's edited

In the Rule Criteria put ISCHANGED(Gender__C)

Add a Workflow Action of type Field Update

And in Workflow Action select Use a Formula to set a new value and in fomula type CASE(Gender__c,'MALE',10,20)

  • Thank you very much Sir Commented Dec 27, 2021 at 9:39

You must log in to answer this question.

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