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
Sowmya YakkalaSowmya Yakkala 

How to get Particular field value from Json Response in Salesforce lightning Component

Actually when i selected Template i am getting the Data from apex class in Json Format. But the Problem here is, i want only Subject from the response from Apex class.

The Code is :
 getTemplate : function(component, event) {
        
        var tempName= component.get("v.selTempl");
        var action = component.get("c.getTemplateDetails");
        action.setParams({"templteId":tempName});
        action.setCallback(this,function(response){
              var responseVal = JSON.stringify(response.getReturnValue());
               component.set("v.subjTxt",responseVal.Subject);
               console.log('Subject--',subjTxt);
               component.set("v.templDetail",responseVal);                    
         });
       $A.enqueueAction(action);
  },

Actual Output is:
Selected Template Name: Scheduled Letter 
Response in JSON  Format - [{"Id":"00X2v000001H8xjEAC","Name":"Scheduled Letter","Subject":"Interview Scheduled","TemplateType":"visualforce"}]

Subject -- undefined

But Expected Output--
Subject -- Interview Scheduled

Can anyone pls Suggest the Solution for this....
Best Answer chosen by Sowmya Yakkala
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi sowmya,
Apex controller returning in list< EmailTemplate>  format and here in javascript you are accessing that list without specifying index.Try this one
getTemplate : function(component, event) {
        
        var tempName= component.get("v.selTempl");
        var action = component.get("c.getTemplateDetails");
        action.setParams({"templteId":tempName});
        action.setCallback(this,function(response){
              var responseVal = response.getReturnValue();//changed here
               component.set("v.subjTxt",responseVal[0].Subject);
               console.log('Subject--',subjTxt);
               component.set("v.templDetail",responseVal);                    
         });
       $A.enqueueAction(action);
  },
Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.
Thanks and Regards

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi sowmya,
use JSON.parse() to convert it into javascript object format if your are serializing while returning from controller and then you can access properties of it.
 
getTemplate : function(component, event) {
        
        var tempName= component.get("v.selTempl");
        var action = component.get("c.getTemplateDetails");
        action.setParams({"templteId":tempName});
        action.setCallback(this,function(response){
              var responseVal = JSON.parse(response.getReturnValue());//changed here
               component.set("v.subjTxt",responseVal.Subject);
               console.log('Subject--',subjTxt);
               component.set("v.templDetail",responseVal);                    
         });
       $A.enqueueAction(action);
  },

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
 
Sowmya YakkalaSowmya Yakkala
Hi Chandrika,

Thank you for  u r response,But when i  use JSON.parse() the data is not visible in console.log it self.
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
If you can post the apex controller code here,i can help you in this

Thanks!
Sowmya YakkalaSowmya Yakkala
My Apex controller is 

@AuraEnabled 
    public static List<EmailTemplate> getTemplateDetails(string templteId){
        System.debug('templteId--'+templteId);
        List<EmailTemplate>  emailTempLst = [SELECT Id,Name,Subject,TemplateType,body FROM EmailTemplate WHERE Name =: templteId];
        System.debug('emailTempLst--'+emailTempLst);
        return emailTempLst;
        
    }
 
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi sowmya,
Apex controller returning in list< EmailTemplate>  format and here in javascript you are accessing that list without specifying index.Try this one
getTemplate : function(component, event) {
        
        var tempName= component.get("v.selTempl");
        var action = component.get("c.getTemplateDetails");
        action.setParams({"templteId":tempName});
        action.setCallback(this,function(response){
              var responseVal = response.getReturnValue();//changed here
               component.set("v.subjTxt",responseVal[0].Subject);
               console.log('Subject--',subjTxt);
               component.set("v.templDetail",responseVal);                    
         });
       $A.enqueueAction(action);
  },
Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.
Thanks and Regards
This was selected as the best answer