Hot answers tagged

43 votes
Accepted

Apex Cast sObject list dynamically to a specific sObject Type

I know this approach is strange because you are still working with a List<SObject>, but when you assign it you can make it more specific (e.g. List<Account>) by using Type.forName and Type....
user avatar
  • 143k
41 votes
Accepted

Howto wildcard SELECT * to query all fields of objects in APEX?

Update: Spring '21 Release (API v51+) adds new SOQL FIELDS function can select all fields of an object. Refer to documentation Knowing that new FIELDS function must have a LIMIT of at most 200, when ...
user avatar
  • 14.5k
27 votes
Accepted

How to convert a string to sObjectType

An alternate way to do this without going through Schema.getGlobalDescribe(): String myString = 'Account'; SObjectType sObjType = ((SObject) Type.forName(myString).newInstance()) ...
user avatar
  • 63k
27 votes
Accepted

Get all Parent and child objects related to the current object

Get All Parent objects. for(Schema.SobjectField strFld: Account.SobjectType.getDescribe().fields.getMap().Values()) { if(strFld.getDescribe().getType() == Schema.DisplayType.REFERENCE) { ...
user avatar
  • 22.1k
26 votes
Accepted

What is a use case where one would use an SObject as a Map key?

I use SObject as a key when I want to use a composite key. For example, say I want to find duplicates based on first name, last name, and email. I might loop through a list of contacts, and create a ...
user avatar
  • 438k
25 votes

How to convert a string to sObjectType

Here is the way: // I have Account name in String String objectStr = 'Account'; // Convert to schema.sObjectType Schema.SObjectType convertType = Schema.getGlobalDescribe().get(objectStr); // ...
user avatar
  • 22.3k
23 votes
Accepted

Get SObject by Id

I'm not sure you could do exactly that, but what you could do is use the Id instance class to achieve what you need, for example: Schema.SObjectType sobjectType = myId.getSObjectType(); String ...
user avatar
22 votes

Is it possible to convert an SObject to a Map?

The sObject has a native function getPopulatedFieldsAsMap that converts from an sObject to a Map<String,Object>. This was released in Summer '16 which is probably why the other answers resort ...
user avatar
  • 957
21 votes

Howto wildcard SELECT * to query all fields of objects in APEX?

I suggest that rather than relying on some utility class, you become familiar with the platform's APIs so you can compose the appropriate solution each time you want to query all the fields, ...
user avatar
  • 130k
21 votes
Accepted

Clone list of SObject records using deepClone() to clone all the fields on records

Yes, deepClone will only pull in fields you have in memory. In this case, you do indeed have to query for them. You should question why you need every field, but if you want to maintain a configurable ...
user avatar
  • 143k
19 votes

Howto wildcard SELECT * to query all fields of objects in APEX?

I created some helper classes for this. You can find them here. There are 3 classes: xt, xs and xr. They contain also some other tools I couldn't separate for this answer, but the method you need is ...
user avatar
  • 33.7k
18 votes

How to get ID prefix if you know the object name

As a regular user, the only way would be to either attempt to create a record from a related object then look at the URL to get the prefix or view an existing record and look at the url. If the user ...
user avatar
  • 53.2k
17 votes
Accepted

How to get to which object the field has reference

You can get this data on the lookup field using the DescribeFieldResult.getReferenceTo() method. From that field describe result you can retrieve the related object's name or the display label. // ...
user avatar
  • 22.4k
16 votes
Accepted

How to use addError() with a dynamically derived field name?

You could vote for this Provide an additional addError method on SObject that accepts a field name idea, but as it has got nowhere in the last 5 years there is no reason to expect any progress in the ...
user avatar
  • 130k
16 votes
Accepted

Working with SObjects instead of concrete custom objects (i.e., abstraction in apex)

Mechanics You need to use the get and put methods. Each method supports both String and SObjectField as the parameter type: // terse myObject.put('UniqueId__c', myObject.get('ExternalId__c')); // ...
user avatar
  • 143k
15 votes

Values of SObject map are lost after a DML call

Using complex objects as Map keys can have unexpected consequences. In your example, the insert causes the Id of the key Account to be updated so the hashCode and equals methods of the Account ...
user avatar
  • 130k
14 votes
Accepted

How to compare two sObjects from 2 List<Lead>()?

The comparison operators don't work the way you think they do, not even across all objects. Let's focus on SObject and SObject[] types for this question, since that's your question. == acts as a deep ...
user avatar
  • 438k
13 votes
Accepted

Can someone please explain what is meant by sObject?

An SObject represents a specific table in the database that you can discretely query. The API Name is what you reference as ending in __c. Standard SObjects have names like Account or Opportunity, ...
user avatar
  • 143k
13 votes
Accepted

How to send wrapped generic SObjects from Lightning to Apex

I know that when you serialize an SObject it has some extra information on it that you might not expect: Contact c = new Contact(); system.debug(json.serialize(c)); Result: {"attributes":{"type":"...
user avatar
  • 9,121
12 votes
Accepted

Utility Methods flexible enough for all sObject Types

You don't need to have a return type to get data back to the callee, and sometimes this is more convenient. Here's my version of a "get values from records" implementation: public static void ...
user avatar
  • 438k
12 votes
Accepted

Setting field values generic SObject

You don't need to go through the complicated method you went through to get the new record. The following works just as well: Id objId = 'a0Ci000000vd7xA'; SObject record = objId.getSObjectType()....
user avatar
  • 438k
12 votes
Accepted

How to initialize sObject with Relational fields?

You need to use the name pointing reference: MyObject__c record = new MyObject__c(); record.Parent__r = new Parent__c(Name='Populated'); The above is functionally equivalent to putSObject: ...
user avatar
  • 143k
11 votes
Accepted

Using a generic SObject in VisualForce Page

This works fine for me. Ran a couple tests and all of the below work. VF Page <apex:page controller="GenericTest"> {!test['name']} {!test1.obj['name']} <apex:repeat value="{!...
user avatar
11 votes

What is a use case where one would use an SObject as a Map key?

Sfdcfox explains a great reason to use this, but at a more abstract level the reason it's supported it because maps can have any type as a key. In a model where custom classes, or any other type, can ...
user avatar
  • 22.2k
11 votes
Accepted

How to create nested JSON object from custom sObject?

I suggest building and serializing nested maps for this: Map<String, Object> m = new Map<String, Object>(); for (options__C opt:options) { m.put(opt.Name__c, new Map<String, ...
user avatar
  • 130k
11 votes
Accepted

Collection store exception adding all List<Opportunity> to List<Account>

Instantiate a new generic list and it won't carry over any type information. Simply casting to List<SObject> doesn't remove the fact it was originally a List<Account>. List<SObject> ...
user avatar
  • 143k
10 votes

Utility Methods flexible enough for all sObject Types

It is absolutely possible! We have even built this exact utility where I work. However, I can't just give you the answer, but I will give some hints. SObject methods: get will return the value of the ...
user avatar
  • 143k
10 votes
Accepted

Name field of Group sObject is Not Nillable but returns null

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 ...
user avatar
10 votes

What is the object that stores "files"

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 ...
user avatar
  • 63k
10 votes
Accepted

Get Object API name of any Lookup field

Use Schema.getGlobalDescribe() method that accepts String as API name of object and then use getMap() method to get map of SObjectFields String objectName = 'Opportunity'; String fieldName = '...
user avatar

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