Solved

determine if account has child opportunities within specific date ranges

Posted on 2011-09-08
6
443 Views
Last Modified: 2016-02-17
I've created a batch program that loops over all accounts and then loops over each accounts related child opportunities.  

As I'm looping over the child opportunities, I want to first see if the account has any closed won opportunities within the last year.  If I find a match then I want to set the account field Customer_Status__c to current.  

If I don't find a match, then I want to check to see if there are any closed won opportunities within the last 2 years (24 months).  If I find a match then I want to set the account field Customer_Status__c to prior active.  

If I don't find a match, then I want to check to see if there are any closed won opportunities that are beyond 2 years and set the account field Customer_Status__c to prior inactive.

I'm attempting to loop over the accounts and related child opportunity records  (see code below)

Something is not quite right with my logic because when I run the batch program it is setting the account field correctly for some accounts, but then other accounts do not get the Customer Status field set even though it has a closed won opportunity that is within a date range that should cause the value to get set?

Does anyone see a problem with the logic in my code that might cause that to happen?
global class AccountCustomerStatusBatchUpdate implements Database.Batchable<Sobject>, Database.AllowsCallouts {
	
   	Public String query =  'Select Id, Customer_Status__c, (Select StageName, CloseDate From Opportunities Where isWon = True ORDER BY CloseDate DESC) From Account';
   	
	global database.querylocator start(Database.BatchableContext BC)
	{
		return Database.getQueryLocator(query);
	}


	global void execute(Database.BatchableContext BC, Sobject[] scope)
	{
		boolean found = false;
		Date beforeOneYear = date.newInstance(Date.today().Year() - 1, Date.today().Month(), Date.today().Day());
		Date beforeTwoYear = date.newInstance(Date.today().Year() - 2, Date.today().Month(), Date.today().Day());
		List<Account> AcctsToUpdate = new List<Account>();	
		for (Account master : (List<Account>)scope) {
			for (Opportunity o : master.Opportunities) {	
				while(!found) {
    				if (o.CloseDate >= beforeOneYear) {
    					master.Customer_Status__c = 'Current';
    					AcctsToUpdate.add(master);
    					found = true;
    				}
    				else if (o.CloseDate < beforeOneYear && o.CloseDate >= beforeTwoYear) {
    					master.Customer_Status__c = 'Prior Active';
    					AcctsToUpdate.add(master);
    					found = true;
    				}
    				else if (o.CloseDate < beforeTwoYear) {
    					master.Customer_Status__c = 'Prior Inactive';
    					AcctsToUpdate.add(master);
    					found = true;
    				}
				}
				if (found) {
					break;
				}
			}
		}
		update AcctsToUpdate;
	}
	

	global void finish(Database.BatchableContext BC) {
		
	}
}

Open in new window

0
Comment
Question by:-Dman100-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36503023
That's not Java code - why have you posted in the Java TA?
0
 

Author Comment

by:-Dman100-
ID: 36503265
Yes, you are correct.  It is APEX code (Salesforce), but APEX is actually JAVA light.  It is compilted into JAVA.  So, since it is a derivative of JAVA I posted in this zone.  The interface is the significant difference, but the logic should be very similar.

Can you still help?

Thanks for responding to my post.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36503593
Oh - it looked like C# ;)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:-Dman100-
ID: 36503668
Definitely not C#.  The salesforce language APEX actually runs on a Java App Server and is compilted down into Java.  The fundamental difference from my understanding is APEX doesn't have the JAVA class libraries available.  I know there are other differences, probably more significant, but the language is definitely derived from Java ;)

Let me know if you are still able to help with the logic in my code?

Thanks again! :)
0
 

Accepted Solution

by:
-Dman100- earned 0 total points
ID: 36519005
I figured it out.  I needed to rreset the boolean variable found = false at the start of the first for loop.  Once found is equal to true in the while loop, it never got re-entered even when the next cycle of the for loop started for a new account record.


for (Account master : (List<Account>)scope) {
found = false

Open in new window

0
 

Author Closing Comment

by:-Dman100-
ID: 36547889
No other solution was provided.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question