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 

Want to add validation in picklist Before Closing a Case

Hi folks,

I have a custom code for dependent Picklist lightning component which is inserted in the case ,like below,now my requirement is whenever user wants to close the case ,i need to display a message in my custom picklist lightning component as below

"Level 1, level 2 and level 3 field must be populated before saving."

Here is the case stages as "CLOSED" and custom picklist component screenshot

User-added image


Your help is highly apreciated

Regards,
Fiona
Best Answer chosen by fiona gentry
Abdul KhatriAbdul Khatri

Hi Fiona,

Here is what I suggest without making any changes in the  lightning component. 

 

1. On the Case Object Create a Rollup Summary Field 

User-added image

2. Use the following Validate Rule on the Case Object
User-added image

Here is the validation rule code for you convenience
 

AND( ISPICKVAL(Status, 'Closed'), EST_Case_Type_Count__c  = 0)

I hope this will help, what you are looking for

All Answers

Abdul KhatriAbdul Khatri
Hi Fiona,

Is Level 1, Level 2 and Level 3 are custom fields on the case or they are just and input field in the component. Do you know where you store those information on the Case Level?
fiona gentryfiona gentry
Yes they are fields from custom object named EST_Case_Type__c ,here is cmp file if it helps
<aura:component controller="EST_MultiLevelInsertCaseTypes" 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 event="force:refreshView" action="{!c.isRefreshed}" />
  
    <!-- variable-->
    <aura:attribute name="lstLevel1" type="String[]" />
     <aura:attribute name="lstLevel2" type="String[]"  />
      <aura:attribute name="lstL3"  type="String[]"  />
        <aura:attribute name="firstlevel1selected" type="String" default="" />
     <aura:attribute name="secondlevelselected" type="String" default="" />
      <aura:attribute name="thirdlevelselected"  type="String" default="" />
    
	   <div class="slds-container--center slds-container--small slds-m-top--small">
        <div class="slds-form--stacked">
             
            <lightning:select name="parentPicklist" label="Level 1"  aura:id="ddLevel1"  onchange="{!c.getLvl1}">
                <option value="">--- None ---</option>
                <aura:iteration items="{!v.lstLevel1}" var="item1">
                    <option  value="{!item1.value}" selected="{!item1.selected}" >{!item1}</option>
                </aura:iteration>
            </lightning:select>
             
            <lightning:select name="Level2Picklist" label="Level 2"  aura:id="ddLevel2"   onchange="{!c.getSelectedValue}" >
                <option value="">--- None ---</option>
                <aura:iteration items="{!v.lstLevel2}" var="item2">
                    <option  value="{!item2.value}" selected="{!item2.selected}" >{!item2}</option>
                </aura:iteration>
            </lightning:select>
			
			 <lightning:select name="Level3Picklist" label="Level 3"  aura:id="ddLevel3" onchange="{!c.getlevel3}"  >
                <option value="">--- None ---</option>
                <aura:iteration items="{!v.lstL3}" var="item3">
                    <option  value="{!item3.value}" selected="{!item3.selected}" >{!item3}</option>
                </aura:iteration>
            </lightning:select>
             
        </div>        
    </div>
   <lightning:button variant="brand" label="Save" onclick="{!c.onConfirm}" />
</aura:component>

here is controller. js
({ 

     

    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'); 

        component.set("v.firstlevel1selected",picklistvalue); 

        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); 

                helper.getSelectedValue(component,event,helper); 

            } 

        }); 

        $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'); 

        component.set("v.secondlevelselected",picklistvaluedep2); 

        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); 

    }, 

    getlevel3:function(component, event, helper){ 

        var picklist=component.find('ddLevel3'); 

         

        var picklistvalue=picklist.get('v.value'); 

        component.set("v.thirdlevelselected",picklistvalue); 

         

    }, 

    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' : component.get('v.secondlevelselected'), 

                          'level3' : component.get('v.thirdlevelselected'), 

                          '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": "Please Select Unique Combination", 

                        "message": "Combination Already Exists,Please Select Another Combination." 

                         

                    }); 

                    toastEvent.fire(); 

                } 

            } 

            $A.get('e.force:refreshView').fire();  

        }); 

        $A.enqueueAction(action); 

         

    }, 

    /*page refresh after data save*/     

    isRefreshed: function(component, event, helper) { 

        location.reload(); 

    } 

     

     

})
Here is apex from which data coming
public class EST_MultiLevelInsertCaseTypes {
    @AuraEnabled 
    
    public static List<String> getLevel1(){ 
        
        List<String> tempLst1 = new List<String>(); 
        
        for(AggregateResult  ar : [select Level_1__c,COUNT(id) from Case_Type_Data__c  group by Level_1__c]) 
            
        { 
            
            tempLst1.add(''+ar.get('Level_1__c')); 
            
        } 
        
        
        
        return tempLst1; 
        
        
        
        
        
    }  
    
    
    
    @AuraEnabled 
    
    public static List<String> getLevel2(string strName){ 
        
        List<String> tempLst2 = new List<String>(); 
        
        for(AggregateResult  ar : [select Level_2__c,COUNT(id) from Case_Type_Data__c where Level_1__c=:strName  group by Level_2__c]) 
            
        { 
            
            tempLst2.add(''+ar.get('Level_2__c')); 
            
        } 
        
        
        
        return tempLst2; 
        
        
        
    }  
    
    
    
