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
prateek khanna 24prateek khanna 24 

I am new to Lightning aura component, I need to perform one task when I click on Approve button in this component i Have one object feedback in that status picklist filed should be updated to approved. below are the code. Please help.

I have one query-
 Component-
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" controller="feedbackCls">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="feedbackLst" type="object"/>
    <div>
        <table>
            <thead>
                <th>Opportunity Name</th>
                <th>Manager</th>
                <th>Comments</th>
                <th>Action</th> 
            </thead>
            <tbody>
                <aura:iteration items="{!v.feedbackLst}" var="lst">
                    <tr>
                        <td>{!lst.Opportunity__r.Name}</td>
                        <td>{!lst.Opportunity__r.ownerId}</td>
                        <td>{!lst.Feedback__c}</td> 
                        <td><input id="{!lst.Id}" type="button" label="approve" value="Approve" onclick="{!c.doApprove}"/><input type="button" value="Reject"/></td>
                    </tr> 
                </aura:iteration>
            </tbody>
        </table>
    </div>
</aura:component>

Controller - 

({
    doInit : function(component, event, helper) {
        console.log('===Test==');
        var action = component.get("c.fetchRecords");
        console.log('===action==='+action);
        action.setCallback(this, function(response) {
            console.log('===response==='+response.getReturnValue());
            component.set("v.feedbackLst",response.getReturnValue());
            var state = response.getState();
            if (state === "SUCCESS") {
                console.log("From server: " + response.getReturnValue());
            }
        }); 
        $A.enqueueAction(action);
    },
   doApprove:function(component,event,helper){
        var a = event.target.Id;
        console.log(a);
  },
})

Apex class-
 
public class feedbackCls {
    @auraEUser-added imagenabled
    public static list<Feedbacks__c> fetchRecords(){
        list<Feedbacks__c> lst = new list<Feedbacks__c>();
        lst = [select id,name,Feedback__c,Opportunity__r.Name,Opportunity__r.ownerId from Feedbacks__c];
        return lst;
    }
Maharajan CMaharajan C
HI Prateek,

Please use the below updated JS: ( There is a problem in line  event.target.Id ==>  event.target.id

JS:

doApprove:function(component,event,helper){
        var a = event.target.id;    //   here Id should be in small letters. Otherewise you will gwt undefined error
        console.log(a);
        var action = component.get("c.updateFeedback");
        action.setParams({
            recordId : a   
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                alert("**** Record Updated Successfully *** ");
            }
        });
        $A.enqueueAction(action);
    }


Apex Class:

   @AuraEnabled
    public static void updateFeedback(Id recordId){
        system.debug('  ==>  ' + recordId);
        update new Feedbacks__c(Id=recordId, Status__c = 'approved');
    }


Thanks,
Maharajan.C