Solved

determine if account has child opportunities within specific date ranges

Posted on 2011-09-08
6
436 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sites similar to codingbat to improve coding hanson skills 3 41
GUI builder for Eclipse? 8 27
servlet doXXX methods 3 39
Apps blocked by Java 9 64
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
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 tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

863 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

23 Experts available now in Live!

Get 1:1 Help Now