I've been following the Salesforce documentation on using the force:recordData component to instantiate a new instance of a Case object (targetRecord) using component.getNewRecord
, then pushing data into the targetFields object with the intent of ultimately saving a new Case record using component.saveRecord
.
Issue: The record saves successfully, but only the Subject and Description are saved, the Issue_Type__c and Business_Is_Down__c fields are not being populated. They aren't even showing in the console.log when I convert
simpleNewCase
to a JSON string to review it.I don't understand why these values aren't being stored/saved with the rest of the record, and the documentation doesn't make any special mention of handling custom fields differently.
Below is all of my code and the steps I've taken to get to this point
I created 3 attributes, same as the user guide:
<aura:attribute name="newCase" type="Object" />
<aura:attribute name="simpleNewCase" type="Object" />
<aura:attribute name="newCaseError" type="String" />
I put the recordData component in my .cmp:
<force:recordData aura:id="caseRecordCreator"
layoutType="FULL"
targetRecord="{!v.newCase}"
targetFields="{!v.simpleNewCase}"
targetError="{!v.newCaseError}" />
I instantiate the new object during my init actions:
init: function (component, event, helper) {
component.find("caseRecordCreator").getNewRecord(
"Case", // sObject type (objectApiName)
null, // recordTypeId
false, // skip cache?
$A.getCallback(function() {
var rec = component.get("v.newCase");
var error = component.get("v.newCaseError");
if(error || (rec === null)) {
console.log("Error initializing record template: " + error);
return;
}
console.log("Record template initialized: " + rec.apiName);
})
);
},
I then set some values on the simpleNewCase
attribute based on data from the page and invoke saveRecord
on the object:
save: function (component, event, helper) {
console.log('Invoke Save On Click');
var subject = component.get("v.subjectVal");
var description = component.get("v.descriptionVal");
var platform = component.get("v.platformVal");
var systemDown = component.get("v.systemDownVal");
console.log('Save on Click - Subject: ' + subject + ' | Description: ' + description + ' | Platform: ' + platform + ' | System Down: ' + systemDown);
component.set("v.simpleNewCase.Subject", subject);
component.set("v.simpleNewCase.Description", description);
component.set("v.simpleNewCase.Issue_Type__c", platform);
component.set("v.simpleNewCase.Business_Is_Down__c", systemDown);
console.log('Record: ' + JSON.stringify(component.get("v.simpleNewCase")));
component.find("caseRecordCreator").saveRecord(function(saveResult) {
if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
// record is saved successfully
var resultsToast = $A.get("e.force:showToast");
resultsToast.setParams({
"title": "Saved",
"message": "The record was saved."
});
resultsToast.fire();
} 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));
}
});
},
This is the output I get in my console:
Save on Click - Subject: Test Subject | Description: Test Description | Platform: Platform A | System Down: No
Record: {"Contact":null,"ContactId":null,"Description":"Test Description","Owner":{"Id":"REDACTED","Name":"Morgan Marchese"},"OwnerId":"REDACTED","RecordType":{"Id":"REDACTED","Name":"Customer Community Site"},"RecordTypeId":"REDACTED","Status":"New","Subject":"Test Subject","LastModifiedDate":null,"SystemModstamp":null,"CreatedDate":null,"ContactEmail":null,"ClosedDate":null,"Id":null}
without sharing
? Or will I need to pass the created component/object to a custom save method in the handler?without sharing
unless and until really required. Because LDS honors the FLS, if at all you want to "override" the behavior, you need to go a custom route.