Tuesday, September 3, 2013

Listing Required Fields for an Object in Salesforce Using Apex Code

Sometimes it's desirable or necessary to get a list of all required fields for a particular Salesforce object, especially for subsequent processing in Apex. Instead of hard-coding this list, which is not sustainable or maintainable long-term, a better approach is to use Salesforce field describe results and the isNillable() method.

Below is sample code that demonstrates this approach, code that can be adapted for any standard or custom object.

2 comments:

  1. getting an error "Unexpected Token: Map"

    ReplyDelete
    Replies
    1. I apologize for the delayed response, devi! Thank you for the feedback, and it turns out the problem was a problem with how I pasted code into a pre tag. I replaced the pre tag with SyntaxHighlighter, which hopefully makes it easier for you to copy/paste.

      Delete