0
public class CloneCaseController {
    public static void cloneCase(Id caseId){
        
        // Retrieve the original case record
        Case originalCase = [SELECT Id, Subject, Description, Status, Priority, Origin, 
            RecordTypeId, ContactId, AccountId, OwnerId 
            FROM Case 
            WHERE Id = :caseId];
        
        // Create a new case record based on the original record
        Case newCase = originalCase.clone(false, true, false, false);
        newCase.OwnerId = UserInfo.getUserId(); // Set the owner of the new case to the current user
        
        // Insert the new case record
        insert newCase;
    }
}
3
  • 4
    is your CloneCaseController an inner class ?
    – Aman Patil
    Commented Feb 27, 2023 at 6:50
  • Can you edit the question and let community know where excatly you are facing the issue. Is it when calling the method ? Commented Feb 27, 2023 at 6:51
  • Yes Aman its the inner class Commented Feb 27, 2023 at 7:01

1 Answer 1

2

The error message

static can only be used on methods of a top level type

indicates that you are trying to declare a static method inside an inner class. In Apex, only top-level classes can have static method.

An Inner class cannot have a static method. You must refactor the inner class to its own class.

you need to refactor it making CloneCaseController a separate class.

I believe you have class like below where CloneCaseController is inner class of myExampleClass

public class myExampleClass{

    public static string doIt(Boolean optionOne){

        if(optionOne)
            return 'First';
        else
            return 'Second';

    }

    public class CloneCaseController {

        public static void cloneCase(Id caseId){
        
        // Retrieve the original case record
        Case originalCase = [SELECT Id, Subject, Description, Status, Priority, Origin, 
            RecordTypeId, ContactId, AccountId, OwnerId 
            FROM Case 
            WHERE Id = :caseId];
        
        // Create a new case record based on the original record
        Case newCase = originalCase.clone(false, true, false, false);
        newCase.OwnerId = UserInfo.getUserId(); // Set the owner of the new case to the current user
        
        // Insert the new case record
        insert newCase;
    }

    }


}

You need to move the inner class CloneCaseController to is own main class it will compile just fine

public class myExampleClass{

    public static string doIt(Boolean optionOne){

        if(optionOne)
            return 'First';
        else
            return 'Second';

    }
}

Own class

public class CloneCaseController {

        public static void cloneCase(Id caseId){
        
        // Retrieve the original case record
        Case originalCase = [SELECT Id, Subject, Description, Status, Priority, Origin, 
            RecordTypeId, ContactId, AccountId, OwnerId 
            FROM Case 
            WHERE Id = :caseId];
        
        // Create a new case record based on the original record
        Case newCase = originalCase.clone(false, true, false, false);
        newCase.OwnerId = UserInfo.getUserId(); // Set the owner of the new case to the current user
        
        // Insert the new case record
        insert newCase;
    }

    }


reference:Only top-level class methods can be declared static

1
  • i have modified the code as public static void cloneCase(Id caseId){ to public void cloneCase(Id caseId){ it is working for me now Commented Feb 27, 2023 at 7:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .