45

You don't need to hard code the Object name as you have specified, you can indeed do it dynamically, for example: SObjectType accountType = Schema.getGlobalDescribe().get('Account'); Map<String,Schema.SObjectField> mfields = accountType.getDescribe().fields.getMap();


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 "...


22

The docs suggest you use the newer Schema.describeSObjects(List) method - that lets you pass one or more names of objects as a list, and get back the results. Here's the sample code: // sObject types to describe String[] types = new String[]{'Account','Merchandise__c'}; // Make the describe call Schema.DescribeSobjectResult[] results = Schema....


22

Get All Parent objects. for(Schema.SobjectField strFld: Account.SobjectType.getDescribe().fields.getMap().Values()) { if(strFld.getDescribe().getType() == Schema.DisplayType.REFERENCE) { system.debug('==parent object='+strFld.getDescribe().getReferenceTo()); } } Get All Child objects. Schema.DescribeSObjectResult R = Account....


20

This is possible through Apex, but not SOQL --- and no Metadata API required. To determine whether a given SObject's RecordType is available to a given user's profile, you will need to use the Apex DescribeSObjectResult getRecordTypeInfos() call on that SObject (see the docs here). This returns a list of RecordTypeInfo objects corresponding to all ...


20

You cannot do it with SOQL. You need describe call for ( Schema.SObjectType o : Schema.getGlobalDescribe().values() ) { Schema.DescribeSObjectResult objResult = o.getDescribe(); system.debug( 'Sobject: ' + objResult ); system.debug( 'Sobject API Name: ' + objResult.getName() ); system.debug( 'Sobject Label Name: ' + objResult.getLabel() ); ...


17

Despite what the docs would seem to indicate you are allowed to call getDescribe() on over 100 SObjectType objects in order to get their key prefix (the limits seem to apply to enumerating fields and global describes). I have an answer on a similar question with details on how to implement your idea, but there is a much cleaner way of doing this. To quote ...


17

Once you have the sObjectType you should be able to construct the sObject instance using the sObjectType.newSObject(ID Id) method. You can get the sObjectType from the Id using the Id.getSObjectType() method; Map<Id, String> idsToUpdate = new Map<Id, String>(); // Put the Id's and associated name values in the map List<SObject> ...


13

When treated as a String, the first three characters of ParentId represent the object prefix, which can be used to search for the object in the getGlobalDescribe map. for example. // This will be set to the object description metadata if found Schema.DescribeSObjectResult object_desc = null; // Get the Id we're searching for String parent_id = feed_item....


13

Check out the documentation for Dynamic DML and family of Describe() methods String typeName = 'Account'; Schema.SObjectType targetType = Schema.getGlobalDescribe().get(typeName); sObject obj = targetType.newSObject(); // now either cast it to Account and proceed // Account a = (Account) obj; // a.Name = 'Abc'; // or use sObject's generic get() and put() ...


13

Schema.DescribeSObjectResult r = User.sObjectType.getDescribe(); Map<String,Schema.SObjectField> M = r.fields.getMap(); for(String fieldName : M.keySet()) { Schema.SObjectField field = M.get(fieldName); Schema.DescribeFieldResult F = field.getDescribe(); // A nillable field can have empty content. // A ...


13

You can get the label using the DescribeSObjectResult: DescribeSObjectResult describe = SObjectType.MyApiName__c; system.debug(describe.getLabel()); Another common syntax is: DescribeSObjectResult describe = MyApiName__c.sObjectType.getDescribe(); You can also just one-line it: system.debug(SObjectType.MyApiName__c.getLabel());


13

There is no difference except the latter becomes less verbose when you drop the optional Schema. portion. Every time you type: SObjectType.Account instead of: Account.sObjectType.getDescribe() you save 14 characters. Here are some examples of the flexibility you have here. Schema.SObjectType accountType = Schema.Account.sObjectType; accountType = ...


12

No, but the limits around field describes are probobly much more generous than you think. You're allowed up to 100 static compile-time field references (e.g. Schema.SobjectType.Contact.fields.name.label). The limits for dynamic describes are even more generous in most situations, and can be combined with this compile-time format for more flexibility. ...


11

You can obtain the object type from an Id directly using the Id.getSObjectType method (which I don't think was around the first time this question was answered btw). Without having to describe all objects and filter by the key prefix (which is also still technically a correct answer though is less direct now). Returns the token for the sObject ...


11

You can query for apex pages, returning a list of selectOption instances as you would for any other custom list: // controller public String chosen {get; set;} public List<SelectOption> GetPageOptions() { List<SelectOption> pages = new List<SelectOption>(); for(ApexPage p : [select Id, Name from ApexPage limit 1000]) { // use ...


10

Looking through the Spring 13 release notes (pg 121) I would expect the new fields to have "Code" appended. E.g. Account.ShippingCountry becomes Account.ShippingCountryCode. This lines up with the online docs for Account: ShippingCountryCode (beta) Type picklist Properties Create, Filter, Group, Nillable, Sort, Update Description The ISO ...


10

If a visualforce tab is created for the page "abc" then even "/apex/abc" will take you to the visualforce tab only. And to highlight particular tab you will use the following code as below <apex:page controller="abcctrl" tabstyle="tabname__tab" id="ThePage"> The _tab when appended with actual tabname and used in attribute tabstyle of the page the ...


10

isQueryable means the object is queryable as a top level object, e.g. you can do select id from Foo__c Some objects, e.g. Name are marked as isQueryable=false, and are only accessiable as a relationship from another object, so trying to do select id from name will return an error "entity type Name does not support query", but you can do select who....


10

As per the documentation, Group.Name is required. Name Type: string Properties: Create, Filter, Group, Sort, Update Description: Required. Name of the group. Corresponds to Label on the user interface. As you found, the Metadata on this field also reports nillable = false. At least as far as you are concerned for Groups you can create or edit the ...


10

Here's a method I've found. It's more explicit than using the permissions object. Fields SELECT QualifiedApiName FROM FieldDefinition WHERE EntityDefinition.QualifiedApiName = 'Account' Objects SELECT QualifiedApiName FROM EntityDefinition order by QualifiedApiName


9

The most accurate and quickest way is to loop over the list of valid target Objects of the WhatId field on the Task or Event object, as this list will change whenever you check/uncheck the "Allow Activities" checkbox on a given Object: Set<Schema.SObjectType> objectsWithActivitiesEnabled = new Set<Schema.SObjectType>(); String objectNames = ''; ...


9

If you only have the key prefix you can pad it out with the empty id string: String emptyId = '000000000000AAA'; String prefix = '00D'; ID myId = prefix + emptyId; System.debug(myID.getSObjectType().getDescribe().getName()); https://vffiddle.herokuapp.com/415/11


9

You can check this by global describe : public Boolean hasField(String objectname, fieldname) { Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); Schema.DescribeSObjectResult r = gd.get(objectname).getDescribe(); Map<String, Schema.SObjectField> FsMap = r.fields.getMap(); if(FsMap....


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 ...


8

It seems something like that is possible with: System.Type objType = Type.forName(objName); Object obj = objType.newInstance();


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

You are so close, this is not because you are using an External Id, it is because you are not using the right SObject method. I have been caught out by this many times as well. The correct syntax is: new_obj.putSObject('Account__r', new Account(External_ID__c='123'));


8

You can use Schema.SObjectType to get the object type dynamically: String obj = 'Account'; Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe(); Schema.SObjectType sobjType = gd.get(obj); Schema.DescribeSObjectResult describeResult = sobjType.getDescribe(); Map<String,Schema.SObjectField> fieldsMap = describeResult.fields.getMap();


8

Elegant? Perhaps not, but checking 'Name' first and only iterating as a fallback plan should be more efficient. With every custom object and most standard objects, this shortcut will cut out the looping and additional describes entirely. My natural inclination is to start with a positive condition: public static String getNameField(SObjectType sObjectType) ...


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