function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
fiona gentryfiona gentry 

Setting dependent picklist Level2 and Level3 then resetting the Level2 at Lightning component but Level2 Cached Data is Getting Saved

Hi Gurus,

Step1--> I have a scenario where, in my dependent picklist, first i am setting values for  picklist Level2 and then selected dependent picklist value in Level3
User-added image

Step2--> Now this time removed selection at Level2 ,automatically Level3 selection also got erased and 

User-added image

Step3--> Click Save

User-added image

Here is problem,that value selected in Step2 getting saved rather than the reset value in step3

I have added code to reset values at 
ui:inputSelectOption
but it seems the reset  values from lightning components are not getting reset and saved rather cached values are getting saved after I click save,

Please help in fixing the code, here is the component code
<aura:component controller="PickListHandler" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
					<!-- Actions-->
					<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
					<aura:handler name="change" value="{!v.pageReference}"
								  action="{!c.onPageReferenceChanged}" />
					<!-- variable-->
					<aura:attribute name="lstLevel1" type="String[]" />
					 <aura:attribute name="lstLevel2" type="String[]" />
					  <aura:attribute name="lstL3" type="String[]" />
					<span> Level 1</span>
					<ui:inputSelect aura:id="ddLevel1" change="{!c.getLvl1}">
						<ui:inputSelectOption label="-Select-" value="true"/>        
						<aura:iteration items="{!v.lstLevel1}" var="value">          
							<ui:inputSelectOption label="{!value}" text="{!value}" />
						</aura:iteration>
					</ui:inputSelect>
					<span>Level 2</span>
					<ui:inputSelect aura:id="ddLevel2"  change="{!c.getSelectedValue}">
						 <ui:inputSelectOption label="-Select-" value="{!v.clrlv1}"/>        
						<aura:iteration items="{!v.lstLevel2}" var="value">          
							<ui:inputSelectOption label="{!value}" text="{!value}" />
						</aura:iteration>
					</ui:inputSelect>
					 <span>Level 3</span>
					<ui:inputSelect aura:id="ddLevel3" >
						<ui:inputSelectOption label="-Select-" value="{!v.clrlv2}"/>        
						<aura:iteration items="{!v.lstL3}" var="value">          
							<ui:inputSelectOption label="{!value}" text="{!value}" />
						</aura:iteration>
					</ui:inputSelect>
				   <lightning:button variant="brand" label="Save" onclick="{!c.onConfirm}" />
				</aura:component>

here is controller  code
({
					   reset1 : function(component, event, helper) {
							component.set("v.clrlvl", "");
						},
						reset2 : function(component, event, helper) {
							component.set("v.clrlv2", "");
						},onPageReferenceChanged: function(cmp, event, helper) {
							$A.get('e.force:refreshView').fire();
						},
						doInit : function(component, event, helper) {
							var action = component.get("c.getLevel1");
							action.setCallback(this, function(e) {
								if(e.getState()=='SUCCESS'){
									var result=e.getReturnValue();
									component.set("v.lstLevel1",result);
								}
							});
							$A.enqueueAction(action);
						},    
						getLvl1:function(component, event, helper){
						   
							var picklist=component.find('ddLevel1');
							var picklistvalue=picklist.get('v.value');
							var action = component.get("c.getLevel2");
							action.setParams({  'strName' : picklistvalue  });
							action.setCallback(this, function(e) {
								if(e.getState()=='SUCCESS'){
									var result=e.getReturnValue();
									component.set("v.lstLevel2",result);
								}
							});
							$A.enqueueAction(action);
								},
						getSelectedValue:function(component, event, helper){
							var picklist=component.find('ddLevel1');
							var picklistvalue=picklist.get('v.value');
							var picklistdep=component.find('ddLevel2');
							var picklistvaluedep2=picklistdep.get('v.value');
							var action = component.get("c.getLevel3");
							action.setParams({  'strName1' : picklistvalue,
											 'strName2' : picklistvaluedep2});//
							action.setCallback(this, function(e) {
								if(e.getState()=='SUCCESS'){
									var result=e.getReturnValue();
									component.set("v.lstL3",result);
								}
							});
							$A.enqueueAction(action);
						},
						onConfirm:function(component, event, helper){
							var picklist=component.find('ddLevel1');
							var picklistvalue=picklist.get('v.value');
							var picklistdep=component.find('ddLevel2');
							var picklistvaluedep2=picklistdep.get('v.value');
						   
							var picklistdep3=component.find('ddLevel3');
							var picklistvaluedep3=picklistdep3.get('v.value');
							var action = component.get("c.savecasetype");
							
							action.setParams({  'level1' : picklistvalue,
											  'level2' : picklistvaluedep2,
											  'level3' : picklistvaluedep3,
											  'id' : component.get("v.recordId")});
											  
							
							var toastEvent = $A.get("e.force:showToast");
							action.setCallback(this, function(e) {
								if(e.getState()=='SUCCESS'){
									var result=e.getReturnValue();
									if(result==='successfull'){
										toastEvent.setParams({
											"title": "Success!",
											"message": "The record has been inserted  successfully."
										});
										toastEvent.fire();
									}else{
										toastEvent.setParams({
											"title": "Error",
											"message": "The record has not been inserted  successfully."
										});
										toastEvent.fire();
									}
								}
							});
							$A.enqueueAction(action);
						   
						}
					})

