How to get Apex class that implements the Schedulable interface? - salesforce

I have many classes that implement Schedulable Interface. I want to schdule multiple Apex classes at a time through an another apex class. So I need to query all the classes that are implement Schedulable interface.
I am using the following code snipet to achieve this, but I am getting a compiler error like below
ERROR:
"You must select an Apex class that implements the Schedulable interface. at line 127 column 13"
CODE:
list<ApexClass> listClasses;
String input='0 0 8 13 2 ?';
listClasses=[Select Id, Name,body from ApexClass]
for(ApexClass a:listClasses){
system.schedule(a.Name+' AutoScheduler', input, a);
}
Question:
How do I query all the apex class which implement schedulable interface? So that I can directly pass it to system.schedule() method.
DIFFERENT TRY:
When after getting this error I tried to query only one apex class(Known Class) which implements schedulable interface. Again no use. Please see the below snipet for the different try
CODE:
list<ApexClass> listClasses;
String input='0 0 8 13 2 ?';
//Retriving only one class of Name='SchedularTest'
listClasses=[Select Id, Name,body from ApexClass where Name ='SchedularTest']
for(ApexClass a:listClasses){
system.schedule(a.Name+' AutoScheduler', input, a);
}
ERROR:
"You must select an Apex class that implements the Schedulable interface. at line 127 column 13"
Thanks in advance
Satheeskumar

