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
HrudayHruday 

How to pull Field values of a on the Controller side

I am trying to build a quick action, when clicked, will check for the Status field. IF status != Closed, wil redirect to visualforce page. Below is my component and controller codes. 
CloseCase.cmp
<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId,flexipage:availableForAllPageTypes">
     <!-- This attribute saves the record ID -->
    <aura:attribute name="recordId" type="String" />
     <aura:attribute name="CaseObject" type="Object" /> 
    <aura:attribute type="Object" name="record"/>
 
    <aura:attribute name="CaseStatusValue" type = "String" /> -->
   

   <aura:attribute name="recordLoadError" type="String"/>

   <force:recordData aura:id="recordLoader"
   recordId="{!v.recordId}"
   fields="Name,Status"
   targetFields="{!v.CaseObject}"
   targetError="{!v.recordLoadError}"
   targetRecord="{!v.record}"                    
   />

    
    
    <!-- This executes a function from the controller as soon as the component is open. 
    This is recommended when the action only processes data and there’s no need for user interaction -->
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
</aura:component>

CloseCaseController.js
({
    doInit : function(component, event, helper) {
        var Caseid = component.get("v.recordId");
        var CaseStatus = component.get("v.CaseObject").Status;
        console.log(CaseStatus);
        if(CaseStatus != "Closed"){     
        var urlEvent = $A.get("e.force:navigateToURL");
    urlEvent.setParams({
        "url": "/apex/KBReviewClose?id="+Caseid+"",
        "width" : "700",
        "height" : "1000",
        "scrollbars" : "yes"
    });
        
    urlEvent.fire();
    }
        else{ 
                    var resultsToast = $A.get("e.force:showToast");
                    resultsToast.setParams({
                        "title" : "Failed",
                        "message" : "Case is already closed",
                        "type" : "warning",
                         "duration" : "60000ms"
                    });
                      resultsToast.fire();}
    }
})

On the controller side, I am trying to get the 'Status' value of the case record and redirecting to a visualforce page if status is not equal to 'Closed'.

I am getting the below error. please help.
Error while creating component for lightning component quick action [Action failed: c:LCC_Close_KB_Review_Case$controller$doInit [Cannot read property 'Status' of null]].
 
Best Answer chosen by Hruday
Rushikesh KhandaleRushikesh Khandale
Well here is the complete code which is working for me,
component:
<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId,flexipage:availableForAllPageTypes">
     <!-- This attribute saves the record ID -->
     <aura:attribute name="CaseObject" type="Object" /> 
     <aura:attribute name="recordLoadError" type="String"/>
   <force:recordData aura:id="record"
        recordId="{!v.recordId}"
        fields="Status"
        targetFields="{!v.CaseObject}"
        targetError="{!v.recordLoadError}"
        recordUpdated="{!c.doInit}"                  
   />
</aura:component>

Controller:
({
    doInit : function(component, event, helper) {
        var Caseid = component.get("v.recordId");
        console.log(JSON.stringify(component.get("v.CaseObject")));
        var CaseStatus = component.get("v.CaseObject").Status;
        console.log(CaseStatus);
        if(CaseStatus != "Closed"){
            var urlEvent = $A.get("e.force:navigateToURL");
            urlEvent.setParams({
            "url": "/",
            "width" : "700",
            "height" : "1000",
            "scrollbars" : "yes"
        });        
    urlEvent.fire();
    } else { 
        var resultsToast = $A.get("e.force:showToast");
        resultsToast.setParams({
            "title" : "Failed",
            "message" : "Case is already closed",
            "type" : "warning",
                "duration" : "60000ms"
        });
        resultsToast.fire();}
    }
})

 

All Answers

Rushikesh KhandaleRushikesh Khandale
force:recordData handles loading the record asynchronously. Therefore it will not be available during the first load. Instead of aura:handler use recordUpdated in your force:recordData.
Remove  <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
and update the force:recordData to
<force:recordData aura:id="recordLoader"
   recordId="{!v.recordId}"
   fields="Name,Status"
   targetFields="{!v.CaseObject}"
   targetError="{!v.recordLoadError}"
   targetRecord="{!v.record}"      
   recordUpdated="{!c.doInit}"          
  />
Hope this helps. Please mark it as a best answer if it does.

 
HrudayHruday
Hi @Rushikesh Khandale,

Thanks for the help. however, i still get CaseStatus =  'null' at CaseStatus variable in the controller.

component
<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId,flexipage:availableForAllPageTypes">
   
    <aura:attribute name="recordId" type="String" />
     <aura:attribute name="CaseObject" type="Case" /> 
    <aura:attribute type="Object" name="record"/>
   <aura:attribute name="recordLoadError" type="String"/>
   <force:recordData aura:id="record"
   recordId="{!v.recordId}"
   fields="Name,Status"
   targetFields="{!v.CaseObject}"
   targetError="{!v.recordLoadError}"
   targetRecord="{!v.record}"  
   recordUpdated="{!c.doInit}"                  
   />
       
</aura:component>