Your help is highy appreciated

Regards,
Fiona

 
Best Answer chosen by fiona gentry
ANUTEJANUTEJ (Salesforce Developers) 
>> https://developer.salesforce.com/forums/?id=9060G0000005pt3QAA

As shown in this link you can use the onchangecontroller method, below is the method for quick reference:
 
onControllerFieldChange: function(component, event, helper) {     
        var controllerValueKey = event.getSource().get("v.value"); // get selected controller field value
        var depnedentFieldMap = component.get("v.depnedentFieldMap");
        
        if (controllerValueKey != '--- None ---') {
            // disable and reset sub dependent field 
           
           
            var ListOfDependentFields = depnedentFieldMap[controllerValueKey];
            
            if(ListOfDependentFields.length > 0){
                component.set("v.bDisabledDependentFld" , false);  
                helper.fetchDepValues(component, ListOfDependentFields,"v.listDependingValues");    
            }else{
                component.set("v.bDisabledDependentFld" , true); 
                component.set("v.listDependingValues", ['--- None ---']);
            }  
            
        } else {
            component.set("v.listDependingValues", ['--- None ---']);
            component.set("v.bDisabledDependentFld" , true);
        }
        
        component.set("v.bDisabledSubDependentFld" , true);
        component.set("v.listSubDependingValues", ['--- None ---']);
    }

I think you should check the above link once.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Fiona,

I think before submitting you can have a final check where if the second field is empty and if the third is not then you can remove the third value and submit it.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
 
fiona gentryfiona gentry
how to do that?
ANUTEJANUTEJ (Salesforce Developers) 
>> https://developer.salesforce.com/forums/?id=9060G0000005pt3QAA

As shown in this link you can use the onchangecontroller method, below is the method for quick reference:
 
onControllerFieldChange: function(component, event, helper) {     
        var controllerValueKey = event.getSource().get("v.value"); // get selected controller field value
        var depnedentFieldMap = component.get("v.depnedentFieldMap");
        
        if (controllerValueKey != '--- None ---') {
            // disable and reset sub dependent field 
           
           
            var ListOfDependentFields = depnedentFieldMap[controllerValueKey];
            
            if(ListOfDependentFields.length > 0){
                component.set("v.bDisabledDependentFld" , false);  
                helper.fetchDepValues(component, ListOfDependentFields,"v.listDependingValues");    
            }else{
                component.set("v.bDisabledDependentFld" , true); 
                component.set("v.listDependingValues", ['--- None ---']);
            }  
            
        } else {
            component.set("v.listDependingValues", ['--- None ---']);
            component.set("v.bDisabledDependentFld" , true);
        }
        
        component.set("v.bDisabledSubDependentFld" , true);
        component.set("v.listSubDependingValues", ['--- None ---']);
    }

I think you should check the above link once.
This was selected as the best answer