1

I have a requirement where I need to populate data dynamically into my community page through my apex class.

@AuraEnabled
public static List<ZenObject__c> getZenObjectnew(){
    // ZenObject__c ZenObjsnew=DataBase.query('SELECT ZenLms_Name_del__c,ZenLms_SubText__c FROM ZenObject__c order By CreatedDate desc limit 1');
    List<ZenObject__c> ZenObjsnew=[SELECT ZenLms_Name_del__c,ZenLms_SubText__c FROM ZenObject__c order By CreatedDate desc limit 1];
    return ZenObjsnew; 
}

In this method I am returning some fields to my community page in a lightning component.As i create the record in my salesforce object, the query should fetch that record and immediately show it in my community page. I tried the above query but when ever I am creating new records it is replacing previously created records.

<aura:component implements="forceCommunity:availableForAllPageTypes" controller="ClassName">
    <aura:attribute name="ZenObjsnew" type="ZenObject__c[]" />
    <div class="slds-grid slds-wrap content-box">
        <aura:iteration items="{!v.ZenObjsnew}" var="zenobjsnew">
            <div class="slds-size--12-of-12 slds-medium-size--3-of-12">
                <img src="{!v.imagepath1}" alt="{!v.imagetext1}" />
            </div>
            <div class="slds-size--12-of-12 slds-medium-size--9-of-12">
                <h4 class="liheading">                     
                   <div aura:id="{!zenobjsnew.Id}">
                      <a href="{!v.linkpath1}">{!zenobjsnew.ZenLms_Name_del__c}</a>
                      <p class="text-limit">{!zenobjsnew.ZenLms_SubText__c}</p><br/><br/>
                   </div>   
                </h4>
            </div>
        </aura:iteration>
    </div>
</aura:component>

This is the component in which I am displaying the records. There is something definitely wrong in my query.

Edited

//Js controller

getZenObjectnew : function(component) {
    var action = component.get("c.getZenObjectnew");      
   // var self = this;
    action.setCallback(this, function(actionResult) {
        //alert('getZen1');
        var state = actionResult.getState();
        if (component.isValid() && state === "SUCCESS") {
            alert('frth');
            var resultObj = actionResult.getReturnValue();
            //var data=JSON.stringify(resultObj);
            alert(resultObj);
            var arr=component.get("v.ZenObjsnew");
            alert('arr line'+arr);
            arr.push(resultObj[0]);
            alert(arr);
              //component.set("v.ZenObjsnew", arr);
            component.set("v.ZenObjsnew", actionResult.getReturnValue()); 
            //alert(actionResult.getReturnValue());
            }

    });


        $A.enqueueAction(action);
    },
4
  • Chances are that you are over-writing the v.ZenObjsnew attribute in lightning component controller with the returned value from the Apex Controller. So if you paste your lightning controller code as well, we can confirm. – Santanu Halder Apr 17 '17 at 14:19
  • @SantanuHalder Thanks for reply..But v.ZenObjsnewis getting data from apex class only. – Deepak Agarwal Apr 17 '17 at 14:24
  • That's what I said, I am guessing you are overwritting the existing value of ZenObjsnew while saving the data from apex class. Paste your Lightning JS controller in the question. – Santanu Halder Apr 17 '17 at 14:26
  • @SantanuHalder.. Yaa when ever i am creating new data it is getting over ridden.But that should not happen. – Deepak Agarwal Apr 17 '17 at 15:01
3

The fix is simple - you need to PUSH new items to your array, rather than adding a new array - it overwrites it every time.

Also, before you push, make sure that you have a valid object before pushing.

Inside your calling method change it to something like this:

if (component.isValid() && state === "SUCCESS") {
    var resultArr = actionResult.getReturnValue();
    if (resultArr.length > 0 && resultArr[0].Id){ //or some other property that verifies success
      var arr = component.get("v.ZenObjsnew");
      arr.push(resultArr[0]);
      component.set("v.ZenObjsnew", arr);
    } 
}

EDIT - Here is the entire function

getZenObjectnew: function(component) {
  var action = component.get("c.getZenObjectnew");
  var self = this;
  action.setCallback(this, function(actionResult) {
    var state = actionResult.getState();
    if (component.isValid() && state === "SUCCESS") {
      var resultArr = actionResult.getReturnValue();
      if (resultArr.length > 0 && resultArr[0].Id){ //or some other property that verifies success
        var arr = component.get("v.ZenObjsnew");
        arr.push(resultArr[0]);
        component.set("v.ZenObjsnew", arr);
      } 
    }
  });
  $A.enqueueAction(action);
}

I don't even know if you are call the function, as you don't have the $A.enqueueAction(action);. Also, in your component, what calls this?

1
  • Solved the issue.Actually we cannot get single record every time.I changed the query and removed the limit of 1.Which will fetch all the records and every time i create new record the record will not be over ridden – Deepak Agarwal Apr 19 '17 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.