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
alok29novalok29nov 

Batch Apex..urgent help needed

Hi All, I have written a batch class and am not getting the expected results. I have tried everything possible and could not find out where I am wrong with my logic. Let me explain the issue in detail and with an example. I have a sales executive John who has total 412 opportunities and number of won opportunities for him are 256. When i run the batch, What I get is three set of won opportunities i.e. 197, 18,41 total of these are exactly 256.

 

What i think is that the results i am getting this because the data is being passed into three batches and a batch gets processed and its gets updated then the next one and then the third one. But what is expected is that number of won opportunities field should be updated with 256 not with three different values i.e. all the opportunities get processed for one opportunity owner and then updated not for 1 batch at a time.

 

I would like to mention here that I am getting the expected results for the opportunities owners who have less 200 opportunities. Please suggest me what change I need to make so that I can get the expected result and what the mistake is.

 

Best Answer chosen by Admin (Salesforce Developers) 
alok29novalok29nov

I found the solution!! I passed one more parameter  in  the execute.batch method of the scheduler class of my batch apex and it resolved the issue.

Relieved now :).

 

 

All Answers

yvk431yvk431

I am not very clear with your functional req.

 

I wonder the approach chosen by you is correct or not. Because we need to intilize the counters to 0 for the first time and need to accumulate from Later on. Using batch its difficult to consolidate a single owner records in a single batch so a single owner's Opportunities might get appear accross different batches and we may run out of options when to initialize and when to accumulate the counts. Hope you got my point .

 

First i thought future method will be correct for this, we have limitation around in using future calls. So my post wont be of much help. Lets wait for an experts suggestion.

 

 

--yvk

Hpandey_ForceLabsHpandey_ForceLabs

Well, again not very clear with the requirements, while navigating throug the code, i leared there is more of for loop, inside another for loop - keeping the requirement apart - the efficiency of loop could be another significant factor, as complexity is raising to N*N (N2).

 

 

Can you please elaborate the problem, one more time in real clear words, easy for us to help you out here 

 

 

alok29novalok29nov

Hi yvk431,

 

I am having exactly the same issue.. how to accumulate the  number of won opportunites for a single owner's as you pointed out that oportunities may appear across different batches and that is what happening.

 

As far as the reuirement is concerned, I want to count the number of won and active opportunities at different stages and amonut at that stage.(I have different  stages for an opportunity.. Stage A, Stage G etc.)

 

Why I chose batch because I have  to update opportunites once/twice  in a week and generate a report . So I thought batch is the best possible approach.

 

Please let me know if you think I should use some other approach. but batch suits my requirement best.

 

Thanks

~Alok

 

 

alok29novalok29nov

@ Hpandey_Forcelabs..

 

Ok, Let me explain the requirement once again.

 

I want to calculate the number of won/active  opportunities and amount  for different owners at deffrenet stages. (An opportunity has six stages in my case.. like Stage A, Stage G ect).

I hope now you undersatnd my requirement. I want to do it once in a week not on a daily basis and that's why i chose to write a batch class.

 

And as far as 'for loop' is concerend, If I have to calculate number of won opportunities for different owner, I don't thin kthere is any other way to do it. and anyway my not much concerned about the complexity factor :). and let me tell you I have around 4000 opportunities in all and a bacth can process upto 50 million records.

 

thanks for your help!

 

~Alok

alok29novalok29nov

I found the solution!! I passed one more parameter  in  the execute.batch method of the scheduler class of my batch apex and it resolved the issue.

Relieved now :).

 

 

This was selected as the best answer
yvk431yvk431

Please share your solution so that it will be useful to others.

 

 

--yvk