• hometeam
  • NEWBIE
  • 0 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 8
    Questions
  • 6
    Replies

I know this is one of the most popular topics on the board and I've spent a day reading and trying several of the posted suggestions but I'm not finding a workable solution (or one that I understand) to help me.

 

What I need to do is traverse through all open opportunities and based on some criteria on the account either mark the opportunity as closed/won or closed/lost.  So I will have a large query.  I could in theory split the query but then I'm doing multiple loops and from what I read that's not best practises. 

 

This is my class and what I'm needing to understand is what is the correct way I handle the large number of rows returned from the query when I have to update each row. 

public class clsCloseOppty { 
	
	public void CloseOppty() {
		List<Opportunity> OpptyToUpdate = new List<Opportunity>();

		// Set This Years Close date to use in the record query
		Date dCloseDate = date.newinstance(date.today().year(),04,01);

		// Query for this years Opportunities along with the Account Type 
		List<Account> ClsOpptys = [Select a.name, a.Type, (Select o.Id, o.Name from Opportunities o where o.closedate = :dCloseDate) From Account a];
		
		// Loop through ClsAccts
		for (Account a : ClsOpptys) {
			// Set value for Stage
			string strStage = 'Closed/Lost';	
			if (a.Type == 'Existing Agency') {
				strStage = 'Closed/Won';
			}
			
			// Get the oppty Id
			for (Opportunity opp : a.opportunities){	
				ID id = opp.Id;
						
				Opportunity O = New Opportunity(Id=Id, StageName=strStage);
				OpptyToUpdate.add(O);
			}		
		}
		update OpptyToUpdate;
	}
}

 

This is my query:

 

I'm getting this error on line one 101....  co.CreateOppty()

 

I'm fairly new to APEX and I don't understand how to fix or where to begin with this.  In the Sandbox all the code test perfectly but I can't get it to run in production (another issue I don't understand). 

 

This is my test class:

@isTest
private class clsOpptyTest {

    static testMethod void TestCode() {
		Date dTodayDate = date.today();
		Date dNYCloseDate = date.newinstance(dTodayDate.Year()+1, 4, 1);
		Date dCYCloseDate = date.newinstance(dTodayDate.Year(), 4, 1);
		
		
		// Get an Admin User Id
		Id AdminId = [Select Id From User where firstname = 'Joe' and lastname ='Young'].Id;
		for (Profile Prof : [Select p.Name, (Select u.Id From Users u where u.IsActive = TRUE) 
					From Profile p where p.Name = 'System Administrator']) {
			for (User users : Prof.users){
				AdminId = users.Id;
				break;
			}
		}
		
		
		// Get the Account Agency RecordType ID	
		Id strAgencyID = [Select Id From RecordType where sObjectType='Account' and Name='Agency' and isActive=true].Id;


		// Create test Accounts		
		Account a0 = new Account(Name='Test0', Type='Prospective Agency',  
					OwnerID=AdminId, RecordTypeId=strAgencyId,
					Prospect_Stage__c='BIF Signed', Sub_Type__c='General Agency', 
					Total_Premium__c=4500000);
					
		Account a1 = new Account(Name='Test1', Type='Existing Agency', 
					OwnerID=AdminId, RecordTypeId=strAgencyId,
					Prospect_Stage__c='Signed', Sub_Type__c='General Agency',
					Total_Premium__c=4000000);
		
		Account a2 = new Account(Name='Test2', Type='Existing Agency', 
					OwnerID=AdminId, RecordTypeId=strAgencyId,
					Prospect_Stage__c='Signed', Sub_Type__c='General Agency', 
					Total_Premium__c=3500000);
		
		Account a3 = new Account(Name='Test3', Type='Existing Agency', 
					OwnerID=AdminId, RecordTypeId=strAgencyId,
					Prospect_Stage__c='Signed', Sub_Type__c='General Agency', 
					Total_Premium__c=3000000);
		
		Account[] Acct = new Account[] {a0, a1, a2, a3};
		insert Acct;


		// Create test Opportunities
	    Opportunity o0 = new Opportunity(AccountID=a0.Id, OwnerId='00560000001VRy9', 
					Name=a0.Name + ' - Test', StageName='Open', Type='Existing Business',
					Total_Premium__c=2000000, Committed_Premium__c=500000, 
					CloseDate=dNYCloseDate);

        Opportunity o1 = new Opportunity(AccountID=a1.Id, OwnerId='00560000001VRy9',  
					Name=a1.Name + ' - Test', StageName='Open', Type='Existing Business',
					Committed_Premium__c=1500000, Actual_Premium__c=0, Total_Premium__c=4500000,
					CloseDate=dNYCloseDate);
		
        Opportunity o2 = new Opportunity(AccountID=a2.Id, OwnerId='00560000001VRy9',  
					Name=a2.Name + ' - Test', StageName='Open', Type='Existing Business',
					Committed_Premium__c=1500000, Actual_Premium__c=1550000, Total_Premium__c=4500000,
					CloseDate=dCYCloseDate);

        Opportunity o3 = new Opportunity(AccountID=a3.Id, OwnerId='00560000001VRy9',  
					Name=a3.Name + ' - Test', StageName='Open', Type='Existing Business',
					Committed_Premium__c=1500000, Actual_Premium__c=1650000, Total_Premium__c=4500000,
					CloseDate=dCYCloseDate);

		Opportunity[] Oppty = new Opportunity[] {o0, o1, o2, o3};
		insert Oppty;
		
		
		// Validate and Test trgOpptyAmt 
		// The results of Amount = Amount should equal Total Premium if Actual Premium isn't greater than 0 
		Double AmtTest1 = [select Amount from Opportunity where id = :o0.Id].Amount;
		system.assertEquals(AmtTest1, 500000);

		Opportunity ou0 = new Opportunity(Id=o0.Id);
		ou0.Actual_Premium__c=800000;
		update ou0;

		Double AmtTest2 = [select Amount from Opportunity where id = :o0.Id].Amount;
		system.assertEquals(AmtTest2, 800000);
		
		
		Double AmtTest3 = [select Amount from Opportunity where id = :o1.Id].Amount;
		system.assertEquals(AmtTest3, 1500000);

		Opportunity ou1 = new Opportunity(Id=o1.Id);
		ou1.Committed_Premium__c=1800000;
		update ou1;

		Double AmtTest4 = [select Amount from Opportunity where id = :o1.Id].Amount;
		system.assertEquals(AmtTest4, 1800000);

		
		// Validate and Test clsCreateOppty
		clsCreateOppty CO = new clsCreateOppty();
		CO.CreateOppty();
		
		
		// Validate and Test clsCloseOppty
		String strType1 = [select Type from Account where id = :a2.Id].Type;
		system.assertEquals(strType1, 'Existing Agency');
		
		Account au1 = new Account(Id=a2.Id);
		au1.Type='Terminated';
		update au1;
		
		String strType2 = [select Type from Account where id = :a2.Id].Type;
		system.assertEquals(strType2, 'Terminated');

		clsCloseOppty ClsO = new clsCloseOppty();
		ClsO.CloseOppty();

		String StageTest1 = [select StageName from Opportunity where id = :o2.Id].StageName;
		system.assertEquals(StageTest1, 'Closed/Lost');

		String StageTest2 = [select StageName from Opportunity where id = :o3.Id].StageName;
		system.assertEquals(StageTest2, 'Closed/Won');
    }
}

 

 

 

