Tuesday, September 13, 2016

Dynamic Apex in Salesforce

You can fetch all sobject and their field information in apex by using describe methods. Apex gives 2 data types for sobject and field information.
  1. Token: A lightweight, serializable reference to an sObject or a field that is validated at compile time.
  2. Describe Result: It contains all the describe information about sobject and fields. Describe result objects are not serializable, and are validated at runtime.
It is easy to move from a token to its describe result, and vice versa. Both sObject and field tokens have the method getDescribe which returns the describe result for that token.
On the describe result, the getSObjectType and getSObjectField methods return the tokens for sObject and field, respectively.

Finding All object in Organization

Schema getGlobalDescribe method returns a map that represents the relationship between all sObject names (keys) to sObject tokens (values).
For example:
Map<String, Schema.SObjectType> schemaMapOfOrg = Schema.getGlobalDescribe();
The map has the following characteristics:
  • It is dynamic, that is, it is generated at runtime on the sObjects currently available for the organization, based on permissions.
  • The sObject names are case insensitive. 
  • The keys use namespaces as required. The keys reflect whether the sObject is a custom object.
Creating sObjects Dynamically

If we need to create contact record dynamically, then use below code:
String typeName='Contact';
Schema.SObjectType objToken = Schema.getGlobalDescribe().get(typeName);
sobject son= objToken.newSObject();
son.put('firstname','Execute1');
son.put('LastName','Kumar');
son.put('Email','sunil.kumar2@in.gmail.com');
insert son;

Apex code to find all sobject and their fields

Apex code to generate SOQL to fetch all fields of an object

18 comments:

  1. Great post! I am see the Salesforce Programming coding and step by step execute the outputs.I am gather this lot of coding with more output information. Also great post for the valuable information of salesforce.

    Salesforce Training in Chennai

    ReplyDelete
    Replies
    1. Great Article Cloud Computing Projects

      Networking Projects

      Final Year Projects for CSE

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. This is an awesome post.Really very informative and creative contents.
    SEO company in Chennai

    ReplyDelete
  3. I attempted IT exams many times but could not find as suitable and authentic study material as Identity-and-Access-Management-Designer Dumps. I grabbed all the required knowledge quickly and confidently attempted for the final test. After completing preparation from Identity-and-Access-Management-Designer Dumps I used online practice test for enhancement of confidence and familiarity to real exam settings.

    ReplyDelete
  4. I am thankful for the kindness and patience that was showed by the experts at www.exam4lead.com while answering my queries. I also appreciate the effort put on IDENTITY-AND-ACCESS-MANAGEMENT-DESIGNER DUMPS because this stuff helped me a lot during my preparation. I studied this guide thoroughly and understood all the concepts. It was only because of this handy material that I was able to answer all the questions in the final test. IDENTITY-AND-ACCESS-MANAGEMENT-DESIGNER EXAM DUMPS material was fully relevant and comprehensive. I didn’t feel any need to consult anywhere further. One more thing that was helpful for me was its PDF form.

    ReplyDelete
  5. You can make your life wonderful as it is a very valuable certificate. It is also desire of many other people because their jobs can become very reliable.
    https://www.dumpstool.com/Salesforce.html

    ReplyDelete
  6. Thanks for the valuable Content. I really learned a lot here. Do share more like this.
    Sales Force Testing
    Salesforce Project Documentation

    ReplyDelete
  7. The education is necessary because through this you become an expert and get acknowledge about different important aspects. DumpsExpert products contain information that has been extracted from authentic and reliable sources. https://www.dumpsexpert.com/Salesforce-Dumps.html dumps pdf offers you updated knowledge about all the contents of the syllabus and will enable you to pass it with flying colors. If you need to go through the entire syllabus and learn each and every topic, choose our Study Guides that equip you with the best knowledge.

    ReplyDelete
  8. Authenticity and simplicity are the characteristics of Identity-and-Access-Management-Designer which attracted me towards this study material. A person with normal background knowledge can easily pick all the ideas easily from this material. I will strongly suggest to all my fellows to prepare from Identity-and-Access-Management-Designer so you can also be find pleasure like me. It tells you about how to put your effort at the right place and in the right direction.

    For More Info Visit: https://www.dumpspool.com/salesforce/identity-and-access-management-designer-dumps.html

    ReplyDelete
  9. Well stated, you have furnished the right information that will be useful to everybody. Thank you for sharing your thoughts. Security measures protect your company not only from data breaches, but also from excessive financial losses, a loss of people's trust, and potential risks to brand reputation and future benefits.
    IT infrastructure services
    Cybersecurity Service Provider

    ReplyDelete
  10. Thanks for sharing this useful blog. Keep it up. Please visit
    Salesforce classes in pune

    ReplyDelete