Activity Forums Salesforce® Discussions If You Are In Situation That You Don’t Know Which Sobject Is Going To Be Instantiated?

  • If You Are In Situation That You Don’t Know Which Sobject Is Going To Be Instantiated?

    Posted by Aman on September 20, 2018 at 3:06 pm

    How To Create Instance Of Sobject Dynamically? Normally The Sobject Is Created Like “account A = New Account();”. But If You Are In Situation That You Don’t Know Which Sobject Is Going To Be Instantiated? Means It Will Be Decided At Runtime, How You Will Handle It?

    shariq replied 5 years, 7 months ago 2 Members · 1 Reply
  • 1 Reply
  • shariq

    Member
    September 20, 2018 at 3:08 pm

    Hi,

    public SObject getNewSobject(String t){

    // Call global describe to get the map of string to token.

    Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();

    // Get the token for the sobject based on the type.

    Schema.SObjectType st = gd.get(t);

    // Instantiate the sobject from the token.

    Sobject s = st.newSobject();

    return s;

    }

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos