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;
}
}
1 Answer
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
-
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
CloneCaseController
an inner class ?