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
atharva Vispute 3atharva Vispute 3 

Help me to solve this problem in batch apex

Hi,

problem statement is
in currency if the amount is in  10,000-20,000 then picklist value must be cold, if amount is in 20,000-50,000 then picklist value must be hot and if the amount is more than 50,000 then value should be warm. So what kind of trigger i have to use here?? Plzz help me to find solution.

Thanks!!


 
Best Answer chosen by atharva Vispute 3
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Atharva,

Can you check the below batch class for the above question.
 
global class projectRatingBatch  implements Database.Batchable<sObject> { 
    
    
global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
         
        String query = 'SELECT Id, Amount__c,Rating__c FROM Project__c  where  Amount__c >10000';
        return Database.getQueryLocator(query);
    }
     
    global void execute(Database.BatchableContext BC, List<Project__c> projectList) {
        List<Project__c> projecttoupdate= new List<Project__c>();
        // process each batch of records default size is 200
        for(Project__c pr:projectList){
            
             if(pr.Amount__c>10000 && pr.Amount__c <=20000)
                    pr.Rating__c='cold';
                if(pr.Amount__c>20000 && pr.Amount__c <=50000)
                 pr.Rating__c='hot';
                if(pr.Amount__c>50000 )
                 pr.Rating__c='warm';
            projecttoupdate.add(pr);
        }
        try {
            // Update the project Record
            update projecttoupdate;
         
        } catch(Exception e) {
            System.debug(e);
        }
         
    }   
     
    global void finish(Database.BatchableContext BC) {
        // execute any post-processing operations like sending email
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,


 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Atharva,

Can you check the below batch class for the above question.
 
global class projectRatingBatch  implements Database.Batchable<sObject> { 
    
    
global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
         
        String query = 'SELECT Id, Amount__c,Rating__c FROM Project__c  where  Amount__c >10000';
        return Database.getQueryLocator(query);
    }
     
    global void execute(Database.BatchableContext BC, List<Project__c> projectList) {
        List<Project__c> projecttoupdate= new List<Project__c>();
        // process each batch of records default size is 200
        for(Project__c pr:projectList){
            
             if(pr.Amount__c>10000 && pr.Amount__c <=20000)
                    pr.Rating__c='cold';
                if(pr.Amount__c>20000 && pr.Amount__c <=50000)
                 pr.Rating__c='hot';
                if(pr.Amount__c>50000 )
                 pr.Rating__c='warm';
            projecttoupdate.add(pr);
        }
        try {
            // Update the project Record
            update projecttoupdate;
         
        } catch(Exception e) {
            System.debug(e);
        }
         
    }   
     
    global void finish(Database.BatchableContext BC) {
        // execute any post-processing operations like sending email
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,


 
This was selected as the best answer
atharva Vispute 3atharva Vispute 3
Hi Praveen,

There is no error in code but picklist value is not getting change.. Kindly help me to do this.
Thanks!!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Atharva,

Have you executed the batch class?

Thanks,
 
atharva Vispute 3atharva Vispute 3
Hi Praveen,

Sorry I am not getting you. How can I do this?

Thanks!!
Sai PraveenSai Praveen (Salesforce Developers) 
HI Atharva,

Trigger runs automaticllay when a record is created or updated based on our code. But Batch class we have to run manually or schedule to run at particuoar time.

To run batch class you can use the below code and run in anonomous window in developer console so the batch runs and updates all existing projects.
 
projectRatingBatch bc= new projectRatingBatch();
database.executeBatch(bc);

Thanks,
​​​​​​​
atharva Vispute 3atharva Vispute 3
Hi Praveen,

I will do it and then let you know.

Thanks!!
atharva Vispute 3atharva Vispute 3
Hi Praveen,

It's working now. But after 50,000 value is assgined to be warm but it shows me as a hot.. How is this possible??

Thanks!!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Atharva,

What is the Amount value and did you checked it after refresh? 

I dont see any issue with the batch. Can you update just that one record again and execute the batch and try.

Thanks,


 
atharva Vispute 3atharva Vispute 3
Hi Praveen,

Ok Praveen I will do it and then let you know.

Thanks!!
atharva Vispute 3atharva Vispute 3
Hi Praveen,

Its working fine now.

Thanks!!