0

I have a lightning component which saves a record to the server with some data on the fields. On the record I have a formula field which calculates the sum of the fields.

How do I return the value of the formula field directly to the clientside?

Apex controller:

public with sharing class ExpensesCreate {
    @AuraEnabled
    public static ExcelSheet__c saveExpense(ExcelSheet__c expense) {
        upsert expense;
        return expense;
    }
}

Javascript controller:

create : function(component, event, helper) {

        //getting the excelSheet information
        var excelSheet = component.get("v.excelSheet");
        var action = component.get("c.saveExpense");
        action.setParams({
            expense : excelSheet
        });

        action.setCallback(this,function(a){
            var state = a.getState();
            if(state == "SUCCESS"){

                var excelSheet = component.get("v.excelSheet");
                excelSheet.push(response.getReturnValue());
                component.set("v.excelSheet", excelSheet);

                alert('Record is Created Successfully');   
            } else if(state == "ERROR"){
                alert('Error in calling server side action');
            }
        });
        $A.enqueueAction(action);
    },



I tried the following:


In my debug log I see the return of the record with Id without the formulafield.

I tried in my apex controller:

upsert expense;
update expense;
return expense;

and

upsert expense;
expense.recalculateFormulas();
return expense;

and

expense.Totaal__c = [SELECT Totaal__c FROM ExcelSheet__c WHERE id=expense.id LIMIT 1].Totaal__c;

Gives: Field is not writeable: ExcelSheet__c.Totaal__c

None seem to return the value of the formula

  • Query the fields in apex controller and You will get the updated values but that's not the best solution. – amidstCloud Mar 28 '18 at 7:22
1

Write a new method GetExpense() at apex side, query the fields which are needed and call this method after ExpensesCreate from JS controller so you will get all field values.

The reason you are getting id in ExpensesCreate response is, when you create/update the record it returns the id only which indicate the record is created/updated. So if you want other field values of that record, you need to query them in another method.

Create the following method at the apex controller side

@AuraEnabled
public static ExcelSheet__c GetExpense(Id expenseId) {
    List<ExcelSheet__c> lstExpense = [SELECT id,fieldname1,fieldname2,fieldname3.....fieldnameN FROM ExcelSheet__c WHERE id = : expenseId LIMIT 1];
    if(!lstExpense.isEmpty())
        return lstExpense[0];

    return null;
}

And call this after saveExpense method when the response is success.

1

Usage This method doesn’t recalculate cross-object formulas. If you call this method on objects that have both cross-object and non-cross-object formula fields, only the non-cross-object formula fields are recalculated.

Each recalculateFormulas call counts against the SOQL query limits. See Execution Governors and Limits.

Query the fields in apex controller and You will get the updated values for sure but that's might not be the best solution.

  • Then what is the best solution? – itzmukeshy7 Mar 28 '18 at 7:28
  • @amidstCloud , tried your solution but because this is a formula field this doesnt work. See my update. – Thomas Mar 28 '18 at 7:35
  • why are you assigning it to expense object? You can't assign values to formula fields...Can't You return the queried ExcelSheet__c – amidstCloud Mar 28 '18 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.