Skip to main content
I am new to Salesforce development and trying to use the Data Service to save changes to a custom pickList field (Status__c) on a custom object (Celebrity__c) using a new component. Celebrity__c has one custom field - Status__c - a picklist field with the following options ['A-List', 'D-List', 'Z-List'].

I am seeing some cross communication when editing records with this component: the first record saves the change as expected, going into a second record loads the correct status value in the component but when attempting to save a status change, the change is saved against the first record. I have tried this with a simpler custom text field  on a different object and the changes are saved against the correct object. Can anyone spot if there is an error in the code below?

Component

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >

<aura:attribute name="record" type="Celebrity__c"/>

<aura:attribute name="simpleRecord" type="Object"/>

<aura:attribute name="recordError" type="String"/>

<aura:attribute name="statusOptions" type="List"

default="['A-List', 'D-List', 'Z-List']"/>

<force:recordData aura:id="recordLoader"

recordId="{!v.recordId}"

targetFields="{!v.simpleRecord}"

fields="Id,Name,Status__c"

mode="EDIT"

targetError="{!v.recordError}"

/>

<lightning:card class="slds-text-heading_small" title="Simple Status" iconName="utility:broadcast">

<div class="slds-p-left--medium slds-p-right--medium">

<fieldset class="slds-form-element">

<div class="slds-form-element__control">

<p>

<lightning:formattedText title="name"

value="{!v.simpleRecord.Name}"/>

<lightning:formattedText title="status"

value="{!v.simpleRecord.Status__c}"/>

</p>

<div class="slds-radio_button-group">

<aura:iteration items="{!v.statusOptions}" var="opt">

<span class="slds-button slds-radio_button">

<input name="radio" type="radio" id="{!opt}"

value="{!opt}" onclick="{!c.handleStatusSelected}"

checked="{!v.simpleRecord.Status__c == opt}"/>

<label class="slds-radio_button__label" for="{!opt}">

<span class="slds-radio_faux">

<lightning:formattedText title=""

value="{!opt}"/>

</span>

</label>

</span>

</aura:iteration>

</div>

</div>

</fieldset>

</div>

</lightning:card>

</aura:component>

Controller

({

handleStatusSelected : function(component, event, helper) {

var newStatus = event.target.value;

component.set("v.simpleRecord.Status__c", newStatus);

component.find("recordLoader").saveRecord($A.getCallback(function(saveResult) {

if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {

console.log('SAVED OK');

} 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));

}

}));

}

})

 
1 件の回答
  1. 2023年4月17日 10:28
    The issue seems to be with using the simpleRecord attribute to hold the record data instead of record. As record is the attribute that is being used to store the record data retrieved by force:recordData, changes made to simpleRecord will not reflect the correct record ID.

    To fix this issue, replace all instances of simpleRecord with record in the component markup and controller code.

    Here's the modified component code: Source (https://celebrities.com.pk/

    )

    <aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >

    <aura:attribute name="record" type="Celebrity__c"/>

    <aura:attribute name="recordError" type="String"/>

    <aura:attribute name="statusOptions" type="List"

    default="['A-List', 'D-List', 'Z-List']"/>

    <force:recordData aura:id="recordLoader"

    recordId="{!v.recordId}"

    targetFields="{!v.record}"

    fields="Id,Name,Status__c"

    mode="EDIT"

    targetError="{!v.recordError}"

    />

    <lightning:card class="slds-text-heading_small" title="Simple Status" iconName="utility:broadcast">

    <div class="slds-p-left--medium slds-p-right--medium">

    <fieldset class="slds-form-element">

    <div class="slds-form-element__control">

    <p>

    <lightning:formattedText title="name"

    value="{!v.record.Name}"/>

    <lightning:formattedText title="status"

    value="{!v.record.Status__c}"/>

    </p>

    <div class="slds-radio_button-group">

    <aura:iteration items="{!v.statusOptions}" var="opt">

    <span class="slds-button slds-radio_button">

    <input name="radio" type="radio" id="{!opt}"

    value="{!opt}" onclick="{!c.handleStatusSelected}"

    checked="{!v.record.Status__c == opt}"/>

    <label class="slds-radio_button__label" for="{!opt}">

    <span class="slds-radio_faux">

    <lightning:formattedText title=""

    value="{!opt}"/>

    </span>

    </label>

    </span>

    </aura:iteration>

    </div>

    </div>

    </fieldset>

    </div>

    </lightning:card>

    </aura:component>

    And the modified controller code:

    ({

    handleStatusSelected : function(component, event, helper) {

    var newStatus = event.target.value;

    component.set("v.record.Status__c", newStatus);

    component.find("recordLoader").saveRecord($A.getCallback(function(saveResult) {

    if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {

    console.log('SAVED OK');

    } 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));

    }

    }));

    }

    })

0/9000