Activity Forums Salesforce® Discussions My component is not working in Update

  • My component is not working in Update

    Posted by Raghav Chaturvedi on September 11, 2018 at 9:34 am

    <aura:component implements="force:appHostable,flexipage:availableForAllPageTypes" controller="AccountSearchController">
    <aura:attribute name="searchKeyword" type="String" default=""/>
    <aura:attribute name="lstOfAccount" type="Account[]"/>
    <aura:attribute name="listcontact" type="Contact[]"/>
    <aura:attribute name="columns" type="List"/>
    <aura:attribute name="isBlank" type="boolean" default="false"/>
    <aura:attribute name="recordId" type="String"/>
    <aura:attribute name="updatedRecord" type="Object[]" />

    <div class="slds-m-around_medium" style="background:#fff">
    <lightning:layout>
    <lightning:layoutItem padding="around-small">
    <lightning:spinner variant="brand" size="medium" aura:id="Id_spinner" class="slds-hide" />
    <h1 class="slds-page-header__title slds-truncate slds-align-middle"
    title="Enter Account Name">Enter Account Name</h1>
    <lightning:input value="{!v.searchKeyword}" required="true" aura:id="searchKeyword"/>
    </lightning:layoutItem>
    </lightning:layout>

    <lightning:layout>
    <lightning:layoutItem size="2" padding="around-small">
    <lightning:button onclick="{!c.Search}" variant="brand" label="Search" iconName="utility:search"/>
    </lightning:layoutItem>
    </lightning:layout>
    <aura:if isTrue="{!and(v.isBlank, v.lstOfAccount!=NULL)}">
    <lightning:card title="Account Records" variant="brand">
    <lightning:datatable columns="{! v.columns }" data="{!v.lstOfAccount}"
    hideCheckboxColumn="true" keyField="Id" onsave ="{!c.Save}"/>
    </lightning:card>
    </aura:if>
    </div>
    </aura:component>
    ({
    Search : function(component, event, helper) {
    component.set('v.columns', [
    {label: 'Name', fieldName: 'Name', editable:'true', type: 'text'},
    {label: 'Phone', fieldName: 'Phone', editable:'true', type: 'Phone'},
    {label: 'Industry', fieldName: 'Industry', editable:'true', type: 'text'}]);
    var action = component.get("c.fetchAccount");
    action.setParams({
    "searchKeyWord" : component.get("v.searchKeyword")
    });
    action.setCallback(this, function(response) {
    var state = response.getState();
    if (state === "SUCCESS") {
    component.set("v.lstOfAccount", response.getReturnValue());
    component.set("v.isBlank",true);
    if(response.getReturnValue()==null){
    var toast = $A.get("e.force:showToast");
    toast.setParams({
    "type":"Error",
    "title":"Error",
    "message":"No Account Found",
    "duration":1000
    });
    toast.fire();
    }
    }
    });
    $A.enqueueAction(action);
    },
    Save : function (component,event,helper){
    var action=component.get("c.saveAccount");
    action.setParams({
    "AccListStr":JSON.stringify(component.get("v.lstOfAccount"))
    });
    action.setCallback(this, function(response) {
    var state = response.getState();
    if (component.isValid() && state === "SUCCESS") {
    component.set("v.lstOfAccount", response.getReturnValue());
    alert(response.getReturnValue());
    var toast = $A.get("e.force:showToast");
    toast.setParams({
    "type":"Success",
    "title":"Success",
    "message":"Account Saved Successfully",
    "duration":1000
    });
    toast.fire();
    }
    });
    $A.enqueueAction(action);
    }
    })
    public class AccountSearchController{

    @AuraEnabled
    public static List <Account> fetchAccount(String searchKeyWord) {
    String searchKey = searchKeyWord +'%';
    List <Account> lstOfAccount = [SELECT Id, Name,Phone,Industry FROM Account
    WHERE Name LIKE:searchKey];

    system.debug('==>'+lstOfAccount);
    if(lstOfAccount.size() > 0)
    return lstOfAccount;
    else return null;
    }
    @auraEnabled
    public static List<Account> saveAccount(String AccListStr){
    List<Account> accList = (List<Account>)json.deserialize(AccListStr, List<Account>.class);
    update accList;
    system.debug('===>accList'+accList);
    return accList;
    }
    }

    Avnish Yadav replied 6 years, 3 months ago 2 Members · 1 Reply
  • 1 Reply
  • Avnish Yadav

    Member
    September 11, 2018 at 10:53 am

    Hey Raghav,

    I had implemented your code in my org and I found that an edited value is not capturing the component.get("v.lstAccount"), therefore I suggest you to use "event.getParam('draftValues')" of lightning datatable feature and send it to Apex class. This will help you.

    For more information read the documentation of Lightning:datatable.

    Thanks.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos