function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sathish LoganathanSathish Loganathan 

How to get the value of a formula field inside a lightning controller without Apex class?

Hi,

I am new to Lightning development.

Can someone help me with this:

I get undefined when I try to get the value of a formula field.

var EditPaymentsfieldValue = component.get('v.EditPaymentsRequired__c'); - < formula field.

If I refer other custom field, it is returning the value.

Regards,
Sathish Loganathan
Deepali KulshresthaDeepali Kulshrestha
Hi Sathish,
Greetings to you!

- Without an apex class, it is not possible to fetch the records or value from the database.
- var EditPaymentsfieldValue = component.get('v.EditPaymentsRequired__c'); , In this you can't get field value directly .
- Can you please share your code?
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
 
Ajay K DubediAjay K Dubedi
Hi Satish,

As far as I understand your problem, here's the answer. Lightning component as you knnow has 3 parts: component which serves as the frontend, Javascript which acts as a link between frontend and backend and Apex which serves as backend. Since objects and custom fields are on backend, so you can't fetch them EASILY without using Apex. Still if you want it without using apex do it as per following link. You still need to query fields from backend and then you can use it in controller directly without using Apex. Follow: http://www.sfdcpoint.com/salesforce/soql-query-in-javascript-example/

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi    
Sathish LoganathanSathish Loganathan
Hi Deepika and Ajay,

Thanks a lot for the reply.

Below is my code . I am still confused how non -formula custom fields returns a value and formula fieds doesn't.
({
    executes : function(component, event, helper) {
        var recordId = component.get("v.recordId");
        var EditPaymentsfieldValue = component.get('v.EditPaymentsRequired__c'); - >Data type - checkbox, returns a value
       alert(EditPaymentsfieldValue ); - >  value gets printed
        console.log("success"+EditPaymentsfieldValue );
        var oppname = component.get('v.OppName__c'); -> Formula Field, returns undefined error
  
     if(EditPaymentsfieldValue){
            alert("Before proceeding to Send to Zuora:\n\nMake sure your discount amount is correct – go to Discount Calculator\n\nYou must click 'Edit Payments' to update the invoice split payments for this quote before proceeding."); 
        }
else{
                helper.gotoURL(component, '/apex/z_SendtoZuora_new?id='+component.get('v.sObjectInfo.Id')+'');
                
           }
        $A.get("e.force:closeQuickAction").fire();   

    }
})

Regards,
Sathish Loganathan
Ajay K DubediAjay K Dubedi
Hi Satish,

From what code you have provided, I can suggest you check these points:

component.get('v.SomeName') returns undefined in following cases:
1. If it is not being set like c.set('v.SomeName',SomeValue) in JS controller/helper side.
2. If it is not being defined as aura: attributes in the component side.
3. If you are not fetching the exact name (with exact casing)
*4. If the value you are fetching (like OppName__c) is null or there is no record in which this field is defined.
5. Or maybe you are missing out fetching this field along with other fields like EditPaymentsRequired__c.

Hope this helps.
Thanks,
Ajay Dubedi