Using the instanceof Keyword in Apex in Salesforce

If you need to verify at run time whether an object is actually an instance of a particular class, use the instanceof keyword. The instanceof keyword can only be used to verify if the target type in the expression on the right of the keyword is a viable alternative for the declared type of the expression on the left.

You could add the following check to the Report class in the classes and casting example before you cast the item back into a CustomReport object.

If (Reports.get(0) instanceof CustomReport) {
    // Can safely cast it back to a custom report object
   CustomReport c = (CustomReport) Reports.get(0);
   } Else {
   // Do something with the non-custom-report.


No comments:

Post a Comment

Sharing is Caring!!! Share this with your friends!!!

submit to reddit