This is the clsCreateOppty that has the method it's not finding;

public class clsCreateOppty {

	public void CreateOppty(){ 
		List<Opportunity> OpptyToCreate = new List<Opportunity>();

		// Set This Years Close date to use in the record query
		Date dQueryDate = date.newinstance(date.today().year(),04,01);
		
		// Set Close date to April 1 of next year
		Date dCloseDate = date.newinstance(date.today().year()+1,04,01);

		// Get the string value of the Close date year
		String strCloseDate = string.valueOf(dCloseDate);
		String[] StringDate = strCloseDate.split('-');
		String strCloseYear = StringDate[0];
		system.debug(strCloseYear);
		
		for (Account A : [Select a.Id, a.Name, a.OwnerId, a.Total_Premium__c, (Select o.CreatedDate, o.Committed_Premium__c 
							From Opportunities o where o.CloseDate = :dQueryDate order by o.CreatedDate desc) 
							From Account a where a.type in ('Prospective Agency', 'Existing Agency')]) {
			
			// Set default value for Committed Premium
			double CP = 0;
			// Use the most current opportunity which there should only be one but just in case break out of the loop after the first pass
			for (Opportunity opp : a.opportunities){	
				CP = opp.Committed_Premium__c;
				break;
			}
					
			Opportunity O = New Opportunity(Accountid=a.Id, CloseDate=dCloseDate, Name=a.Name + ' - ' + strCloseYear,
						   OwnerId='00560000001VRy9', StageName='Open', Type='Existing Business',
						   Committed_Premium__c=CP, Last_Year_s_Commitment__c=CP,
						   Total_Premium__c=a.Total_Premium__c);
			OpptyToCreate.add(O);
		}
		insert OpptyToCreate;
	}
}

 

What I'm trying to do is simply identify one of the system Admin Ids for my test class.  My code is below but I'm getting the above error on AdminID = users.Id;

 

  for (Profile Prof : [Select p.Name, (Select u.Id From Users u where u.IsActive = TRUE)
     From Profile p where p.Name = 'System Administrator']) {
   for (Id users : Prof.users){
    AdminId = users.Id;
    break;
   }
  }

Any help/suggestions would be appreciated.

I'm trying to figure out how to set a variable based on a child field.  My SOQL is part of a for loop and I need to reference the CloseDate and Committed_Premium__c fields but I don't know how.  I've searched and read all morning and can't figure this out.  Please help.

 

 

