0

I have a field, Region__c, on my Case object that is a picklist. I am trying to set this field in JS using Lightning Data Service. When using component, set it doesn't seem to be sticking. Any ideas on what I am doing wrong here?

Component.html

 <!-- Case creator -->
 <aura:attribute name="caseRecordId" type="Id" default="{}"/>
 <aura:attribute name="simpleNewCase" type="Case"/>
 <aura:attribute name="newCaseError" type="String"/>
 <aura:attribute name="newCase" type="Case" default="{'sobjectType':'Case',
                                 'Status':'New',                                                            
                                 'Policy_needs_moved_from_Emerald_City__c':false, 
                                 'Policy_Number__c':null,
                                 'Renewal_Effective_Date__c':null,                                            
                                 'Named_Insured__c':null,                                                    
                                 'Transfer_to_Agency_Number__c':null}"/>
 <force:recordData aura:id="caseCreator"
        layoutType="FULL"
        targetRecord="{!v.newCase}"
        targetFields="{!v.simpleNewCase}"
        targetError="{!v.newCaseError}"
        recordId="{!v.caseRecordId}"
        mode="EDIT"
        recordUpdated="{!c.recordUpdated}"/>

Controller.js:

    doInit: function(component) {
        var id = component.get("v.id");
        // Prepare a new record from template for service request
        component.find("caseCreator").getNewRecord(
            "Case",
            id,
            false,
            $A.getCallback(function() {
                var rec = component.get("v.newCase");
                var error = component.get("v.newCaseError");
                if (error || $A.util.isUndefinedOrNull(rec)) {
                    return;
                }
            })
        );
    component.set("v.simpleNewCase.Region__c", "North Division");
    console.log(component.get("v.simpleNewCase.Region__c");
    }

I am 100% certain that this is an active option for this field but when I log after setting it shows as undefined.

  • Is the missing " a typo? – sfdcfox May 17 at 17:34
  • whoops just typo on this post ty! – Landen May 17 at 17:39
  • Can we get some more code? Not much, just enough to see how things are set up – Sebastian Kessel May 17 at 17:41
  • Sure I can edit the post and add some. There is really not much to it. I am thinking it just doesn't want to accept a string as a var type but I am not sure. – Landen May 17 at 17:43
  • what are you getting in this line, console.log(component.get("v.simpleNewCase.Region__c");? – Anurag A May 17 at 18:40
0

If you read documentation here you need to call getNewRecord method to get a new instance of case record which is what you have done but you need to set the value of region field within the callback function once the response to this comes back from server

You were previously setting it outside which means this will get executed before callback and since the new case instance is not there it wont work.

$A.getCallback(function() {
var error = component.get("v.newCaseError");
if (error || $A.util.isUndefinedOrNull(rec)) {
return;
}
component.set("v.simpleNewCase.Region__c", "North Division");
console.log(component.get("v.simpleNewCase.Region__c");
})
  • Thanks for the reply. My SF environment is down right now but I will check this solution when it gets back up. – Landen May 17 at 19:44
  • While this solution did help the real issue was that this field was not on the layout. It needed to be added to a fields attribute on the force:recordData tag like so fields="Region__c". – Landen May 17 at 23:55
0

As mentioned in the comment of the other answer; the setting of the variable needs to be done in the callback or sometime after the callback has fully completed.

However, my issue was that the Region__c field was not on the Case layout. Meaning there was no Region__c in the simpleNewCase json object to set. To solve this the field needs to be explicitly called out in a fields attribute on the force:recordData tag in the markup.

 <force:recordData aura:id="caseCreator"
        layoutType="FULL"
        targetRecord="{!v.newCase}"
        targetFields="{!v.simpleNewCase}"
        **fields="Region__c"**
        targetError="{!v.newCaseError}"
        recordId="{!v.caseRecordId}"
        mode="EDIT"
        recordUpdated="{!c.recordUpdated}"/>

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.