Comments on: 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 Technology, Travel, and Pictures Sun, 01 Mar 2020 00:09:41 +0000 hourly 1 https://wordpress.org/?v=6.0.1 By: Wendy https://www.justinsilver.com/technology/salesforce/salesforce-sobject-id-validation-apex/#comment-2655 Thu, 01 Aug 2019 22:57:56 +0000 http://justinsilver.com/?p=3989#comment-2655 Excellent solution! It worked for me.

]]>
By: Justin Silver https://www.justinsilver.com/technology/salesforce/salesforce-sobject-id-validation-apex/#comment-2162 Fri, 20 May 2016 17:30:35 +0000 http://justinsilver.com/?p=3989#comment-2162 In reply to Chris Yorkston.

That’s a good solution – have you tested it? Coming from a Java background it doesn’t immediately make sense to me that String instanceof Id == true, but in the Apex world I don’t see a reason why it wouldn’t, and I would think it’s more efficient.

J

]]>
By: Chris Yorkston https://www.justinsilver.com/technology/salesforce/salesforce-sobject-id-validation-apex/#comment-2159 Thu, 19 May 2016 13:39:13 +0000 http://justinsilver.com/?p=3989#comment-2159 Hi,

How about using instanceof keyword to check this String value is an Id. Then check the sObject Type matches what you are looking for.

public static Boolean isValid(String stringValue, Schema.SObjectType sObjectType) {
    Id sObjectId;
    if(isId(stringValue)) sObjectId = (Id)stringValue;
    return isValid(sObjectId, sObjectType);
}

public static Boolean isValid(Id sObjectId, Schema.SObjectType sObjectType) {
    return !(sObjectId==null || sObjectId.getSObjectType()!=sObjectType);
}

public static Boolean isId(String stringValue) {
    return stringValue InstanceOf Id;
}
]]>