4

Below is my lightning component bundle and apex controller code. A record may have blank values in SLA__c field. Purpose of this component is to update this field.

Apex Controller:

public with sharing class AccountListController {
    @AuraEnabled
    public static List<Account> getAccountList() {
        return [SELECT Id,Name,SLA__c FROM Account];
    }
}

Component Markup:

<aura:component controller="AccountListController">
    <aura:attribute name="accounts" type="Account[]" />
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>

    <h2>Account Details</h2>

        <ul class="list-group">
            <li class="list-group-item">
                <div class="form-inline">            
                    <aura:iteration items="{!v.accounts}" var="account">
                       <ui:inputText value="{!account.SLA__c}" required="true"/>
                        <button type="button" class="btn btn-primary mr-2" onclick="{!c.myAction}">Update</button>

                    </aura:iteration>  
                </div>
            </li>   
    </ul>   
</aura:component>

JS Controller:

({
    init : function(component, event, helper) {
        console.log('in init');
        var action = component.get("c.getAccountList");
        action.setCallback(this, function(response) {
           var state = response.getState();
            if (component.isValid() && state === 'SUCCESS') {
                console.log('resp-'+response.getReturnValue());
                component.set("v.accounts", response.getReturnValue());
            }
        });

        $A.enqueueAction(action);
    },

    myAction : function(component, event, helper) {
        var accounts = component.get('v.accounts');
        for(var i=0; i<accounts.length;i++)
        console.log('@@@-'+accounts[i].SLA__c);
        /*account.Name = 'New Name';
        account.isSelected = false;
        component.set('v.account', account);
        console.log(account);*/
    }   
})

Problem:

Scenario-1 - When a user updates blank SLA__c field on a record to some value and clicks on update button, in my JS controller action method updated value is not received for that record. In console it always displays undefined.

Scenario-2 - If a record already has some value in SLA__c field and user updates it to a new value then new values is received in the JS controller action method.

Has anyone encountered the same problem? How to solve it?

  • 1
    Feel your pain. At least you can use sObjects as attributes, I cannot event do that cause I get a GACK when trying to pass back to apex controller. But I digress.....Is locker service enabled? If so try disabling it. I know when using JSON I have to explicitly set each property to '' in order to have it be usable in lightning. I have a post here about it but not sure it is relevant until you answer the locker service question – Eric Feb 19 '17 at 17:31
  • @Eric - No, locker service is not enabled. Please share your post URL. – doga Feb 20 '17 at 0:46
  • May not be relevant since locker is not enabled but: salesforce.stackexchange.com/questions/156539/… – Eric Feb 20 '17 at 0:53
  • @Eric - Is it happening because JSON string returned from server-side doesn't even contain that field? – doga Feb 20 '17 at 1:15
  • Not sure. U r using sObject I am using string. In my case yes it is a bug / known issue according to Doug in the comments – Eric Feb 20 '17 at 1:17
3

I had the similar issue with ui:inputCheckbox, the workaround I did is to iterate over the records and set the default value to false.

For your case, iterate over the account records and set a default value to SLA__c on records with no value for SLA__c and create a new instance of the array by doing JSON.stringify and JSON.parse respectively.

if (component.isValid() && state === 'SUCCESS') {
    var accounts = response.getReturnValue();
    accounts.map(function(acc){
        if(!acc.SLA__c){
            acc.SLA__c = '';
        }
    })
    component.set("v.accounts", JSON.parse(JSON.stringify(accounts)));
}

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.