+ Start a Discussion
Eric Blaxton 11Eric Blaxton 11 

lightning component extend existing Apex class

Hi and thanks in advance.

We are converting a url button to Lightning component.  I can get the url button to work in Lightning, but I don't have the control over pre populating fields etc.  I want to clear out a few fields.  This is a Clone account button which fires an Apex Class.  

The error I get when clicking the Action button is "Error while creating component for lightning component quick action [Unable to find action 'getAccount' on the controller of c:Clone]"

Please advise on where i went wrong.


I wrote a component

<aura:component controller="CloneAcctBtn" implements="force:lightningQuickActionWithoutHeader,lightning:isUrlAddressable,force:hasRecordId,lightning:hasPageReference,force:appHostable,flexipage:availableForAllPageTypes" >
    <!--This controls the display of the New RTO Quick action screen, before the New Account screen.  It supresses it.   
    <aura:html tag="style">
        .slds-backdrop {
        background: rgba(43, 40, 38, 0) !important;  
        }
        .slds-fade-in-open {
        display:none !important;
        }
    </aura:html>-->
    <aura:attribute name="acc" type="Account"/>     
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />    
         
</aura:component>

Controller:

({
    doInit : function(component, event, helper) 
    {
      var userId = $A.get("$SObjectType.CurrentUser.Id");   
      var action = component.get("c.getAccount");
      action.setParams({"accountId": component.get("v.recordId")});
      action.setCallback(this, function(response){
      var state = response.getState();   
     
      if(component.isValid() && state === "SUCCESS")
       {
        var  acc = response.getReturnValue(); 
        var createAcountEvent = $A.get("e.force:createRecord");
        var accid=component.get("v.recordId");
            
        console.log('accid-'+accid);
          //example set
          createAcountEvent.setParams({
              "entityApiName": "Account",
              "defaultFieldValues":{
          'Name': acc.sObjectInfo.Name,  
          'DataSrc__cSave' : acc.sObjectInfo.DataSrc__c, 
          'CMF_Bill_To_DUNS_Number__cSave' : acc.sObjectInfo.CMF_Bill_To_DUNS_Number__c,
          'Federal_Tax_ID_Number__cSave' : acc.sObjectInfo.Federal_Tax_ID_Number__c,
          'AccountNumberSave' : acc.sObjectInfo.AccountNumber
              }    
       });

        createAcountEvent.fire(); 
                    
        } 
        else
        {
        //write toast message replace the console.log
        helper.toastMessage('Problem getting account, response state');
        //console.log('Problem getting account, response state: ' + state);
        }
        });
        $A.enqueueAction(action);
    }
                    
 })

Class
    public with sharing class CloneAcctBtn {
@AuraEnabled
    public Account a;
    Map<String, Schema.SObjectField> m1 = Schema.SObjectType.Account.fields.getMap();
    string oldRecType;
    
    public CloneAcctBtn(ApexPages.StandardController stdController) {
        this.a = (Account)stdController.getRecord();
        oldRecType = a.RecordTypeId;
    
    }

    
    public Pagereference acctCloned(){//Account acct
        //find all record types
        List<Schema.RecordTypeInfo> infos = Schema.SObjectType.Account.RecordTypeInfos;
        Id defaultRecordTypeId;
        integer i = 0;
        
        String theId = ApexPages.currentPage().getParameters().get('id');
        system.debug(a + ' '+ theID);
        //a = [select id, name, BillingStreet from Account where id =:theId];
        
        /* query lead and then clone it */
        String soql = GrabUpdateableFieldsUtil.getCreatableFieldsSOQL('Account','id=\''+theId+'\'');
        if(Test.isRunningTest())
            System.debug('SOQL for clone: ' + soql);
        a = (Account)Database.query(soql);
        
        // Do all the dirty work we need the code
        Account clonedAcc = a.clone(false,true,false,false);
        
        //check each one
        for (Schema.RecordTypeInfo info : infos) {
            i++;
            if (info.DefaultRecordTypeMapping && info.isAvailable()) {
                defaultRecordTypeId = info.RecordTypeId;
            }
            
        }
        
        for(String fieldName : m1.keySet()) {
            if(m1.get(fieldName).getDescribe().isUpdateable()) {
                system.debug(m1.get(fieldName) + ' ' +m1.get(fieldName).getDescribe().getType() + ' '+m1.get(fieldName).getDescribe() );
                
                //get data types for each fields
                Schema.DisplayType fielddataType = m1.get(fieldName).getDescribe().getType();

                if(fielddataType  == Schema.DisplayType.Integer)
                {
                    clonedAcc.put(fieldName , 0);
                }
                else if(fielddataType != Schema.DisplayType.DateTime){
                    continue;
                }
                else if(fielddataType != Schema.DisplayType.Date){
                    clonedAcc.put(fieldName , date.valueof(system.today()));
                }
                else if(fielddataType != Schema.DisplayType.Double){
                    clonedAcc.put(fieldName , 0.0);
                }
                else if(fielddataType != Schema.DisplayType.Boolean){
                    clonedAcc.put(fieldName , true);
                }
                //else if(fielddataType != Schema.DisplayType.String){
                   // clonedAcc.put(fieldName , '');
               // }
                else
                   clonedAcc.put(fieldName , 'Some Value');
            }
        }
        //here is the default RecordType Id for the current user
        System.debug(defaultRecordTypeId);
        if(oldRecType != defaultRecordTypeId)
        {
            clonedAcc.Type__c = '';
        }
        clonedAcc.OwnerID = userinfo.getUserID();
        clonedAcc.Account_Status__c = 'Prospect';
        clonedAcc.RecordTypeId = defaultRecordTypeId;
        clonedAcc.ExternalSysID__c = ''; 
        clonedAcc.Bill_To_DUNs_Number__c = '';
        clonedAcc.accountnumber = '';
        clonedAcc.CMF_Bill_To_DUNS_Number__c = '';
        clonedAcc.CMF_Ship_To_DUNS_Number__c = '';
        
        insert clonedAcc;
        //System.assertEquals(a, clonedAcc); to do
        

        //Redirect the user back to the original page
        PageReference pageRef = new PageReference('/' + clonedAcc.id); 
        pageRef.setRedirect(true);
        return pageRef;   
        
        
    }

}
Best Answer chosen by Eric Blaxton 11
Agustin BAgustin B
Hey, as I can see you dont have a getAccount method in your controller CloneAcctBtn.
So the line var action = component.get("c.getAccount"); is the one giving you problems when you enqueue the action.

If you are not using that class anywhere else, then I would just remove all the visualforce code you have there and put everything under a method called getAccount. You can pass the parameters from the lightning component to the controller.

If it helps please mark as correct, it may help others
good luck!