Controller
({
    doInit : function(component, event, helper) {
        var Caseid = component.get("v.recordId");
        var CaseStatus = component.get("{!v.CaseObject.Status}");
        console.log(CaseStatus);
        if(CaseStatus != "Closed"){     
        var urlEvent = $A.get("e.force:navigateToURL");
    urlEvent.setParams({
        "url": "/apex/KBReviewClose?id="+Caseid+"",
        "width" : "700",
        "height" : "1000",
        "scrollbars" : "yes"
    });        
    urlEvent.fire();
    }
        else{ 
                    var resultsToast = $A.get("e.force:showToast");
                    resultsToast.setParams({
                        "title" : "Failed",
                        "message" : "Case is already closed",
                        "type" : "warning",
                         "duration" : "60000ms"
                    });
                      resultsToast.fire();}
    }
})

I am sure there is some syntax error here, but unable to get through it.Can you also please help me how to assign it to CaseStatus variable in the controller?

Thanks
 
Rushikesh KhandaleRushikesh Khandale
The syntax you were using earlier was correct.
var CaseStatus = component.get("v.CaseObject").Status;
also the type of attribute CaseObject should be 'Object'
<aura:attribute name="CaseObject" type="Object" />


 
HrudayHruday
User-added image

component.
<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId,flexipage:availableForAllPageTypes">
     <!-- This attribute saves the record ID -->
    <aura:attribute name="recordId" type="String" />
     <aura:attribute name="CaseObject" type="Object" /> 
     <aura:attribute name="recordLoadError" type="String"/>

   <force:recordData aura:id="record"
   recordId="{!v.recordId}"
   fields="Name,Status"
   targetFields="{!v.CaseObject}"
   targetError="{!v.recordLoadError}"
   targetRecord="{!v.record}"  
   recordUpdated="{!c.doInit}"                  
   />
</aura:component>


controller.
({
    doInit : function(component, event, helper) {
        var Caseid = component.get("v.recordId");
        var CaseStatus = component.get("v.CaseObject").Status;
        console.log(CaseStatus);
        if(CaseStatus != "Closed"){
        var urlEvent = $A.get("e.force:navigateToURL");
    urlEvent.setParams({
        "url": "/apex/KBReviewClose?id="+Caseid+"",
        "width" : "700",
        "height" : "1000",
        "scrollbars" : "yes"
    });        
    urlEvent.fire();
    }
        else{ 
                    var resultsToast = $A.get("e.force:showToast");
                    resultsToast.setParams({
                        "title" : "Failed",
                        "message" : "Case is already closed",
                        "type" : "warning",
                         "duration" : "60000ms"
                    });
                      resultsToast.fire();}
    }
})


Please let me know what am I missing. Appreciate your help.
Rushikesh KhandaleRushikesh Khandale
Hi I tried your code in my Org and when I printed the 'recordLoadError' it showed the error 'No such column 'Name' exists on entity 'Case'. 
Which means that you only have to remove the 'Name' field from the fields List record data as it does not exist on Case.
<force:recordData aura:id="record"
   recordId="{!v.recordId}"
   fields="Status"
   targetFields="{!v.CaseObject}"
   targetError="{!v.recordLoadError}"
   targetRecord="{!v.record}"  
   recordUpdated="{!c.doInit}"                  
  />
Hope this helps. Please mark it as a best answer if it does.
 
HrudayHruday
User-added image

Hi Rushikesh,

I appreciate your help.I have removed 'Name' field from fields, With this, i could fix the syntax error, But still iam unable to pull the status field value onto the controller side. Please see the screenshot.Not sure what I am missing.

The requirement here is, when this component is clicked,  a visualforce page should be opened if the 'Status'!= Closed.
Rushikesh KhandaleRushikesh Khandale
Well here is the complete code which is working for me,
component:
<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId,flexipage:availableForAllPageTypes">
     <!-- This attribute saves the record ID -->
     <aura:attribute name="CaseObject" type="Object" /> 
     <aura:attribute name="recordLoadError" type="String"/>
   <force:recordData aura:id="record"
        recordId="{!v.recordId}"
        fields="Status"
        targetFields="{!v.CaseObject}"
        targetError="{!v.recordLoadError}"
        recordUpdated="{!c.doInit}"                  
   />
</aura:component>

Controller:
({
    doInit : function(component, event, helper) {
        var Caseid = component.get("v.recordId");
        console.log(JSON.stringify(component.get("v.CaseObject")));
        var CaseStatus = component.get("v.CaseObject").Status;
        console.log(CaseStatus);
        if(CaseStatus != "Closed"){
            var urlEvent = $A.get("e.force:navigateToURL");
            urlEvent.setParams({
            "url": "/",
            "width" : "700",
            "height" : "1000",
            "scrollbars" : "yes"
        });        
    urlEvent.fire();
    } else { 
        var resultsToast = $A.get("e.force:showToast");
        resultsToast.setParams({
            "title" : "Failed",
            "message" : "Case is already closed",
            "type" : "warning",
                "duration" : "60000ms"
        });
        resultsToast.fire();}
    }
})

 
This was selected as the best answer
HrudayHruday
Thanks for the help Rushikesh.
Yajuvendra Choubey 1Yajuvendra Choubey 1
I am also trying to achieve the same requiremnet. Did any body get a chance to complete this. Please respond how did you do it. Thanks!