for (Account A : [Select a.Id, a.Name, a.OwnerId, a.Total_Premium__c, (Select o.CloseDate, o.Committed_Premium__c From Opportunities o where o.CloseDate > 2010-03-01) From Account a where a.type in ('Prospective Agency', 'Existing Agency')]) {

I have a situation where I have one opportunity per year per account.  On the current year's opportunity record is a field for Last Year's Amount.  I'd like to populate the field based on last year's opportunity record. 

 

Can anyone give me some direction on a simple method to do this?

We use Mass Emails to stay in touch with our contacts.  When we do this we want their replies to automatically log as an activity within SF. 

 

On the Outlook connector we do NOT automatically sync emails as it would create numerous unnecessary records in SF.  Is there a method to tracking only emails sent from SF.  I dont need a case to be created.  I simply want the email trail.

I have what should be a simple question but for the life of me I can't figure it out.  (Yes, I'm a newbee to Apex) :)

 

I have a test class that I'm working on and I need to set the date field to a specific date.  Ideally, I'd like to set the date value to July 15 of the current year.  How do I do this?  Below is the code, that doesn't work, and the value I'm trying to set is Exp_Date__c (a date field)

 

 

Account a1 = new Account(Name='Test1', Status__c='Active', 'VCM', RecordTypeId='01280000000G1LH','00580000003DryK', Dues__c=100, Exp_Date__c=2010-07-15);

I've inherited some APEX code and having trouble with the test class.  Being new to this I'm trying to understand if I can step through the code?  I'm using the Eclipse Force.com IDE.  I can run the tests and get results but I want to step through the code and see the actual value of the variables as the code is running.  My issue is what I think should be happening isn't but I don't know how to confirm it.  Please help.

I know this is one of the most popular topics on the board and I've spent a day reading and trying several of the posted suggestions but I'm not finding a workable solution (or one that I understand) to help me.

 

What I need to do is traverse through all open opportunities and based on some criteria on the account either mark the opportunity as closed/won or closed/lost.  So I will have a large query.  I could in theory split the query but then I'm doing multiple loops and from what I read that's not best practises. 

 

This is my class and what I'm needing to understand is what is the correct way I handle the large number of rows returned from the query when I have to update each row. 

public class clsCloseOppty { 
	
	public void CloseOppty() {
		List<Opportunity> OpptyToUpdate = new List<Opportunity>();

		// Set This Years Close date to use in the record query
		Date dCloseDate = date.newinstance(date.today().year(),04,01);

		// Query for this years Opportunities along with the Account Type 
		List<Account> ClsOpptys = [Select a.name, a.Type, (Select o.Id, o.Name from Opportunities o where o.closedate = :dCloseDate) From Account a];
		
		// Loop through ClsAccts
		for (Account a : ClsOpptys) {
			// Set value for Stage
			string strStage = 'Closed/Lost';	
			if (a.Type == 'Existing Agency') {
				strStage = 'Closed/Won';
			}
			
			// Get the oppty Id
			for (Opportunity opp : a.opportunities){	
				ID id = opp.Id;
						
				Opportunity O = New Opportunity(Id=Id, StageName=strStage);
				OpptyToUpdate.add(O);
			}		
		}
		update OpptyToUpdate;
	}
}

 

This is my query:

 

What I'm trying to do is simply identify one of the system Admin Ids for my test class.  My code is below but I'm getting the above error on AdminID = users.Id;

 

  for (Profile Prof : [Select p.Name, (Select u.Id From Users u where u.IsActive = TRUE)
     From Profile p where p.Name = 'System Administrator']) {
   for (Id users : Prof.users){
    AdminId = users.Id;
    break;
   }
  }

Any help/suggestions would be appreciated.

I'm trying to figure out how to set a variable based on a child field.  My SOQL is part of a for loop and I need to reference the CloseDate and Committed_Premium__c fields but I don't know how.  I've searched and read all morning and can't figure this out.  Please help.

 

 

for (Account A : [Select a.Id, a.Name, a.OwnerId, a.Total_Premium__c, (Select o.CloseDate, o.Committed_Premium__c From Opportunities o where o.CloseDate > 2010-03-01) From Account a where a.type in ('Prospective Agency', 'Existing Agency')]) {

I have what should be a simple question but for the life of me I can't figure it out.  (Yes, I'm a newbee to Apex) :)

 

I have a test class that I'm working on and I need to set the date field to a specific date.  Ideally, I'd like to set the date value to July 15 of the current year.  How do I do this?  Below is the code, that doesn't work, and the value I'm trying to set is Exp_Date__c (a date field)

 

 

Account a1 = new Account(Name='Test1', Status__c='Active', 'VCM', RecordTypeId='01280000000G1LH','00580000003DryK', Dues__c=100, Exp_Date__c=2010-07-15);

I've inherited some APEX code and having trouble with the test class.  Being new to this I'm trying to understand if I can step through the code?  I'm using the Eclipse Force.com IDE.  I can run the tests and get results but I want to step through the code and see the actual value of the variables as the code is running.  My issue is what I think should be happening isn't but I don't know how to confirm it.  Please help.