    @AuraEnabled 
    
    public static List<String> getLevel3(string strName1,string strName2){ 
        
        List<String> tempLst3 = new List<String>(); 
        
        for(AggregateResult  ar : [select Level_3__c,COUNT(id) from Case_Type_Data__c  where Level_1__c=:strName1 and Level_2__c=:strName2 group by Level_3__c]) 
            
        { 
            
            tempLst3.add(''+ar.get('Level_3__c')); 
            
        } 
        
        
        
        return tempLst3; 
        
        
        
        
        
    }  
    
    
    
    @AuraEnabled 
    
    public  static String  savecasetype(string level1,string level2,string level3,string id){ 
        
        string strMsg='successfull'; 
        
        try{ 
            
            
            
            List<ERT_Case_Type__c> Lvlobj = new List<ERT_Case_Type__c>(); 
            
            
            
            Lvlobj = [SELECT id FROM ERT_Case_Type__c WHERE Case__c=:id and Level_1__c=:level1 and Level_2__c=:level2 and Level_3__c=:level3 LIMIT 10]; 
            
            if (Lvlobj.size() > 0){ 
                
                strMsg= 'Combination Already Exists,Please Select Another Combination'; 
                
                
                
            }else{ 
                
                ERT_Case_Type__c obj=new ERT_Case_Type__c(); 
                
                Obj.Case__c = id; 
                
                System.debug('CASE  = '+ Obj.Case__c);  
                
                Obj.Level_1__c=level1; 
                
                System.debug('Level1  = '+ Obj.Level_1__c);  
                
                Obj.Level_2__c=level2; 
                
                System.debug('Level2  = '+ Obj.Level_2__c);  
                
                Obj.Level_3__c=level3; 
                
                System.debug('Level3  = '+ Obj.Level_3__c);  
                
                Insert obj; 
                
            } 
            
            
            
        } 
        
        
        
        catch(Exception ex){ 
            
            strMsg='error'; 
            
        } 
        
        return strMsg;   
        
    } 
    
    
}


Thanks,
Fiona​​​​​​​
fiona gentryfiona gentry
Case has a master detail relationship with EST_Case_Type__c
Abdul KhatriAbdul Khatri

Hi Fiona,

Here is what I suggest without making any changes in the  lightning component. 

 

1. On the Case Object Create a Rollup Summary Field 

User-added image

2. Use the following Validate Rule on the Case Object
User-added image

Here is the validation rule code for you convenience
 

AND( ISPICKVAL(Status, 'Closed'), EST_Case_Type_Count__c  = 0)

I hope this will help, what you are looking for
This was selected as the best answer
Abdul KhatriAbdul Khatri
Hi Fiona,

Was the last solution working?

I would also like to help on this if not yet resolved
https://developer.salesforce.com/forums/ForumsMain?id=9062I000000IYdpQAG
Abdul KhatriAbdul Khatri
Hi,

Please let me know if my solution works or you are looking in a different way.
fiona gentryfiona gentry
Hi Abdul,
Thanks ,but still dont see the validation message,created a roll up summary then added a validation rule like 
AND( ISPICKVAL(Status, 'Closed'), ERT_Case_Type_Count__c  = 0)



User-added image
Abdul KhatriAbdul Khatri
Hi,

Which SObject you created a rollup summary? Can you share some details Also on which SObject you created a validation rule (They both should be created on the Case SObject). Any details that help me find the issue.
fiona gentryfiona gentry
rollup summary on Case object
User-added image
validation rule on case object
User-added image
will reiterate Level1 ,Level2 and Level3 are fields in ERT_Case_Type__c custtom object and Case has Master detail relationship with ERT_Case_Type__c
Abdul KhatriAbdul Khatri
Hi,

Strange so when you try to save the Case with Status = Closed and there is not record in teh ERT_Case_Type__c. You don't see any message from the validation rule. Can you check what you get in the rollup summary when you a record  and when you don't (=0)

Is this the way you child ERT_Case_Type__c custom sobject is define

User-added image
fiona gentryfiona gentry
Here is how ERT_Case_Type__c looks like

User-added image
Abdul KhatriAbdul Khatri
it worked fine for me see below when I try to close the case I get this message

User-added image

 
fiona gentryfiona gentry
There is 1 difference though,just curious ,if its preventing,the Level1,Level2,Level3 fields are picklist data type for you,whereas those are Text(100) in my case,will it make a difference?
Abdul KhatriAbdul Khatri
No, that should not make a difference because we are looking the record for that SObject.
Abdul KhatriAbdul Khatri
Hi, 
Can you check how that Rollup Summary field shows up on the UI?

User-added image
Abdul KhatriAbdul Khatri
Please let me know how I can help you.

This is most out of box solution I can think off. Any other solution will lead us to customization.
fiona gentryfiona gentry
Thank you for your continued help and reply,here is how the rollup summary looks at case page layout User-added image
fiona gentryfiona gentry
Does this validation rule needs to be attached to a profile or role,currently this Case is accessed  via App named ERT and that app has a profile defined?
fiona gentryfiona gentry
Now Worked like a charm,Thank you abdul
Abdul KhatriAbdul Khatri