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
Francesca Ribezzi 2Francesca Ribezzi 2 

Issues with feeds - Lightning

Hello everyone! I am encountering some issues with feeds in Lightning Components: when I submit my form, the new feed values are showed below it but seem to replace the previous ones!! 
Can anyone help me, please?

Here's my code!

Thank you very much,

Francesca

 
//Commenti.cmp

<aura:component implements="force:hasRecordId" controller="CommentiController">
    <aura:attribute name="newComment" type="Commenti__c"/>
    <aura:attribute name="simpleNewComment" type="Commenti__c"    default="{ 'sobjectType': 'Commenti__c',
                                                                           'Name': '',
                                                                           'Username__c': '',
                                                                           'Feedback__c': ''
                                                                           }"/>
    

    <aura:attribute name="listaCommenti" type="Commenti__c[]"/>
     <aura:attribute name="newCommentError" type="String"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> 
    

    <force:recordData aura:id="commentRecordCreator"
                      
                      layoutType="FULL"
                      
                      targetRecord="{!v.newComment}"
                      
                      targetFields="{!v.simpleNewComment}"
                      
                      targetError="{!v.newCommentError}"
                      
                     mode="EDIT"
                   />    
    

    
    <div class="container">
        <h1 class="titolo">Lascia un Commento</h1>
        <div aria-labelledby="newexpenseform">
            
            <fieldset class="slds-box slds-theme--default slds-container--small">
                <legend id="newexpenseform" class="slds-text-heading--small 
                                                   slds-p-vertical--medium">
                    Give us a feedback! 
                </legend>
                
                
                <form class="slds-form--stacked">    
                    
                    <lightning:input aura:id="simpleNewCommentForm" label="Your Name"
                                     name="contactname"
                                     value="{!v.simpleNewComment.Name}"
                                     required="true"/> 
                    
                    <lightning:input aura:id="simpleNewCommentForm" label="Your Username"
                                     name="contactusername"
                                     value="{!v.simpleNewComment.Username__c}"
                                     required="true"/> 
                    
                    
                    <lightning:input aura:id="simpleNewCommentForm" label="Write your comment here"
                                     name="feedback"
                                     value="{!v.simpleNewComment.Feedback__c}"
                                     required="true"/> 
                    <lightning:button label="Submit!" 
                                      class="slds-m-top--medium"
                                      variant="brand"
                                      onclick="{!c.handleSaveContact}"/>
                </form>
            </fieldset>
        </div>
        
        
        <div class="slds-feed">
            
            <ul class="slds-feed__list">
                
                <aura:if isTrue="{! !empty(v.listaCommenti)}"> 
                <aura:iteration items="{!v.listaCommenti}" var="commento"> 
                    <li class="slds-feed__item">
                        <article class="slds-post">
                            <header class="slds-post__header slds-media">
                                <div class="slds-media__figure">
                                    <a href="javascript:void(0);" class="slds-avatar slds-avatar_circle slds-avatar_large">
                                <!--        <img alt="Immagine User" src="{!commento.User__r.BannerPhotoUrl}" title="Immagine User" /> -->
                                    </a>
                                </div>
                                <div class="slds-media__body">
                                    <div class="slds-grid slds-grid_align-spread slds-has-flexi-truncate">
                                        <p><a href="javascript:void(0);" title="Username">{!commento.Username__c}</a> — <a href="javascript:void(0);" title="Company Name">{!commento.User__c}</a></p>
                                        
                                    </div>
                                    <p class="slds-text-body_small"><a href="javascript:void(0);" title="Click for single-item view of this post" class="slds-text-link_reset">{!commento.CreatedDate}</a></p>
                                </div>
                            </header>
                            <div class="slds-post__content slds-text-longform">
                                <p>{!commento.Feedback__c}</p>
                            </div>
                            <footer class="slds-post__footer">
                                <ul class="slds-post__footer-actions-list slds-list_horizontal">
                                    
                                    <li class="slds-col slds-item slds-m-right_medium">
                                        <lightning:buttonIcon iconName="utility:success" alternativeText="Like"/>
                                    </li>
                                    <li class="slds-col slds-item slds-m-right_medium">
                                        
                                        <a href="javascript:void(0);" title="Comment"> Comment</a>
                                    </li>
                                    
                                    
                                </ul>
                                
                            </footer>
                        </article>
                        
                    </li>
                </aura:iteration> 
                    </aura:if>
            </ul>
        </div>  
        
    </div>            
</aura:component>
 
//CommentiController.js

