1

I have created one custom button to call lightning component. On button click, I want to validate some opportunity field but I'm not getting field value.

Below is my component and controller code.I want to get opportunity field value (custom or standard both) and based on value I validate these fields in my code.

In my case, I want to get lookup field(DivisionDimensionTag__c) and validate lookup field is blank or not. How I achieve this?

Controller

({
    "showSpinner" : function (component, event, helper) {
        var spinner = component.find('spinner');
        var evt = spinner.get("e.toggle");
        evt.setParams({ isVisible : true });
        evt.fire();    
    },

    "doInit" : function(component, event, helper) {
        var name=component.find("opp");
        var stageName=name.get("v.Divisiondimensiontag__c");
        // create a one-time use instance of the serverEcho action
        // in the server-side controller
        var action = cmp.get("c.generate");
        action.setParams({ oppId : component.get("v.recordId") });

        // Create a callback that is executed after 
        // the server-side action returns
        action.setCallback(this, function(response) {


            var state = response.getState();
            if (state === "SUCCESS") {
                window.location.reload();
            }
            else if (state === "INCOMPLETE") {
            }
                else if (state === "ERROR") {
                    var errors = response.getError();
                    if (errors) {
                        if (errors[0] && errors[0].message) {
                            console.log("Error message: " + 
                                        errors[0].message);
                        }
                    } else {
                        console.log("Unknown error");
                    }
                }
        });

        $A.enqueueAction(action);
    }
})

Component
<aura:component controller="GenerateProducts" implements="force:lightningQuickAction,force:hasrecordid" >
    <aura:attribute name="opp" type="Opportunity" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:handler event="aura:waiting" action="{!c.showSpinner}"/>
    <br/>
    <br/>
    <center><ui:spinner aura:id="spinner"/></center>
</aura:component>

1 Answer 1

1

You have to ask for the record first. You can avoid writing Apex Code for this specific purpose via force:recordData:

<aura:attribute name="record" type="Opportunity" />
<force:recordData recordId="{!v.recordId}"
                  targetFields="{!v.record}"
                  fields="Divisiondimensiontag__c"
                  recordUpdated="{!c.handleRecordLoaded}" />

In your handleRecordLoaded method, that's where you'd be able to check the value:

handleRecordLoaded: function(component, event, helper) {
  var opp = component.get("v.record"),
      tag = opp.Divisiondimensiontag__c;
  ...

Also note that the correct interface is force:hasRecordId, not force:hasrecordid. Always pay attention to your capitalization in Lightning, as everything is case sensitive.

2
  • In this case i'm getting only one field value that is Divisiondimensiontag__c. how if i want to get other field values in same call??
    – Harish
    Commented Jun 25, 2018 at 19:53
  • @Harish You can provide a list of fields, or you can use the layoutType attribute to get all fields on the page layout. Go ahead and look at the documentation.
    – sfdcfox
    Commented Jun 25, 2018 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .