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
samnshsamnsh 

How to run batch class in system log.......Please help....

global class batch_emailreport implements Database.Batchable<sObject>, Database.Stateful
{
global String query;
global boolean istest;
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<sobject> scope)
{
for(sObject s : scope)
{
string htmlbdy;
Contact cont = (Contact) s;
system.debug(cont);
date myDate = date.today();
date weekStart = myDate.toStartofWeek();
date StartOfMonth = myDate.toStartOfMonth();

ritika@developerforceritika@developerforce

Hi,

 

To instantaneously run your batch class from System Log, just create an object of the class and run the batch -

 

batch_emailreport bat = new batch_emailreport();

Database.executeBatch(bat, 200);

 

The second parameter, scope, defines the batch size.

By default it is 200, not a mandatory parameter.

 

You can track the progress of yoyuur batch, logs get created for each bat run.

 

Hope this helps.


Regards,

Ritika

samnshsamnsh

thank you for your reply....

its showing an error

 

"first argument cannot be null."

 

Can u help...

ritika@developerforceritika@developerforce

Hi,

 

Make sure your object of your batch class is created correctly. Can you post the script you are using here.

Can you verify the class object (system.debug(bat)) ; ?

 

Thanks

Ritika

Navatar_DbSupNavatar_DbSup

Hi,


Try the below code as reference of calling Batch Class from System Debug:
global class OwnerReassignment implements Database.Batchable<sObject>
{
String query;
String email;
Id toUserId;
Id fromUserId;

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

global void execute(Database.BatchableContext BC, List<sObject> scope)
{
List<Account> accns = new List<Account>();

for(sObject s : scope)
{
Account a = (Account)s;
if(a.Ownerid==fromUserId)
{
a.Ownerid=toUserId;
accns.add(a);
}
}

update accns;
}
global void finish(Database.BatchableContext BC)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

mail.setToAddresses(new String[] {email});
mail.setReplyTo('batch@acme.com');
mail.setSenderDisplayName('Batch Processing');
mail.setSubject('Batch Process Completed');
mail.setPlainTextBody('Batch Process has completed');

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
Calling through System Debug Log
//user u=[select id,name from user where profile.name='system administrator'];
//system.debug('@@@@@@@@@' +u.name);
//user u2=[select id,name from user where profile.name='Standard User'];
//system.debug('########' +u2.name);
id st='00590000000eb7c';
OwnerReassignment reassign = new OwnerReassignment();
reassign.query='SELECT Id, Name, Ownerid FROM Account WHERE ownerid=\'' + st + '\'';
reassign.email='test@gmail.com';
reassign.fromUserId = '00590000000eb7c';
reassign.toUserId = '00590000000eb7w';
ID batchprocessid = Database.executeBatch(reassign);

ritika@developerforceritika@developerforce

Hi,

 

While I try to run this code, in the meanwhile, make you variables public (by default Apex Variables are Private). It seems the batch is not getting the correct value of variable 'query'.

 

Regards,

Ritika

 

 

ritika@developerforceritika@developerforce

Hi,

 

Is the issue resolved? I checked the code by making variables public and it worked fine.

 

Please mark the thread as solved if its done.

 

Regards,

Ritika