0

Is there a way to append the field of an aura attribute?

<aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId" description="AddBoatReview">
<aura:attribute name="boat" type="Boat__c"/>
<aura:attribute name="boatReview" type="BoatReview__c" access="private"
                default="{'sobjectType':'BoatReview__c', 'Name':'', 'Comment__c':''}"/>
    <aura:attribute name="record" type="BoatReview__c" access="private"/>
    <aura:attribute name="recordError" type="String" access="private"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <force:recordData aura:id="service"
                      fields="Id,Name,Comment__c"
                      targetError="{!v.recordError}"
                      targetRecord="{!v.record}"
                      targetFields="{!v.boatReview}"
                      recordUpdated="{!c.onRecordUpdated}"/>
</aura:component>

Controller:

I want to append the boat__C , field of the targetFields Object. I tried doing it by component.set(("v.boatReview").boat__c,component.get("v.boat").Id); but that did not work. How can I update the boat__C field in boatReview attribute?

component.find("service").getNewRecord(
    "BoatReview__c", // sObject type (objectApiName)
    null,      // recordTypeId
    false,     // skip cache?
    $A.getCallback(function() {
        var rec = component.get("v.record");
        var error = component.get("v.recordError");
        if(error || (rec === null)) {
            console.log("Error initializing record template: " + error);
            return;
        }
        console.log("Record template initialized: " + JSON.stringify(rec));

        component.set(("v.boatReview").boat__c,component.get("v.boat").Id);


    })

2 Answers 2

1

What I used once that worked perfectly, was first saving the object I wanted to append an attribute to, then setting with dot notation, and finally setting the attribute back to the component, like so:

var boatReview = component.get("v.boatReview");
boatReview.Boat__c = component.get("v.boat").Id;
component.set("v.boatReview", boatReview);

This has the advantage of triggering "onchange" events on the boatReview attribute, if you need to.

0

Have hitting my head several times I was able to figure this out:

Use - component.set("v.boatReview.Boat__c", component.get("v.boat").Id);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .