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
Semira@gmail.comSemira@gmail.com 

lightning Component validate field value and call apex

Hi, 

I am ripping my hair off trying to figure this one out. I'm relatively new at Lightning so not sure if I'm doing this correctly. So I have a button when clicked will call an apex class but the issue is, it needs to first verify the Opportunity Stage being Complete or Won before calling the Apex class. It should also pop up a message informing users that Operation was not successful because the Stage was not Compelete or Won. 

The apex class returns a Status which is success. If so, it should also popup message saying the operations is success. 

Here is my Javascrip I'm trying to convert to Lightning: 
//alert('{!Opportunity.StageName}');
if("{!Opportunity.StageName}" != "Won" && "{!Opportunity.StageName}" != "Complete"){ 
alert("Only Jobs Won or Complete are Synced with EConz."); 
} 
else{ 
var result = sforce.apex.execute("Econz_Controller","invokeIntegration", {OppId:"{!Opportunity.Id}"}); 
if(result == "Success") 
alert("The Job has been synced with EConz."); 
else 
alert("There was an error while syncing. Pls retry again or contact your Administrator"); 
}

Now, here is my Lightning Component:
<aura:component controller="SyncEconzLT" implements="force:lightningQuickActionWithoutHeader,force:hasRecordId" >
    <aura:attribute name="returnMsg" type="String" />
    
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <div class="recordSaveError">
            {!v.returnMsg}
    </div>
</aura:component>

Here's my Lightning Controller:
({
	doInit : function(component, event, helper) {
        var action = component.get("c.executeJob");
        action.setParams({"oppoId": component.get("v.recordId")});
        // Configure response handler
        action.setCallback(this, function(response) {
            var state = response.getState();
            debugger;
            if(component.isValid() && state === "SUCCESS") {
                component.set("v.returnMsg", response.getReturnValue());  
            } else if (state() === “ERROR”){
                var errors = action.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        cmp.set(“v.message”, errors[0].message);
                    }
                   
                }
            }
        });

		$A.enqueueAction(action);

    },

})

If you have any input on how to approach this, please let me know. Thank you.!
Best Answer chosen by Semira@gmail.com
Raj VakatiRaj Vakati
Try something like this 
 
<aura:component controller="SyncEconzLT" implements="force:lightningQuickActionWithoutHeader,force:hasRecordId" >

    <aura:attribute name="returnMsg" type="String" />
    <aura:attribute name="opp" type="Opportunity" />

    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
  
  <div class="recordSaveError">
            {!v.returnMsg}
    </div>
</aura:component>



({
	doInit : function(component, event, helper) {
	
	var name=component.find("opp");
var stageName=name.get("v.StageName");
if(stageName!='<value>')){
stageName.set("v.errors", [{message:"Error"}]);

}else{

        var action = component.get("c.executeJob");
        action.setParams({"oppoId": component.get("v.recordId")});
        // Configure response handler
        action.setCallback(this, function(response) {
            var state = response.getState();
            debugger;
            if(component.isValid() && state === "SUCCESS") {
                component.set("v.returnMsg", response.getReturnValue());  
            } else if (state() === “ERROR”){
                var errors = action.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        cmp.set(“v.message”, errors[0].message);
                    }
                   
                }
            }
        });

		$A.enqueueAction(action);

    },
}
})