I'd say the error message is quite clear?
The last parameter you're passing to System.schedule has to be a new object. New instance of the class. What you're passing is the metadata information about the class, it's body etc... This stuff can't be "run" like that.
I'm going to bet that system.schedule('SchedulerTest AutoScheduler', input, new SchedulerTest()); will work OK? If it doesn't - make sure the class compiles OK and maybe also whether it is marked as valid.
See? how do you expect something that represents a row in the database / file on disk to "work" like an object of the class?
If you need to create a new object of given class having only this class' name - you might want to check Type methods.
String className = 'SchedulerTest';
Type t = Type.forName(className);
System.schedule(className, '0 0 8 13 2 ?', (Schedulable)t.newInstance());
This makes a new object (it's actually a generic Object) and then you cast it to something that's acceptable for the method. You'll get a runtime failure if the class doesn't implement the interface:
System.TypeException: Invalid conversion from runtime type
SchedulerTest to system.Schedulable

Related

'MyMethod' in 'Dao_Impl' clashes with 'MyMethod + extends' in 'Dao'; both methods have same erasure, yet neither overrides the other

I wasnt getting any errors before. But today, i entered my project to edit and saw these errors.
First one is on this line;
public final class NoteDao_Impl implements NoteDao {
// The error code is : Class 'NoteDao_Impl' must either be declared abstract or implement abstract method 'getAllNotes(Continuation<? super List<? extends Notes>>)' in 'NoteDao'
I can fix it with Alt + Enter and then clicking 'implement methods' but just in case i want to write it.
The second one and third one is on these lines;
#Override
// Error : Method does not override method from its superclass
public Object getAllNotes(final Continuation<? super List<Notes>> continuation) {
// Error : 'getAllNotes(Continuation<? super List<Notes>>)' in 'com.ahmetkaan.kediy.data.NoteDao_Impl' clashes with 'getAllNotes(Continuation<? super List<? extends Notes>>)' in 'com.ahmetkaan.kediy.data.NoteDao'; both methods have same erasure, yet neither overrides the other
The NoteDao;
package com.ahmetkaan.kediy.data
import androidx.room.*
#Dao
interface NoteDao {
#Query("SELECT * FROM notes ORDER BY id DESC")
suspend fun getAllNotes() : List<Notes>
#Query("SELECT * FROM notes WHERE id =:id")
suspend fun getSpecificNote(id:Int) : Notes
#Query("SELECT * FROM notes WHERE category = :id")
suspend fun getCategoryNote(id:Int) : Notes
#Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertNotes(note:Notes)
#Delete
suspend fun deleteNote(note:Notes)
#Query("DELETE FROM notes WHERE id =:id")
suspend fun deleteSpecificNote(id:Int)
#Update
suspend fun updateNote(note:Notes)
}
The problem appears when i set getAllNotes() method as "getAllNotes() : List " So i guess thats why. But i dont understand why i get this error now. I do some research and i encounter some solutions like;
public class a<T extends Comparable> implements A<T>
but probably i cant make it compatible with my project :,) Thanks in advance.
I met the same problem. I try this link to fix the problem.
You can try changing List<Notes> to Array<Notes>.
Hope it will help.
Also, if this error causes your app to crash at runtime, and you don't want to change the List to Array, maybe there is another approach. If you are using manual migration of the room database, try to change it to auto migration(Doc). Although there still has this error in the build package, it won't affect your app performance. Your app can run without crashing.

How to use sealed classes in Android using Kotlin?

I am not able to understand how/when to use sealed classes in Android using Kotlin. I have read the docs but still I am getting confused about its' structure and how to use it. It'd of great help if someone could help me understand this.
Sealed classes allow us to represent hierarchies in a more flexible way. It’s also more readable and is used for better state management.
Child-classes of Sealed class, can be of any type, like: data class, object class, any regular class, or even another sealed class.
This is how we define a sealed class in Android using Kotlin:
sealed class UiState {
object Loading : UiState()
data class Success(val successMessage: String) : UiState()
data class Error(val error: Throwable?) : UiState()
}
Let’s understand this part of the code:
object Loading : UiState() : declared this as an object, as we don’t need to know anything else at this stage.
data class Success(val successMessage: String) : UiState() : declared as data class, as we need the message when we got Success state.
data class Error(val error: Throwable?) : declared this as data class, as we need the Throwable when we got Error state.
Notice how we have used object Loading : UiState(). We are using the same sealed class name as the return type of “Loading” object.
This says, that the “Loading” object belongs to “UiState” sealed class.
This is same for Success and Error states as well.
We can then use this sealed class as the following:
fun observeUiStates(uiState: UiState) = when(uiState) {
UiState.Loading -> println("Loading...Please wait.")
is UiState.Success -> println(uiState.successMessage)
is UiState.Error -> println(uiState.errorMessage)
}
In this aforementioned block of code we are passing the UiState sealed class as a parameter to observeUiStates() method and then by using when we are checking which is the current state of the app and then we will be handling the states accordingly.
Here for the sake of this example, I am just printing the result in the console.
Sealed classes, best work with Model-View-Intent pattern in Android by using RxJava.
By using them, we will now be able to observe to the state changes.
Whenever there is a new state of the app, this observeUiStates() method will give us that specific state only.
To keep it simple for this specific example, I am calling this method from main(). See the code below:
fun main() {
observeUiStates(UiState.Loading)
observeUiStates(UiState.Success("The Ui has returned success state.”))
}
This will print the following in the console:
Loading...Please wait.
The Ui has returned success state.

How Mybatis (iBatis) read my private variable?

I was wondering how Mybatis get the private variable in Java.
For example:
Let's say we have a Java class called Foo:
public class Foo{
private int foolID;
public Foo(int foolID){
this.foolID = foolID;
}
}
And let's create XML mapper for insert.
<insert id="insert" parameterType="Foo">
insert into foo_table (id)
values (#{foolID});
</insert>
Let's say there are FooDAO java class and FooMapper java interface for this insert.
My question is how come foolID is readable even without Getter (Even if there is a getter method for foolID, I never specify what the getter is...). It seems like magic to me, and I know there is no magic for programming... :)
The only way I can think of is reflection.
Thanks for your help in advance.
That's correct, reflection is used to access private fields, but only if accessing private fields is not restricted.
Seams like reflection is used heavily not just to access private fields but to invoke setters getters etc.

Apex Compile Error: sObject type 'Book__c' is not supported

I am new in apex programming. I am using site.com.
I am creating New apex class using this code --
#isTest
private class HelloWorldTestClass {
static testMethod void validateHelloWorld() {
Book__c b = new Book__c(Name='Behind the Cloud', Price__c=100);
System.debug('Price before inserting new book: ' + b.Price__c);
// Insert book
insert b;
// Retrieve the new book
b = [SELECT Price__c FROM Book__c WHERE Id =:b.Id];
System.debug('Price after trigger fired: ' + b.Price__c);
// Test that the trigger correctly updated the price
System.assertEquals(90, b.Price__c);
}
}
but it gives me this error -Error: Compile Error: sObject type 'Book_c' is not supported. If you are attempting to use a custom object, be sure to append the '_c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 13 column 12.
help me out..
Have you actually created such object in your Salesforce instance? There are some standard objects (like User) and you most likely also have the objects related to the CRM product (like Account, Contact, Opportunity) but Book__c will be a custom object created by you or another System Administrator in your organization.
Check Setup (upper right corner in web interface) -> Create -> Objects. There's a youtube tutorial or you can always click "Help for this page" link.
To expose this object on the Site (which means anybody in the world will be able to view and insert Books) you'll have to follow few more steps. But as it's a test class that fails to compile I think you haven't reached this problem yet. More info about permissions: http://login.salesforce.com/help/doc/en/siteforce_data_access_perms.htm

Error Application cast in WPF

i have 2 projects in my solution (main is A.WPF and secondary is B.WPF)
when i'm trying to access variables inside my App.xaml.cs in B.WPF:
filename = ((App)Application.Current).ErrorLogFileName;
i get the following error:
Unable to cast object of type 'A.App' to type 'B.App'.
i also tried the following:
filename = ((B.App)Application.Current).ErrorLogFileName;
but still the same error...
the definition in B.App is:
private string _errorLogFileName = "error log.xml";
public string ErrorLogFileName
{
get { return _errorLogFileName; }
}
please assist...
Looks like you need to do:
filename = ((A.App)Application.Current).ErrorLogFileName;
The error is saying the type is A.App, yet in both cases you are trying to cast to B.App.
There can only be one current application also.
Application.Current refers to the current application. The only way to be allowed to cast the current App to another App-type is when the other App-type is a base class of the current App-type.
Are A.App and B.App siblings or is B.App a base class of A.App?
If you don't want B to have a reference to A (or can't as you want A to reference B and that would cause a circular reference), then you need a common type defined in a third assembly that both A and B reference. In our implementation we tend to have a ConfigurationData type that is in a separate project referenced by both Wpf projects, e.g.
public static class ConfigurationData
{
private static string _errorLogFileName = "error log.xml";
public string ErrorLogFileName
{
get { return _errorLogFileName; }
}
}
Another approach would be to define an Interface for your ErrorLogFileName property in a 3rd assembly that both A and B reference, and then implement that interface on your Wpf Application class - A and B would then both be able to cast to that type. If you wanted your A project to set the values on that at runtime, you could make the ErrorLogFileName a read-write property instead and initialize it in your application startup.
I personally prefer using a separate ConfigurationData type from the Wpf app object for this kind of stuff (ErrorLogFileName etc.) as it can then also be used for code that might execute in a unit test and therefore might not be running under a Wpf application - it also avoids having to do casts all over the place (ConfigurationData.ErrorLogFileName instead of ((IAppConfigurationData)Application.Current).ErrorLogFileName.
BTW, if you have an Application object in both assemblies it sounds like you might have both assemblies configured to build as Output type: Windows Application in your project properties. You should only really have one assembly that is configured as the Windows Application and the rest should be Class Library to avoid confusing numbers of Application classes being generated - only the one in the main EXE (and it's related resources) will get created at runtime.

Resources