-3

I have an Apex method deleteAddress() that deletes a record of custom object Address. This method is placed in VF page controller. I have an Lightning Web Component which I have placed on the same VF page using lightning out. I wish to use the same deleteAddress() method for my LWC.

The problem is, the method needs ID of addressToDelete. In classic, it's value is set using getter setter.

public string addressToDelete{ get; set; }

public void DeleteAddress(){
        if(addressToDelete==null)
            return;
        AP_Customer_Relationship__c toDelete=[Select Id,Customer_Address__r.Id FROM AP_Customer_Relationship__c WHERE Id=:addressToDelete LIMIT 1];
        if (toDelete!=null){
            delete toDelete.Customer_Address__r;
        }
        getRelatedContactAddressLst(); 
    }

In LWC, I need to pass it as a parameter to the method. Rest of the code is same.

    @auraEnabled
    public static void DeleteAddress1(String addressToDelete){
        if(addressToDelete!=null)
        {
           AP_Customer_Relationship__c toDelete=[Select Id,Customer_Address__r.Id FROM AP_Customer_Relationship__c WHERE Id=:addressToDelete LIMIT 1];
           if (toDelete!=null){
               delete toDelete.Customer_Address__r;
           }
        }
    }

Currently, I have separate methods for classic and lightning. I wish to have a single method that will work for both classic and lightning without changing the prototype of the method. Maybe a way to set the ID addressToDelete in the method without passing it as a parameter in LWC?

I referred this, but it didn't provide a solution for my problem. Please guide. Thanks!

  • IMHO this is bad programming practice; you should separate responsibilities and concerns into various different classes. By all means have common code, in a separate class used by the Visualforce controller and the Aura Enabled methods used by your LWC, but don't mix the Visualforce and LWC code directly in the same class. Doing so makes it harder to maintain and understand. – Phil W Jan 20 at 7:56
  • @PhilW Thanks for your time! My objective was to avoid hitting governor limits on apex code and also avoid extra efforts of re-writing the test classes for the same functionality in lightning. – Koustubh Jan 20 at 8:06
3

IMHO this is bad programming practice; you should separate responsibilities and concerns into various different classes. By all means have common code, in a separate class used by the Visualforce controller and the Aura Enabled methods used by your LWC, but don't mix the Visualforce and LWC code directly in the same class. Doing so makes it harder to maintain and understand.

It's worth remembering that LWC doesn't have the same "controller" concept as Visualforce; you can use Aura Enabled methods from any number of classes in an LWC.

| improve this answer | |
  • Thanks for the guidance! My objective was to avoid hitting governor limits on apex code and also avoid extra efforts of re-writing the test classes for the same functionality in lightning. – Koustubh Jan 20 at 8:05
  • Refactor your code - you can do the same processing just split across a couple of classes/methods without significant CPU impact as long as you consider bulkification. – Phil W Jan 20 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.