Hot answers tagged

26

No no, the field (API) name is Schematic__c, you need to start from it. Think about Schematic__r (the rel. name) like a consistent table alias when you'd make JOINs in regular database, nothing more. To go "up": Schema.DescribeFieldResult f = Schema.sObjectType.Order_Line_Item__c.fields.Order__c; System.debug(f.getRelationshipName()); // Outputs "...


20

Thanks to the comment of @KeithC I was able to go on with my research and as a result, it seems not possible to get a primitive data-type dynamically as a string. There are concepts to come close to it by the usage of instance of or try/catch in brute-force-like manner. Since the primitive types are just a few, this workaround with instanceof seems ...


16

The isAccessible, isCreateable, and isUpdateable properties on DescribeFieldResult actually incorporate both Object Security and Field Security information for the given Field and its Object. Essentially it's a two-tier result --- first the Object's Security is considered, then additional Field properties are considered. In general, if a User has Create ...


12

You can use JSON.deserializeUntyped() to convert any JSON-serialized SObject into a Map<String,Object> containing as its keys fields actually present on that SObject. Using your example with Account: Account myAcc = new Account(Name = 'Doug', BillingCity=null); // Serialize our Sobject into JSON String s = JSON.serialize(myAcc); // Deserialize it ...


11

I may be missing the boat here but why not a method based on the following? // Build a map of hashcodes for each fieldDescribe taken from Schema Global Describe Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); Map<Integer,Schema.SObjectType> fldHashCodeToSObjectTypeMap = new map<Integer,Schema.SObjectType>(); for (String ...


11

