Solved

determine if account has child opportunities within specific date ranges

Posted on 2011-09-08
6
433 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-
  • 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Read about achieving the basic levels of HRIS security in the workplace.
Messaging apps are amazing tools with the power to do a lot of good, but the truth is the process of collaborating with coworkers requires relationships established through meaningful communication - the kind of communication that only happens face-…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now