Solved

determine if account has child opportunities within specific date ranges

Posted on 2011-09-08
6
439 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 46
how to add new optional parameter to JSP 1 49
Custom Java Application connects to database: Which one of the 20? 4 35
collection output issue 9 37
I showed you how to use console view (HERE (http://www.experts-exchange.com/articles/18379/Getting-Started-and-Using-the-Salesforce-com-Console.html)) -– but how do you set it up on the admin side of Salesforce? Note that you have to have Admin leve…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

828 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