+ Start a Discussion
santhiya duraisanthiya durai 

My Lightning component is not working when validation rule is active why?

Hi ,
I have created a Lightning component and I am calling that from Quick action.
I have a validation rule that will throw an error and restrict a user to make changes on a record when Record_Locked field is true. But When validation rule is Active, Success part is not working instead else part is working. If I deactivated a validation rule then IF part is working fine.
Can anyone help me on this if I missed anything?

Class: 

public class ArchiveRecord {

    @AuraEnabled
    public static void getItemSetupForm(Id itemsetupId){
        Item_Setup__c itemsetup= [Select Id, Name,Processed__c,Item_Setup_Form_Status__c from Item_Setup__c where Id=:itemsetupId and Item_Setup_Form_Status__c!='Archived'];
        itemsetup.Item_Setup_Form_Status__c='Archived';
        update itemsetup;
        
        if(itemsetup.Item_Setup_Form_Status__c=='Archived'){
          itemsetup.Name=itemsetup.Name +'_Archived';
        }
        upsert itemsetup;
}

}

Component:
<aura:component controller="ArchiveRecord" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickActionWithoutHeader" access="global">
   <aura:html tag="style">
        .cuf-content {
        padding: 0 0rem !important;
        }
        .slds-p-around--medium {
        padding: 0rem !important;
        }       
        .slds-modal__content{
        overflow-y:hidden !important;
        height:unset !important;
        max-height:unset !important;
        }
    </aura:html>
    
    <aura:attribute name="showSpinner" type="boolean" />
    <aura:attribute name="accessFlag" type="boolean"/>
    <aura:attribute name="noAccessFlag" type="boolean"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:if isTrue="{!v.accessFlag}">
     <div class="modal-header slds-modal__header slds-size_1-of-1">
        <h4 class="title slds-text-heading--medium">Archive</h4>
    </div>
    <!--End Modal Header--> 
     
    <!--Modal Body-->  
    <div class="slds-modal__content slds-p-around--x-large slds-align_absolute-center slds-size_1-of-1 slds-is-relative">
        <form class="slds-form--stacked">
           
                <div class="slds-align_absolute-center">
                    <b>Do you want to Archive this record?</b>
                </div>            
            
        </form> 
    </div>
    <!--End of Modal Body-->
     
    <!--Modal Footer-->
    <div class="modal-footer slds-modal__footer slds-size_1-of-1">
        <lightning:button variant="Neutral" class="slds-button" label="Cancel" onclick="{!c.cancelBtn}"/>
        <lightning:button variant="Brand" class="slds-button" label="Continue" onclick="{!c.updateStatus}"/>
        
    </div>
        
    </aura:if>
    
     <aura:if isTrue="{!v.noAccessFlag}">
         
     <div class="modal-header slds-modal__header slds-size_1-of-1">
        <h4 class="title slds-text-heading--medium">Archive</h4>
    </div>
    <!--End Modal Header--> 
     
    <!--Modal Body-->  
    <div class="slds-modal__content slds-p-around--x-large slds-align_absolute-center slds-size_1-of-1 slds-is-relative">
        <form class="slds-form--stacked">
           
                <div class="slds-align_absolute-center slds-text-color_error">
                    <b>You Don't Have Permission to Archive this Record</b>
                </div>            
            
        </form> 
    </div>
    <!--End of Modal Body-->
     
    <!--Modal Footer-->
    <div class="modal-footer slds-modal__footer slds-size_1-of-1">
        <lightning:button variant="Neutral" class="slds-button" label="Cancel" onclick="{!c.cancelBtn}"/>
        
    </div>
    </aura:if>
    
    <aura:if isTrue="{!v.showSpinner}">
        <lightning:spinner variant="brand" alternativeText="Loading" />
    </aura:if>
</aura:component>

Controller:


({
    updateStatus : function(component, event, helper) {
    var itemsetupId=component.get("v.recordId");
        var action=component.get("c.getItemSetupForm");
        action.setParams({"itemsetupId":itemsetupId
            
        });
        action.setCallback(this,function(response){
            
            var state=response.getState();
            if(state==="SUCCESS"){
               let toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Success!",
                    "message": "The record Archived Successfully",
                    "type": "success"   
                });
                toastEvent.fire();
            }
             else if (state === "ERROR")
        {
            alert("You can't make any changes on Archived Record");
        }
              var dismissActionPanel = $A.get("e.force:closeQuickAction");
                dismissActionPanel.fire();  
              $A.get('e.force:refreshView').fire();
             component.set("v.showSpinner",false);
        });
        $A.enqueueAction(action);
    },

    cancelBtn : function(component, event, helper) {
        // Close the action panel
        var dismissActionPanel = $A.get("e.force:closeQuickAction");
        dismissActionPanel.fire();
    },
     // Checking user access for creating new version record
    doInit: function(component, event, helper){ 
         component.set("v.showSpinner",true);
        var action = component.get("c.checkUserValidity");
        action.setCallback(this, function(response){
           var state = response.getState();
            if(state === "SUCCESS"){
                if(response.getReturnValue()){
                   component.set("v.accessFlag", true); 
                }
                else{
                  component.set("v.noAccessFlag", true);  
                    
                }
                component.set("v.showSpinner",false); 
            }
            
        });
       
        $A.enqueueAction(action);
        
    }
    
})


Validation rule:
condition:  Record_Locked__c =true
error message : The Record Archived, You can't make any changes.
AnudeepAnudeep (Salesforce Developers) 
Can you please confirm what is the result of the below SOQL when Record_Locked__c is true and when it is false based on itemsetupId value? 
 
Select Id, Name,Processed__c,Item_Setup_Form_Status__c from Item_Setup__c where Id=:itemsetupId and Item_Setup_Form_Status__c!='Archived'

 
santhiya duraisanthiya durai
Hi Anudeep,

Thanks for your response.
I am updating Record_Locked__c field as checked when Status is Archived using Process builder.
So When Item_Setup_Form_Status__c is Archived, Record_Locked__c should be checked.

Record_Locked__c is unchecked in below query 
Select Id, Name,Processed__c,Item_Setup_Form_Status__c from Item_Setup__c where Id=:itemsetupId and Item_Setup_Form_Status__c!='Archived'



 
AnudeepAnudeep (Salesforce Developers) 
 Your code is trying to update the record and the process builder is updating the record as well making changes to Record_Locked__c field enabling the validation rule (restrict a user to make changes on a record when Record_Locked field is true) which is preventing further upserts

      itemsetup.Item_Setup_Form_Status__c='Archived';

What do you see in the debug logs? Have you tried disabling the process builder or commenting out the upsert part of the code?
santhiya duraisanthiya durai
Hi Anudeep,

I will explain you a Use case.
I have created a Quick Action button (Archive) on itemsetup object. On click of Quick Action Lightning popup will display. If I click on continue button then Status of Itemsetup record should get updated as Archived and name should be Name_Archived. Without validation rule this working fine.

I want to Achieve a below part.
Once Record is Archived then that record is never reusable. User can't make any changes on that Archived and record is just read-only.