You need to sign in to do that
Don't have an account?
David Silva
Error: Initial term of field expression must be a concrete sObject.
public class TotalExpenseComputation {
/*
* The totalExpense method calculates the total of all the approved expenses as well as the amount left
*
* for both the marketing and sales budget.
* */
public static void totalExpense(Expense__c[] expenses)
{
//Query to get Initial Amount,Total Expense and Remaining Amount field from respective budgets.
List<Budget__c> typeList = new List<Budget__c>();
for(Budget__c[] b:[SELECT Name,Initial_Amount__c,Total_Expenses__c,Remaining_Amount__c,Type__c FROM Budget__c])
{
for(Expense__c exp:expenses)
{
if(exp.Type__c.equals('Marketing')&& exp.Budget_LookUp__c.equals(b.Name))
{
if(exp.Status__c.equals('Approved'))
{
b.Total_Expenses__c= exp.Amount__c+b.Total_Expenses__c;
b.Remaining_Amount__c=b.Initial_Amount__c-b.Total_Expenses__c;
typeList.add(b);
}
}
if(exp.Type__c.equals('Sales')&& exp.Budget_LookUp__c.equals(b.Name))
{
if(exp.Status__c.equals('Approved'))
{
b.Total_Expenses__c =exp.Amount__c+b.Total_Expenses__c;
b.Remaining_Amount__c=b.Initial_Amount__c-b.Total_Expenses__c;
typeList.add(b);
}
}
}update typeList;}
/*
* The totalExpense method calculates the total of all the approved expenses as well as the amount left
*
* for both the marketing and sales budget.
* */
public static void totalExpense(Expense__c[] expenses)
{
//Query to get Initial Amount,Total Expense and Remaining Amount field from respective budgets.
List<Budget__c> typeList = new List<Budget__c>();
for(Budget__c[] b:[SELECT Name,Initial_Amount__c,Total_Expenses__c,Remaining_Amount__c,Type__c FROM Budget__c])
{
for(Expense__c exp:expenses)
{
if(exp.Type__c.equals('Marketing')&& exp.Budget_LookUp__c.equals(b.Name))
{
if(exp.Status__c.equals('Approved'))
{
b.Total_Expenses__c= exp.Amount__c+b.Total_Expenses__c;
b.Remaining_Amount__c=b.Initial_Amount__c-b.Total_Expenses__c;
typeList.add(b);
}
}
if(exp.Type__c.equals('Sales')&& exp.Budget_LookUp__c.equals(b.Name))
{
if(exp.Status__c.equals('Approved'))
{
b.Total_Expenses__c =exp.Amount__c+b.Total_Expenses__c;
b.Remaining_Amount__c=b.Initial_Amount__c-b.Total_Expenses__c;
typeList.add(b);
}
}
}update typeList;}
I reviewed the code and modified accordingly.
Here I considered:
(1) Naming Convention.
(2) Alignment.
(3) Added the comments.
(4) Corrected the logic to bulkify the code.
I couldn't test is as I don't have setup in my DE environment.
Please do let me know if it helps you.
Regards,
Mahesh
All Answers
I reviewed the code and modified accordingly.
Here I considered:
(1) Naming Convention.
(2) Alignment.
(3) Added the comments.
(4) Corrected the logic to bulkify the code.
I couldn't test is as I don't have setup in my DE environment.
Please do let me know if it helps you.
Regards,
Mahesh
Thanks for the help.
That issue is resolved.
The new issue is: on program execution
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger TotalExpenseComputationtrigger caused an unexpected exception, contact your administrator: TotalExpenseComputationtrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.TotalExpenseComputation.totalExpense: line 25, column 1.
With Regards,
Nisha
Please try the code which I posed above.
Regards,
Mahesh
Thanks fo the guidance.
Problem is solved.
Could u explain this step.
Budget__c bud = budMap.get(exp.Budget_LookUp__c);
if(bud != null && exp.Status__c == 'Approved' && (exp.Type__c == 'Marketing' || exp.Type__c == 'Sales')) {
bud.Total_Expenses__c = exp.Amount__c + bud.Total_Expenses__c;
bud.Remaining_Amount__c = bud.Initial_Amount__c - bud.Total_Expenses__c;
With Regards,
Nisha
// Here I am passing the Budget Lookup and get the corresponding Budget record.
Budget__c bud = budMap.get(exp.Budget_LookUp__c);
// Here I am checking the corresponding budget record exists or not. Also checking the other parameters.
if(bud != null && exp.Status__c == 'Approved' && (exp.Type__c == 'Marketing' || exp.Type__c == 'Sales')) {
// Here your business logic for the calculation.
bud.Total_Expenses__c = exp.Amount__c + bud.Total_Expenses__c;
bud.Remaining_Amount__c = bud.Initial_Amount__c - bud.Total_Expenses__c;
Regards,
Mahesh
Understood.Thank You very much.
I am just a beginner:
Could u help me in designing the test class for it :
/////////////////////////////////////////////
Trigger
trigger TotalExpenseComputationtrigger on Expense__c (after insert,after update) {
Expense__c[] expenses = Trigger.New;
TotalExpenseComputation.totalExpense(expenses);
}
///////////////////////////////////////////////////////////
Test Class I wrote:
isTest public class TotalExpenseTestClass {
@ isTest public static void TesttotalExpense()
{
Expense__c exp = new Expense__c(Name='Mar',Type__c='Marketing',Amount__c=200,Status__c='Approved',Budget_LookUp__c='Marketing 2');
//TotalExpenseComputation.totalExpense(exp);
}
}
With Regards,
Nisha.
You need to create a Budget record first then insert the Expense record by assigning the Budget_Lookup__c with the created id.
It will automatically fire the trigger.
Regards,
Mahesh
Hi Mahesh,
@isTest public class TotalExpenseTestClass {
@ isTest public static void TesttotalExpense()
{
Budget__c budg= new Budget__c(Name='Sales2',Type__c='Sales',Initial_Amount__c = 50000,Total_Expense__c= 0,Remaining_Amount__c=0);
insert budg;
Expense__c exp = new Expense__c(Name='Mar',Type__c='Marketing',Amount__c=200,Status__c='Approved',Budget_LookUp__c.Id=budg.Id);
insert exp;
}
}
The above code is not working.
With Regards,
Nisha
Hi Mahesh,
Budget__c budg= new Budget__c(Name='Sales2',Initial_Amount__c = 50000,Valid_From__c='1/1/2016',Valid_To__c='12/1/2016',Description__c='Budget',
Type__c='Sales',Budget_Owner__c='David Silva',Total_Expenses__c= 0,Remaining_Amount__c=0);
insert budg;
Expense__c exp = new Expense__c(Name='Mar',Expense_id__c=257,Reason__c='Travel',Amount__c=200,Date__c='5/22/2016',
Other_Expense__c='Certification',
Status__c='Approved',Type__c='Sales',Budget.LookUP__c.Id=budg.Id);
insert exp;
Error:
Invalid field initializer: Budget.LookUP__c.IdDisplay Error
Display Error
With Regards,
Nisha
Please find the below code:
If your field is Budget_LookUP__c
If your field is LookUP__c
Regards,
Mahesh
Thanks a lot for your help.
But it is not taking the date,I gave system.today() and run the program.The test passed and 86%code coverage.
The budget and the expense that created through the testclass is not there in the actual salesforce UI.
Is it like that?
With Regards
Nisha.
Yes, whatever we insert / update / delete the records in Test Methods will not commit to the Salesforce database. It will just simulate the operation.
Regards,
Mahesh
Thank You for the help.
With Regards,
Nisha.