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
GYAN ANDRUSGYAN ANDRUS 

Hi I want to write the batch apex.I am updating the field using trigger and i have created the formula fields as well,

Now i want to apply this trigger and formula field values for existing records also,Can you pleas any one tell how to update this

I want to update the custom field - Finish_Code__c from PBSI__PBSI_Item__c object..please help me anyone
Best Answer chosen by GYAN ANDRUS
VIVEK 998VIVEK 998
You can write your apex code like :
global class UltParentName implements Database.Batchable<sObject>{        
        //string query;
        string query = 'SELECT Id, Finish_Code__c FROM PBSI__PBSI_Item__c ';

        
        global Database.QueryLocator start(Database.BatchableContext BC){
            return Database.getQueryLocator(query);
        }

        global void execute(Database.BatchableContext BC,List<PBSI__PBSI_Item__c> scope) {
             List<PBSI__PBSI_Item__c> pbs = new List<PBSI__PBSI_Item__c>();
             for(PBSI__PBSI_Item__c a : scope){
                 pbs.add(a);
             }                  
             update pbs;          
        }
        global void finish(Database.BatchableContext BC)
        {     //System.debug(LoggingLevel.WARN,'Batch Job Complete');
        }       
    }
And you can call this snipped code by excuting this annymous code.
Create above batch class.
Than go to developer console and in that go to execute anonymous window.
Write following code and run :
UltParentName getuprecs = new UltParentName(); 
Database.executeBatch(getuprecs);


Let me know if you fall in any problem.
Hope it helps!
Thanks

All Answers

VIVEK 998VIVEK 998
Hi,
Write a anonymous block of coe updating the records you would like this updations on. So as you update htose records simply you rtrigger will also get calles and your work of formula fields will also be done.
Mark as answer if fits what you wanted.
Thanks
GYAN ANDRUSGYAN ANDRUS
Can you please share the Code
VIVEK 998VIVEK 998

There are some governor limits that you should be aware of with both (e.g. only being able to retrieve maximum of 50,000 rows and only able to update a maximum of 10,000 etc.). If you exceed any of these limits, you may need to employ batch apex. However, if you are just talking about a few records you could do this:
List<PBSI__PBSI_Item__c> pbs = [ Select Id, Finish_Code__c  From PBSI__PBSI_Item__c Where CreateDate<Today() LIMIT 50000];
update pbs;
Edit: you could also use dataloader, i.e. extract the records, update the value to true and then update with data loader if you don't want to use Apex.
GYAN ANDRUSGYAN ANDRUS
I have 1lakh plus records,So i need to go with Batch APex,,but i dont know how to write batch,,Pleas ehelp
VIVEK 998VIVEK 998
You can write your apex code like :
global class UltParentName implements Database.Batchable<sObject>{        
        //string query;
        string query = 'SELECT Id, Finish_Code__c FROM PBSI__PBSI_Item__c ';

        
        global Database.QueryLocator start(Database.BatchableContext BC){
            return Database.getQueryLocator(query);
        }

        global void execute(Database.BatchableContext BC,List<PBSI__PBSI_Item__c> scope) {
             List<PBSI__PBSI_Item__c> pbs = new List<PBSI__PBSI_Item__c>();
             for(PBSI__PBSI_Item__c a : scope){
                 pbs.add(a);
             }                  
             update pbs;          
        }
        global void finish(Database.BatchableContext BC)
        {     //System.debug(LoggingLevel.WARN,'Batch Job Complete');
        }       
    }
And you can call this snipped code by excuting this annymous code.
Create above batch class.
Than go to developer console and in that go to execute anonymous window.
Write following code and run :
UltParentName getuprecs = new UltParentName(); 
Database.executeBatch(getuprecs);


Let me know if you fall in any problem.
Hope it helps!
Thanks
This was selected as the best answer
GYAN ANDRUSGYAN ANDRUS
UltParentName getuprecs = new UltParentName();
Database.executeBatch(getuprecs);


I have run this code in execute anonymous window.
User-added imageIs it correct?
VIVEK 998VIVEK 998
Yes and you must have also created the above class which I sent.
So it should be fine now.
So once this batch is suucesfully completed you can check the old records.
And mark it as the answer if you get resolved.
Thanks!