-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
4Questions
-
7Replies
Apex and Test Class don't behave same as Anonymous Window
Hi,
I am a newbie to development and I am trying to create an Apex class that recreates activity of anonymous window code.
Intent is to delete contents of Account_Manager_Budget__c for the current month and repopulate it with data from the Account_Budget__c for the current month.
The aggregate result returns 9 records in Anonymous window but zero records when testing the Apex Class.
Test Class:
System.assertEquals shows recountCount = 0 but I know there are Account Budget records for 9 Account Managers that should be aggregated (and are in Anonymous window).
I would appreciate your expert eyes on this.
I am a newbie to development and I am trying to create an Apex class that recreates activity of anonymous window code.
Intent is to delete contents of Account_Manager_Budget__c for the current month and repopulate it with data from the Account_Budget__c for the current month.
public class PopulateAccountManagerBudget { @InvocableMethod public static void PopulatingBudgets(){ //set current month Integer BudgetYear = System.today().year(); Integer BudgetMonth = System.today().month(); system.debug(BudgetMonth + '/' + BudgetYear); //Delete all Account Manager Budget records for current month (including forecasts) List<Account_Manager_Budget__c> AccMgrBudget = [SELECT Id FROM Account_Manager_Budget__c WHERE Year__c = :BudgetYear AND Month__c = :BudgetMonth]; system.debug(AccMgrBudget.size() + ' records deleted.'); delete AccMgrBudget; //Retrieve all current Month Account budgets and sum to Account Manager level AggregateResult[] groupedBudget = [SELECT Account_Manager__c, SUM(Budget__c)Budget FROM Account_Budget__c WHERE Year__c = :BudgetYear AND Month__c = :BudgetMonth GROUP BY Account_Manager__c]; system.debug('groupedBudget = ' + groupedBudget.size() + ' results'); //Set up List to insert budget into Account Manager Budget object List<Account_Manager_Budget__c> NewAccMgrBudget = new List<Account_Manager_Budget__c>(); String AM_Id = ''; String AM_Budget = ''; //Loop through budgets and assign to List for(AggregateResult ar:groupedBudget){ AM_Id = String.valueof(ar.get('Account_Manager__c')); AM_Budget = string.valueof(ar.get('Budget')); System.debug('Account Manager = ' + String.valueof(ar.get('Account_Manager__c')) + ', BudgetYear = ' + BudgetYear + ', BudgetMonth = ' + BudgetMonth + ', Budget = ' + string.valueof(ar.get('Budget'))); // Account_Manager_Budget__c amb = new Account_Manager_Budget__c(Account_Manager__c=String.valueof(ar.get('Account_Manager__c')), Year__c=BudgetYear, Month__c=BudgetMonth, Budget__c=decimal.valueof(string.valueof(ar.get('Budget')))); Account_Manager_Budget__c amb = new Account_Manager_Budget__c(Account_Manager__c=AM_Id, Year__c=BudgetYear, Month__c=BudgetMonth, Budget__c=decimal.valueof(AM_Budget)); NewAccMgrBudget.add(amb); } //insert list into Account Manager Budget object for current month, with Forecast = 0 if(NewAccMgrBudget.size()>0){ insert NewAccMgrBudget; } } }
The aggregate result returns 9 records in Anonymous window but zero records when testing the Apex Class.
Test Class:
@isTest private class PopulateAccountManagerBudget_test{ @isTest static void MyTestClass(){ PopulateAccountManagerBudget.PopulatingBudgets(); //system.debug('RecourdCount=' + recordCount); Integer recordCount = 0; Integer BudgetYear = System.today().year(); Integer BudgetMonth = System.today().month(); system.debug(BudgetMonth + '/' + BudgetYear); // AggregateResult[] countRecords = [SELECT COUNT(Id)recordCount FROM Account_Manager_Budget__c WHERE Budget_Date__c = :dateBudgetDate]; AggregateResult[] groupedBudget = [SELECT Account_Manager__c, SUM(Budget__c)Budget FROM Account_Budget__c WHERE Year__c = :BudgetYear AND Month__c = :BudgetMonth GROUP BY Account_Manager__c]; for(AggregateResult ar:groupedBudget){ recordCount = recordCount+1; } system.assertEquals(9, recordCount); } }
System.assertEquals shows recountCount = 0 but I know there are Account Budget records for 9 Account Managers that should be aggregated (and are in Anonymous window).
I would appreciate your expert eyes on this.
- Michael Clarke 36
- May 07, 2021
- Like
- 0
- Continue reading or reply
How do I test vf page and controller extension
Hi, I have built my first vf page and controller extension and it works fine in Sandbox.
How do I create a Test class for this?
Visualforce Page:
Controller Extension Class:
How do I create a Test class for this?
Visualforce Page:
<apex:page standardController="Case" extensions="Case_ListOppSplits_Controller" lightningStylesheets="true"> <apex:pageBlock> <apex:pageBlockTable value="{!Opportunity_Splits}" var="oppSplit"> <apex:column value="{!oppSplit.Name}"/> <apex:column value="{!oppSplit.Split_Loan_Amount__c}"/> <apex:column value="{!oppSplit.Rate_Type__c}"/> <apex:column value="{!oppSplit.Repayment_Type__c}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:page>
Controller Extension Class:
public class Case_ListOppSplits_Controller { public Case myCase; public Case_ListOppSplits_Controller(ApexPages.StandardController stdController){ this.myCase = (Case)stdController.getRecord(); } //initialise setController and return a list of records public list<Opportunity_Split__c> getOpportunity_Splits(){ Case currentCase = [SELECT Id, Subject, Opportunity__c FROM Case WHERE Id =: ApexPages.currentPage().getParameters().get('id')]; List<Opportunity_Split__c> OppSplit = [SELECT Id, Name, Opportunity__c, Loan_Purpose__c, Loan_Type__c, Loan_Usage__c, Rate_Type__c, Repayment_Type__c, Split_Loan_Amount__c FROM Opportunity_Split__c WHERE Opportunity__c =: currentCase.Opportunity__c]; return OppSplit; } }
- Michael Clarke 36
- August 14, 2020
- Like
- 0
- Continue reading or reply
My first controller extension
Hi, I am learning skills as a developer and I have a use case for what I have learnt.
The Case object has parent Opportunity, and the Opportunity object has children Opportunity_Split__c.
I would like to show Opportunity Splits on the Case page using the Lightning App Builder.
My understanding is that for a visualforce page to appear on a Case Page it needs to utilise the Case standard controller. Therefore I need to extend the standard controller with an extension.
Visualforce code is:
Am I anywhere near a solution or is my understanding way off the mark?
Your help in solving this, and expanding my knowledge, would be very much appreciated.
The Case object has parent Opportunity, and the Opportunity object has children Opportunity_Split__c.
I would like to show Opportunity Splits on the Case page using the Lightning App Builder.
My understanding is that for a visualforce page to appear on a Case Page it needs to utilise the Case standard controller. Therefore I need to extend the standard controller with an extension.
Visualforce code is:
<apex:page standardController="Case" extensions="Case_ListOppSplits_Controller" lightningStylesheets="true"> <apex:pageBlock title="Opportunity Splits"> <apex:pageBlockTable value="{!Opportunity_Splits}" var="oppSplit"> <apex:column value="{!oppSplit.Name}"/> <apex:column value="{!oppSplit.Split_Loan_Amount__c}"/> <apex:column value="{!oppSplit.Rate_Type__c}"/> <apex:column value="{!oppSplit.Repayment_Type__c}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:page>Case Controller extension is:
public class Case_ListOppSplits_Controller { public ApexPages.StandardSetController setController{ get{ if(setController==NULL){ Case currentCase = [SELECT Id, Subject, Opportunity__c FROM Case WHERE Id =: ApexPages.currentPage().getParameters().get('id')]; setController = new ApexPages.StandardSetController(Database.getQueryLocator( [SELECT Id, Name, Opportunity__c, Loan_Purpose__c, Loan_Type__c, Loan_Usage__c, Rate_Type__c, Repayment_Type__c, Split_Loan_Amount__c FROM Opportunity_Split__c WHERE Opportunity__c =: currentCase.Opportunity__c])); } return setController; } set; } //initialise setController and return a list of records public list<Opportunity_Split__c> getOpportunity_Splits(){ return (List<Opportunity_Split__c>) setController.getRecords(); } }The visualforce page cannot be saved as it throws this error: Unknown constructor 'Case_ListOppSplits_Controller.Case_ListOppSplits_Controller(ApexPages.StandardController controller)'
Am I anywhere near a solution or is my understanding way off the mark?
Your help in solving this, and expanding my knowledge, would be very much appreciated.
- Michael Clarke 36
- August 13, 2020
- Like
- 0
- Continue reading or reply
Variable does not exist: addError
Hi, I am new to Apex
I have copied an example trigger code, however I get the error message 'Variable does not exist: addError'.
All it does is prevent deletion of an orphan contact with a custom error msg.
Can you tell me what I am doing wrong?
trigger ContactBeforeDelete on Contact (before delete)
{
for(Contact c:trigger.old)
{
if(c.accountId==null)
{
c.addError=('You are not authorised to delete this contact.');
}
}
}
I have copied an example trigger code, however I get the error message 'Variable does not exist: addError'.
All it does is prevent deletion of an orphan contact with a custom error msg.
Can you tell me what I am doing wrong?
trigger ContactBeforeDelete on Contact (before delete)
{
for(Contact c:trigger.old)
{
if(c.accountId==null)
{
c.addError=('You are not authorised to delete this contact.');
}
}
}
- Michael Clarke 36
- July 17, 2020
- Like
- 0
- Continue reading or reply
Apex and Test Class don't behave same as Anonymous Window
Hi,
I am a newbie to development and I am trying to create an Apex class that recreates activity of anonymous window code.
Intent is to delete contents of Account_Manager_Budget__c for the current month and repopulate it with data from the Account_Budget__c for the current month.
The aggregate result returns 9 records in Anonymous window but zero records when testing the Apex Class.
Test Class:
System.assertEquals shows recountCount = 0 but I know there are Account Budget records for 9 Account Managers that should be aggregated (and are in Anonymous window).
I would appreciate your expert eyes on this.
I am a newbie to development and I am trying to create an Apex class that recreates activity of anonymous window code.
Intent is to delete contents of Account_Manager_Budget__c for the current month and repopulate it with data from the Account_Budget__c for the current month.
public class PopulateAccountManagerBudget { @InvocableMethod public static void PopulatingBudgets(){ //set current month Integer BudgetYear = System.today().year(); Integer BudgetMonth = System.today().month(); system.debug(BudgetMonth + '/' + BudgetYear); //Delete all Account Manager Budget records for current month (including forecasts) List<Account_Manager_Budget__c> AccMgrBudget = [SELECT Id FROM Account_Manager_Budget__c WHERE Year__c = :BudgetYear AND Month__c = :BudgetMonth]; system.debug(AccMgrBudget.size() + ' records deleted.'); delete AccMgrBudget; //Retrieve all current Month Account budgets and sum to Account Manager level AggregateResult[] groupedBudget = [SELECT Account_Manager__c, SUM(Budget__c)Budget FROM Account_Budget__c WHERE Year__c = :BudgetYear AND Month__c = :BudgetMonth GROUP BY Account_Manager__c]; system.debug('groupedBudget = ' + groupedBudget.size() + ' results'); //Set up List to insert budget into Account Manager Budget object List<Account_Manager_Budget__c> NewAccMgrBudget = new List<Account_Manager_Budget__c>(); String AM_Id = ''; String AM_Budget = ''; //Loop through budgets and assign to List for(AggregateResult ar:groupedBudget){ AM_Id = String.valueof(ar.get('Account_Manager__c')); AM_Budget = string.valueof(ar.get('Budget')); System.debug('Account Manager = ' + String.valueof(ar.get('Account_Manager__c')) + ', BudgetYear = ' + BudgetYear + ', BudgetMonth = ' + BudgetMonth + ', Budget = ' + string.valueof(ar.get('Budget'))); // Account_Manager_Budget__c amb = new Account_Manager_Budget__c(Account_Manager__c=String.valueof(ar.get('Account_Manager__c')), Year__c=BudgetYear, Month__c=BudgetMonth, Budget__c=decimal.valueof(string.valueof(ar.get('Budget')))); Account_Manager_Budget__c amb = new Account_Manager_Budget__c(Account_Manager__c=AM_Id, Year__c=BudgetYear, Month__c=BudgetMonth, Budget__c=decimal.valueof(AM_Budget)); NewAccMgrBudget.add(amb); } //insert list into Account Manager Budget object for current month, with Forecast = 0 if(NewAccMgrBudget.size()>0){ insert NewAccMgrBudget; } } }
The aggregate result returns 9 records in Anonymous window but zero records when testing the Apex Class.
Test Class:
@isTest private class PopulateAccountManagerBudget_test{ @isTest static void MyTestClass(){ PopulateAccountManagerBudget.PopulatingBudgets(); //system.debug('RecourdCount=' + recordCount); Integer recordCount = 0; Integer BudgetYear = System.today().year(); Integer BudgetMonth = System.today().month(); system.debug(BudgetMonth + '/' + BudgetYear); // AggregateResult[] countRecords = [SELECT COUNT(Id)recordCount FROM Account_Manager_Budget__c WHERE Budget_Date__c = :dateBudgetDate]; AggregateResult[] groupedBudget = [SELECT Account_Manager__c, SUM(Budget__c)Budget FROM Account_Budget__c WHERE Year__c = :BudgetYear AND Month__c = :BudgetMonth GROUP BY Account_Manager__c]; for(AggregateResult ar:groupedBudget){ recordCount = recordCount+1; } system.assertEquals(9, recordCount); } }
System.assertEquals shows recountCount = 0 but I know there are Account Budget records for 9 Account Managers that should be aggregated (and are in Anonymous window).
I would appreciate your expert eyes on this.
- Michael Clarke 36
- May 07, 2021
- Like
- 0
- Continue reading or reply
My first controller extension
Hi, I am learning skills as a developer and I have a use case for what I have learnt.
The Case object has parent Opportunity, and the Opportunity object has children Opportunity_Split__c.
I would like to show Opportunity Splits on the Case page using the Lightning App Builder.
My understanding is that for a visualforce page to appear on a Case Page it needs to utilise the Case standard controller. Therefore I need to extend the standard controller with an extension.
Visualforce code is:
Am I anywhere near a solution or is my understanding way off the mark?
Your help in solving this, and expanding my knowledge, would be very much appreciated.
The Case object has parent Opportunity, and the Opportunity object has children Opportunity_Split__c.
I would like to show Opportunity Splits on the Case page using the Lightning App Builder.
My understanding is that for a visualforce page to appear on a Case Page it needs to utilise the Case standard controller. Therefore I need to extend the standard controller with an extension.
Visualforce code is:
<apex:page standardController="Case" extensions="Case_ListOppSplits_Controller" lightningStylesheets="true"> <apex:pageBlock title="Opportunity Splits"> <apex:pageBlockTable value="{!Opportunity_Splits}" var="oppSplit"> <apex:column value="{!oppSplit.Name}"/> <apex:column value="{!oppSplit.Split_Loan_Amount__c}"/> <apex:column value="{!oppSplit.Rate_Type__c}"/> <apex:column value="{!oppSplit.Repayment_Type__c}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:page>Case Controller extension is:
public class Case_ListOppSplits_Controller { public ApexPages.StandardSetController setController{ get{ if(setController==NULL){ Case currentCase = [SELECT Id, Subject, Opportunity__c FROM Case WHERE Id =: ApexPages.currentPage().getParameters().get('id')]; setController = new ApexPages.StandardSetController(Database.getQueryLocator( [SELECT Id, Name, Opportunity__c, Loan_Purpose__c, Loan_Type__c, Loan_Usage__c, Rate_Type__c, Repayment_Type__c, Split_Loan_Amount__c FROM Opportunity_Split__c WHERE Opportunity__c =: currentCase.Opportunity__c])); } return setController; } set; } //initialise setController and return a list of records public list<Opportunity_Split__c> getOpportunity_Splits(){ return (List<Opportunity_Split__c>) setController.getRecords(); } }The visualforce page cannot be saved as it throws this error: Unknown constructor 'Case_ListOppSplits_Controller.Case_ListOppSplits_Controller(ApexPages.StandardController controller)'
Am I anywhere near a solution or is my understanding way off the mark?
Your help in solving this, and expanding my knowledge, would be very much appreciated.
- Michael Clarke 36
- August 13, 2020
- Like
- 0
- Continue reading or reply
Variable does not exist: addError
Hi, I am new to Apex
I have copied an example trigger code, however I get the error message 'Variable does not exist: addError'.
All it does is prevent deletion of an orphan contact with a custom error msg.
Can you tell me what I am doing wrong?
trigger ContactBeforeDelete on Contact (before delete)
{
for(Contact c:trigger.old)
{
if(c.accountId==null)
{
c.addError=('You are not authorised to delete this contact.');
}
}
}
I have copied an example trigger code, however I get the error message 'Variable does not exist: addError'.
All it does is prevent deletion of an orphan contact with a custom error msg.
Can you tell me what I am doing wrong?
trigger ContactBeforeDelete on Contact (before delete)
{
for(Contact c:trigger.old)
{
if(c.accountId==null)
{
c.addError=('You are not authorised to delete this contact.');
}
}
}
- Michael Clarke 36
- July 17, 2020
- Like
- 0
- Continue reading or reply