+ Start a Discussion
sparsh bhatnagarsparsh bhatnagar 

var state = response.getState(); in error why ?

component : - <aura:component controller="updatecaseowner" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
         
    <aura:attribute name="case" type="Case[]"/>
     <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="disabled" type="Boolean" default="False" />        
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>
   
    <div class="slds-button-group" role="group">
    
    <ui:button aura:id="button" class="slds-button slds-button--brand slds-size--1-of-1"
           label="Change Owner" press="{!c.onclick}" />
        
    </div>
</aura:component>



controller :-
({    
init : function(component, event, helper) {
    var action = component.get("c.getCase");
    action.setParams({"recordId": component.get("v.recordId")});

       console.log(action);
    
    action.setCallback(this, function(response) {
        var state = response.getState();
 
        if( state == "SUCCESS"){
                        //component.set("v.disabled", "True");
                    component.set("v.case", response.getReturnValue()); }
                         
         else {
            console.log('There was a problem : '+response.getError());
        }
         $A.enqueueAction(action);
    });
   
},
    onclick : function(component, event, helper) {
    var result = confirm("Do you Really want to change the owner details");
     if(result)  { 
    var action = component.get("c.saveCase");
                        console.log(component.get("v.recordId"));
        action.setParams({"recordId": component.get("v.recordId")});
                          console.log('done');
                        console.log(component.get("v.recordId"));
     
        action.setCallback(this, function(response) {
        var state = response.getState();
          console.log(state);
        if(state === "SUCCESS") {
            //var caserec = component.get("v.case"); 
           // caserec.push(); 
            component.set("v.case",response.getReturnValue());
           console.log('here');
            console.log(response.getReturnValue());
        }
        else if(state === "ERROR")
        { alert("Case has no Related records");}
        });
          $A.enqueueAction(action);
    }
}
})

apex :- public class updatecaseowner {
    
@AuraEnabled
public static Case getCase(Id recordId){
   return [SELECT Id , ownerId, ParentID  FROM Case WHERE id =:recordId];

}

@AuraEnabled
public static Case saveCase(Id recordId){
    Case caseRec = [SELECT Id, ownerId, ParentID ,  Status FROM Case WHERE parentId=:recordId];
    Case caseP = [SELECT ID , ownerID from case where id= :recordId ];    
    if (caseRec!=null)
        
    {
     
            caseRec.ownerID= casep.OwnerId;
    

    update caseRec;
    return caseRec; 
    }
   
return caserec;
}}
Best Answer chosen by sparsh bhatnagar
Nayana KNayana K
public class updatecaseowner {
    
@AuraEnabled
public static Case getCase(Id recordId){
   return [SELECT Id , ownerId, ParentID  FROM Case WHERE id =:recordId LIMIT 1];

}

@AuraEnabled
public static List<Case> saveCase(Id recordId){
List<Case> lstCase = new List<Case>();
    Case caseP = [SELECT ID , ownerID from case where id= :recordId LIMIT 1 ];    
    
     for(Case objCase : [SELECT Id, ownerId, ParentID ,  Status FROM Case WHERE parentId=:recordId])
{
           objCase .ownerID= caseP.OwnerId;
lstCase.add(objCase);
 }

    if(!lstCase.isEmpty())
{
    update lstCase;
}
    
   
return lstCase;
}}

<aura:component controller="updatecaseowner" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
         
    <aura:attribute name="relatedCases" type="Case[]"/>
 <aura:attribute name="currentCase" type="Case"/>
     <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="disabled" type="Boolean" default="False" />        
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>
   
    <div class="slds-button-group" role="group">
    
    <ui:button aura:id="button" class="slds-button slds-button--brand slds-size--1-of-1"
           label="Change Owner" press="{!c.onclick}" />
        
    </div>
</aura:component>
 
({    
init : function(component, event, helper) {
    var action = component.get("c.getCase");
    action.setParams({"recordId": component.get("v.recordId")});

       console.log(action);
    
    action.setCallback(this, function(response) {
        var state = response.getState();
 
        if( state == "SUCCESS"){
                        //component.set("v.disabled", "True");
                    component.set("v.currentCase", response.getReturnValue()); }
                         
         else {
            console.log('There was a problem : '+response.getError());
        }
         $A.enqueueAction(action);
    });
   
},
    onclick : function(component, event, helper) {
    var result = confirm("Do you Really want to change the owner details");
     if(result)  { 
    var action = component.get("c.saveCase");
                        console.log(component.get("v.recordId"));
        action.setParams({"recordId": component.get("v.recordId")});
                          console.log('done');
                        console.log(component.get("v.recordId"));
     
        action.setCallback(this, function(response) {
        var state = response.getState();
          console.log(state);
        if(state === "SUCCESS") {
            //var caserec = component.get("v.case"); 
           // caserec.push(); 
            component.set("v.relatedCases",response.getReturnValue());
           console.log('here');
            console.log(response.getReturnValue());
var relatedCases = component.get("v.relatedCases");
if(relatedCases.length == 0)
{
alert('no related cases'); 
}

        }
        else if(state === "ERROR")
        { console.log('some problem'+response.getError());}
        });
          $A.enqueueAction(action);
    }
}
})

Please try this once