You could get this with apex describe methods: for ( Schema.ChildRelationship scr : Account.sObjectType.getDescribe().getchildRelationships() ){ system.debug(''); system.debug('**** Child SObject **** ' + scr.getChildSObject()); system.debug(' Field **** ' + scr.getField()); system.debug(' Relationship Name ...


10

Finally there is a better solution to this. In the Summer '16 release, Salesforce have exposed a new method getPopulatedFieldsAsMap() This does exactly what you would expect it to, returns fieldname -> fieldvalue map for only those fields explicitly populated on this instance of the sObject. We no longer need to serialize and deserialize JSON Example of ...


10

On the Mac, my favorite tool is SoqlXplorer: http://www.pocketsoap.com/osx/soqlx/


10

Force.com Platform Apex Versioning and Feature/Field Visibility The platform (or more specifically Apex runtime) often (but not always) conditions the visibility of various features (including fields on objects) based on the API version (linked with platform version) associated with Apex code file containing the code being run. This way new features ...


10

The closest I could figure out with the approaches that we have right now you see below. Note that as an unfortunate we still can't detect sets and maps and we can't distinguish between decimal and double. The rest feels usable. public class xs { public static string getType(Object o) { if(o==null) return ''; // we can't say much about ...


10

Schema.DescribeFieldResult F = <Object_API_Name>.<Field_API_Name>.getDescribe(); Schema.sObjectField T = F.getSObjectField(); List<PicklistEntry> entries = T.getDescribe().getPicklistValues();


10

Interestingly enough, I discovered this yesterday. Open your Developer Console, click on the Query Editor tab, click the "Use Tooling API" checkbox, then query away! SELECT DeveloperName, TableEnumOrId FROM CustomField WHERE CreatedDate = TODAY If you want to limit it to a single table, you can also filter by TableEnumOrId: Standard Object SELECT ...


9

How about a JSON roundtrip: SObject so = new Account(Name='foo'); String s = JSON.serialize(so); Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(s); system.debug(m.keySet().contains('Name')); This will not tell you if the sobject definition has the field, but only if the instance has it (has been set, or has been read ...


9

I'll answer your second question first. To get the child records if you know the relationship name, you pass a String argument to the SObject.getSObjects method: MyObject__c parent = new MyObject__c(); String relatioonshipName = 'Children__r'; List<Child__c> children = parent.getSObjects(relationshipName); Now back to your first question. There is ...


8

Another fantastic one in the toolkit is workbench. Lots of admin tools, including the ability to explore all standard (even the behind the scenes ones) and custom object. Also good to have a bookmark to the Object Reference handy.


8

There is no way to get Id for a custom or standard field except it you can retrieve name, label type, attributes etc.. The only help can be made is custom field id start from prefix 00N and in production CF appended to 00N In addition there is a idea for which you can vote. Conclusion: getDescribe() don't support id for field.


8

In short after taking a deeper looker at this for you, I have to conclude the answer to your question is that you can only be partially successful in emulating the filters used by Schema Builder... The isCustom method will help with the first obviously, the second two are harder to separate. The following uses the fact that Schema Builder seems to favour ...


8

While I really hope there is an easier way to achieve this, the Tooling API might be one candidate. You can interrogate the Salesforce Tooling API with an Apex library or just use the code below by itself: First specify your field name: //choose your field name (without suffix!) String fieldName = yourField.getDescribe().getName().substringBefore('__c'); ...


8

The best thing I came across is https://schemalister.herokuapp.com/. No installation required since it is hosted as a Heroku app.


8

Here is code that will give list of all objects excluding Share, history , Tag & feed. You can add extra condition to filter out other unnecessary sObjects. List<string> SObjectList = new List<string>(); for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values()){ String name = objTyp.getDescribe().getName(); // Exclude all ...


8

My strategy here would simply be to describe a picklist field that implements the Global Picklist. SObjectField picklistUsingGlobal = SObjectType.MyObject__c.fields.PicklistUsingGlobal__c; List<PicklistEntry> entries = picklistUsingGlobal.getDescribe().getPicklistValues();


7

You can query the RecordType object to find out the record types that way. List<RecordType> rts = [select Id from RecordType where SObjectType = :MyObject__c.SObjectType ]; if (rts.size() > 0) { // MyObject__c has record types }


7

The actual "content" of the file is saved as a ContentVersion ContentVersion can have custom fields ContentVersion is a child of ContentDocument - an object implicitly created by SFDC when the first ContentVersion is created for that file. Subsequent file versions are save as the 2nd, 3rd, ... children of ContentDocument You can think of ContentDocument as ...


6

I found that the describe map was only partially case-insensitive:- Schema.getGlobalDescribe().get('CustomObject__c'); // works Schema.getGlobalDescribe().get('customobject__c'); // works Schema.getGlobalDescribe().get('ns__CustomObject__c'); // works Schema.getGlobalDescribe().get('Ns__CustomObject__c'); // doesn't work So object names are ...


6

I think I found my answer --- according to the docs on Salesforce Primitive Data Types: AnyType is used within the Force.com platform database exclusively for sObject fields in field history tracking tables.


6

Here is an easy way to get default values for all fields on an SObject Foo__c f = (Foo__c)Foo__c.sObjectType.newSObject(null, true); The first argument to newSObject(..) is the recordType Id The second argument, when true, says to create the sobject with defaultValues Then, you can use methods like f.get(Foo__c.MyField__c); // or ... f.get(some variable ...


6

You can tell because when you describe a Rollup Summary Field, its describe returns null when you call getCalculatedFormula(), unlike a Formula Field. system.assertEquals(null, rollupDescribe.getCalculatedFormula()); system.assertNotEquals(null, formulaDescribe.getCalculatedFormula());


5

It is error prone, but possible to do well. root.getSObject('Account__r').get('Id'); aka root.getSObject(currentLevel).get(currentField); To figure out the gotchas and parsing/mapping strategies, look at some libraries like Java OGNL or JSP EL. They are possible to emulate and do dynamic deep querying while learning from their designs. Wish there was ...


5

Does Schema.GetGlobalDescribe() Account for Prefixes? Yep! Use SObjectType.Obj__c.Name to resolve the namespace, for example: Static method: String fullyQualifiedName = SObjectType.Obj__c.Name; System.debug(fullyQualifiedName); //ns__Obj__c String label = Schema.GetGlobalDescribe().get(fullyQualifiedName).getDescribe().getLabel(); System.debug(label); //...


5

In my dev org this apex: Schema.SObjectType.Contact.fields.LastModifiedById.isCreateable() Evaluates to true. There is a black tab option that salesforce support can enable that does allow audit fields like LastModifiedById to be creatable (but not editable). I strongly suspect this was enabled in your org when you checked this.


Only top voted, non community-wiki answers of a minimum length are eligible