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 

How to load an existing record and modify it

Hi Gurus,

this is an apex controller code to insert a record,please help me in writing a code to load an existing record and modify it as well
 
public class PickListHandler {
    @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{
     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;  
}
    
     
    
    

}

Your help is highly appreciated

Regards,
Fiona
ravi soniravi soni

Hi fiona,
I am not able to understand your requirment properly. can you explain it properly, so that i help to  you.
and if you are using js and html then please show me. 
Thank you

fiona gentryfiona gentry
Here is .cmp 
<aura:component controller="PickListHandler" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction,lightning:actionOverride" 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 controllerJS
 
({
  
    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": "Error",
                        "message": "The record has not been inserted  successfully."
                    });
                    toastEvent.fire();
                }
            }
            $A.get('e.force:refreshView').fire(); 
        });
        $A.enqueueAction(action);
       
    },
      /*page refresh after data save*/    
      isRefreshed: function(component, event, helper) {
        location.reload();
    }
       
       
})

Now I want to extend the functionality of above lightning component so that user who wishes to change Record data on Case can just update the value of Level1, Level2 and Level3 and do an Upsert on the same case
Now to achieve Edit a record functionality,added lightning:actionOverride in apex cmp and then overridden the standard Edit button of ERT case type custom object ..Here comes the problem when I use the same component in Edit button ,I see error as belowUser-added image

Hence  I need help to  write a  code to load an existing record and modify it
 
fiona gentryfiona gentry
Current Behaviour of my Lightning component:- Lightning component is used on Case Type to create multi levels Level1 then Dependent Level2 then dependent Level3 selected and clicking in Save saves entire Record to Case,works fine till here on one of custom object Case TypeUser-added image

Expected behavior:- Now I want to extend the functionality of above lightning component so that user who wishes to change Record data on Case can just update the value of Level1, Level2 and Level3 and do an Upsert on the same case