Solved

determine if account has child opportunities within specific date ranges

Posted on 2011-09-08
6
438 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 54
factorial example 4 39
Tomcat: Unable to run tomcat service. 2 18
jboss wildfly 10.1 10 81
Note: You must have administrative privileges in order to create/edit Sharing Rules. Salesforce.com (http://www.salesforce.com) (SFDC) is a cloud-based customer relationship management (CRM) system. It is a database most commonly used by sales an…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

785 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