Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)
edited by

Is it possible to Cast SObject dynamically?

Example :

I know we can do this : 

(Account) Sobject

But I want to do this as the return type of sObject changes based on certain parameters.

(Dynamically Passing the Name) SObject

Any Kind of way around will be helpful... Thanks in advance :)

1 Answer

0 votes
by (32.1k points)
edited by

No, there isn't any way to do a dynamic typecast in Apex. But, what you can do to work through this is to make a virtual or abstract class with all of the logic using the dynamic apex form (e.g. record.put). Then have a subclass for the Lead type that covers this method's return type in a Lead cast.

To learn in-depth about Salesforce, sign up for an industry based Salesforce Training.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...