0

I am not able to show value which is in the record empRecord.State__c

even thought the logs show that it is retrieving properly.

Here is my code -

                        <lightning:select value="{!v.empRecord.State__c}" label="{!$Label.c.State}" required="true">
                            <aura:iteration items="{!v.picState}" var="State">
                                <option value="{!v.empRecord.State__c}" selected="{!v.empRecord.State__c}">{!State}</option>
                            </aura:iteration> 
                        </lightning:select>

Code to fetch values --

loadPicklistStates : function(component,event) {
    var action = component.get("c.getStates");       
    action.setCallback(this, function(response) {
        var state = response.getState();
        console.log('state-->'+state);
        if (state === "SUCCESS") {
            var resList = response.getReturnValue();
            console.log('resList States-->'+resList);
            component.set("v.picState",resList);
            console.log('Contact after new state-->'+component.get("v.empRecord.State"));
            console.log('Contact after new State__c-->'+component.get("v.empRecord.State__c"));
            component.set("v.showSpinner",false);


        }
        else if (state === "INCOMPLETE") {
            // do something
        }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        component.set("v.isError",true);
                        component.set("v.errorMessage","Error message: " + errors[0].message);
                        console.log("Error message: " + 
                                    errors[0].message);
                    }
                } else {
                    component.set("v.isError",true);
                    component.set("v.errorMessage","Unknown error");
                    console.log("Unknown error");
                }
            }
    });

    $A.enqueueAction(action);
}

Passing for update--

updateEmployee: function(component,event){ 
    var action = component.get("c.updateEmployeeDependent");
    console.log('emprecord--->'+JSON.stringify(component.get("v.empRecord")));
    action.setParams({con:component.get("v.empRecord")});
    action.setCallback(this, function(response) {
        var state = response.getState();
        console.log('state-->'+state);
        if (state === "SUCCESS") {
            console.log('Dependent updated');
            var depName = component.get("v.empRecord.Name");
            component.set("v.showSpinner",false);
            // Success! Prepare a toast UI message
            var resultsToast = $A.get("e.force:showToast");
            resultsToast.setParams({
                "title": "Employee Updated",
                "message": depName + " Updated"
            });

            // Update the UI: close panel, show toast, refresh account page
            $A.get("e.force:closeQuickAction").fire();
            resultsToast.fire();

            // Reload the view so components not using force:recordData
            // are updated
            $A.get("e.force:refreshView").fire();
            component.set("v.showSpinner",false);
        }
        else if (state === "INCOMPLETE") {
            // do something
        }
            else if (state === "ERROR") {
                var errors = response.getError();

                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                    errors[0].message);
                        var errMessage='';
                        if(errors[0].message!=undefined && errors[0].message.includes('FIELD_CUSTOM_VALIDATION_EXCEPTION')){
                            console.log('Inside includes');
                            errMessage = errors[0].message.match(/FIELD_CUSTOM_VALIDATION_EXCEPTION,\s*([^:]+)/i)[1];
                        }else{
                            errMessage =errors[0].message;
                        }
                        console.log('errMessage-->'+errMessage);
                        component.set("v.isError",true);
                        console.log('After setting is error');
                        component.set("v.errorMessage",errMessage);
                        console.log('Aftersetting message');

                        component.set("v.showSpinner",false);
                    }
                } else {
                    component.set("v.isError",true);
                    component.set("v.errorMessage","Unknown error");
                    console.log("Unknown error");
                }
            }
    });

    $A.enqueueAction(action);
}

Here before passing the parameter, state is not updating

1

Please update your lightning component with below code because you are specifying each option value with current emprecord state value, that's why when you submit the value it will not give the selected value it will always give the value which specified in value attribute of option tag.

 <lightning:select value="{!v.empRecord.State__c}" label="{!$Label.c.State}" required="true">
     <aura:iteration items="{!v.picState}" var="State">
       <option value="{!State}" selected="{!State == v.empRecord.State__c}">{!State}</option>
     </aura:iteration> 
</lightning:select>
  • Actually that's solves the issue, bit there is one more issue.. while fetching value it is not showing the right value which is in v.empRecord.State__c even when I set it in selected attribute. – mvk1991 Sep 6 '19 at 8:31
  • Please remove the selected attribute attribute from option tag. The value specified in lightning:select tag will automatically select the correct option. – manish porwal Sep 6 '19 at 8:45
  • Removed. didn't work.also tried to replace value with v.State.. no luck. – mvk1991 Sep 6 '19 at 9:01
  • Got it working -- <option value="{!v.State}" selected="{!State==v.empRecord.State__c}">{!State}</option> – mvk1991 Sep 6 '19 at 9:09
  • But it breaks the original issue – mvk1991 Sep 6 '19 at 9:12

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.