({
    doInit: function(component, event, helper) {
        
        helper.getListaCommenti(component, event);
       helper.getNewRecord(component, event);
    },
    
    
    handleSaveContact: function(component, event, helper) {
        
        var userId = $A.get("$SObjectType.CurrentUser.Id");
        console.log("USERID: " + userId);

        var simpleNewComment =  component.get("v.simpleNewComment");
        component.set("v.simpleNewComment.User__c",userId);
        component.find("commentRecordCreator").saveRecord(function(saveResult) {
            if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
                
     
                var rec = component.get("v.newComment");
                
                var listaCommenti = component.get("v.listaCommenti");
                listaCommenti.push(rec);
                
                //   listaCommenti.unshift(recordData);
                component.set("v.listaCommenti", listaCommenti);
                console.log("record is saved successfully"+JSON.stringify(rec));
   

                console.log("SUCCESS "+ JSON.stringify(saveResult));
                
                       helper.getListaCommenti(component, event);
            } else if (saveResult.state === "INCOMPLETE") {
                // handle the incomplete state
                console.log("User is offline, device doesn't support drafts.");
            } else if (saveResult.state === "ERROR") {
                // handle the error state
                console.log('Problem saving contact, error: ' + JSON.stringify(saveResult.error));
            } else {
                console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
            }
        });
        
        
        helper.getNewRecord(component, event);
        
    },
 
//CommentiHelper.js

({

    getListaCommenti: function(component, event){
        var action = component.get("c.getCommenti");
        action.setCallback(this, function(a) {
            
            
            component.set("v.listaCommenti", a.getReturnValue());
            console.log("**"+a.getReturnValue());
        });
        $A.enqueueAction(action);
  
        
        
    },
    
     getNewRecord : function(component, event) {
        // Prepare a new record from template
        component.find("commentRecordCreator").getNewRecord(
            "Commenti__c", // sObject type (objectApiName)
            null,      // recordTypeId
            false,     // skip cache?
            $A.getCallback(function() {
                var rec = component.get("v.newComment");
                var error = component.get("v.newCommentError");
                if(error || (rec === null)) {
                    console.log("Error initializing record template: " + error);
                    return;
                }
                console.log("Record template initialized: " + rec.sobjectType);
            })
        );
    },
    
  
    
})

 
Best Answer chosen by Francesca Ribezzi 2
Alain CabonAlain Cabon
Hello,

You need a $A.getCallback. 
 
public class CommentiController {
    @AuraEnabled
    static public Commenti__c[] getCommenti() {       
        return [select id, Name,Username__c,Feedback__c from Commenti__c order by createddate desc];   
    }
}
 
({
    doInit: function(component, event, helper) {       
        helper.getListaCommenti(component, event);
        helper.getNewRecord(component, event);
    },
    handleSaveContact: function(component, event, helper) {
        
        var userId = $A.get("$SObjectType.CurrentUser.Id");
        console.log("USERID: " + userId);
        
        var simpleNewComment =  component.get("v.simpleNewComment");
        component.set("v.simpleNewComment.User__c",userId);
        
        component.find("commentRecordCreator").saveRecord($A.getCallback(function(saveResult) {
           
            if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT" ) {
               
                helper.getListaCommenti(component, event);
                helper.getNewRecord(component, event);
               
            } else if (saveResult.state === "INCOMPLETE") {
                console.log("User is offline, device doesn't support drafts.");
            } else if (saveResult.state === "ERROR") {
                console.log('Problem saving record, error: ' + JSON.stringify(saveResult.error));
            } else {
                console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
            }
        }));
    },
})

Alain

All Answers

Alain CabonAlain Cabon
Hello,

You need a $A.getCallback. 
 
public class CommentiController {
    @AuraEnabled
    static public Commenti__c[] getCommenti() {       
        return [select id, Name,Username__c,Feedback__c from Commenti__c order by createddate desc];   
    }
}
 
({
    doInit: function(component, event, helper) {       
        helper.getListaCommenti(component, event);
        helper.getNewRecord(component, event);
    },
    handleSaveContact: function(component, event, helper) {
        
        var userId = $A.get("$SObjectType.CurrentUser.Id");
        console.log("USERID: " + userId);
        
        var simpleNewComment =  component.get("v.simpleNewComment");
        component.set("v.simpleNewComment.User__c",userId);
        
        component.find("commentRecordCreator").saveRecord($A.getCallback(function(saveResult) {
           
            if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT" ) {
               
                helper.getListaCommenti(component, event);
                helper.getNewRecord(component, event);
               
            } else if (saveResult.state === "INCOMPLETE") {
                console.log("User is offline, device doesn't support drafts.");
            } else if (saveResult.state === "ERROR") {
                console.log('Problem saving record, error: ' + JSON.stringify(saveResult.error));
            } else {
                console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
            }
        }));
    },
})

Alain
This was selected as the best answer
Francesca Ribezzi 2Francesca Ribezzi 2
Thank you Alain! Now it's working!! :-))