sObject Archives - Justin Silver https://www.justinsilver.com/tag/sobject/ Technology, Travel, and Pictures Sun, 01 Mar 2020 00:09:41 +0000 en-US hourly 1 https://wordpress.org/?v=6.0.1 https://www.justinsilver.com/wp-content/uploads/2013/06/cropped-apple-touch-icon-160x160.png sObject Archives - Justin Silver https://www.justinsilver.com/tag/sobject/ 32 32 Salesforce sObject Id Validation in Apex https://www.justinsilver.com/technology/salesforce/salesforce-sobject-id-validation-apex/?utm_source=rss&utm_medium=rss&utm_campaign=salesforce-sobject-id-validation-apex https://www.justinsilver.com/technology/salesforce/salesforce-sobject-id-validation-apex/#comments Thu, 10 Dec 2015 20:50:22 +0000 http://justinsilver.com/?p=3989 Salesforce sObject Id‘s are specially formatted strings used to uniquely identify sObjects. If you want to test the validity of an sObject Id in Apex, you can go the simple route of checking the...

The post Salesforce sObject Id Validation in Apex appeared first on Justin Silver.

]]>
AmpedSense.OptimizeAdSpot('AP'); AmpedSense.OptimizeAdSpot('IL'); AmpedSense.OptimizeAdSpot('IR');

Salesforce sObject Id‘s are specially formatted strings used to uniquely identify sObjects. If you want to test the validity of an sObject Id in Apex, you can go the simple route of checking the length, the slightly more complex route of writing a regular expression, or even go as far as to implement the logic parsing of ID values. An easier way to get accurate testing of validity is to create an instance of the sObject you want to test against, then try to assign your sObject Id to it, and catch any Exceptions. This will validate both the format, as well as the type of the sObject Id.

The Util Code

public abstract class MyUtilClass {
    /**
     * Test a String to see if it is a valid SFDC  ID
     * @param  sfdcId The ID to test.
     * @param  t      The Type of the sObject to compare against
     * @return        Returns true if the ID is valid, false if it is not.
     */
    public static Boolean isValidSalesforceId( String sfdcId, System.Type t ){
        try {

            if ( Pattern.compile( '[a-zA-Z0-9]{15}|[a-zA-Z0-9]{18}' ).matcher( sfdcId ).matches() ){
                // Try to assign it to an Id before checking the type
                Id id = theId;

                // Use the Type to construct an instance of this sObject
                sObject sObj = (sObject) t.newInstance();
     
                // Set the ID of the new object to the value to test
                sObj.Id = id;

                // If the tests passed, it's valid
                return true;
            }
        } catch ( Exception e ){
            // StringException, TypeException
        }

        // ID is not valid
        return false;
    }
}

You can then test the validity of the sObject Id by passing it into the util method along with the System.Type of the sObject you want to compare against. The type can be accessed using sObject.class.

Call From Apex

// maybe a valid Account.Id, maybe not?
String theId = '001xxxxxxxxxxxx'; 

if ( MyUtilClass.isValidSalesforceID( theId, Account.class ) ){
    // do something if it is valid
}

The post Salesforce sObject Id Validation in Apex appeared first on Justin Silver.

]]>
https://www.justinsilver.com/technology/salesforce/salesforce-sobject-id-validation-apex/feed/ 3