29
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())
...
28
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)
{
...
24
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 ...
22
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 ...
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'));
// ...
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 ...
14
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, ...
13
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> ...
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":"...
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()....
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:
...
11
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 ...
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, ...
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 = '...
10
votes
Accepted
Delete an SObject by knowing only Id
Yes, you can delete an object knowing only its Id.
If you know the object's type at development time, you can initialize an sObject instance with the Id and supply it to the delete DML operator, as ...
10
votes
Apex - break list into multiple small lists before processing
well, you can use the following my class that I have written with help of community to split List into parts. To read about main logic of splitting you can check How to write a generic list method ...
10
votes
Accepted
How to loop trough second level deserializeUntyped Json String
The result from JSON.deserializeUntyped is either a List, Map, String, Boolean, or Decimal/Integer. You can detect these conditions dynamically, even recursively if you want to.
Object source = JSON....
9
votes
Can I tell if I queried a field?
Use Map<String, Object> getPopulatedFieldsAsMap() method on the sobject. It gives you a map of fields and values that got queried.
Please refer to this release notes for more information: (http:...
9
votes
How do I get the list of fields of sObject
One liners:
Sobject__c.sObjectType.getDescribe().fields.getMap().keySet()
Or for dynamic queries:
String.join(new List<String>(SObject__c.sObjectType.getDescribe().fields.getMap().keySet()), '...
9
votes
9
votes
Accepted
Dynamic Schema getDescribe
Instead of Account.Type (a specific field), you can use:
// Get the describe for the object
DescribeSObjectResult objResult = Schema.getGlobalDescribe()
.get(...
9
votes
Cast a Map <Id, sObject> to Map <Id, Opportunity>
The .values() method will return a list of sObjects, which means that you can simply follow the same scenario and cast the list to a concrete sObject type:
for (Opportunity opp : (List <...
9
votes
Accepted
Strange Behavior with null sObject in Apex Class
It's a feature (mostly). The most general use is to allow you to bypass checking for null pointers from query results. For example, consider the following code:
for(Contact record:[SELECT Account.Name ...
9
votes
Accepted
How to check if SObject has certain field?
All the keys in the map are in lower case for some reason. I get a positive result for ownerid but not for OwnerId. I checked for our account object and all the keys in the map are lowercase.
This i ...
9
votes
Accepted
How to use GetSObject to retrieve related data in a null safe manner?
You need to add a null check. The only reason this would fail is if your parent record is null. One option may be to just get the value statically:
String text = record.Parent__r.TextField__c;
If ...
9
votes
Does the `getSObjects` method on `SObject` work with Person Account child relationships?
Ok, I'm guessing Salesforce might hide their known issues from being crawled by search engines. Typed in getSObjects into the success community and the first thing that appeared was a known issue ...
9
votes
Why does Salesforce automatically put the record Id in the Name field?
If you create a record via an API (e.g. Apex code) without a name value provided, Salesforce automatically uses the new ID of the record for its name. This is necessary because the Name field is ...
8
votes
Accepted
Case-Insensitive Maps?
The docs outline the behaviour of the DescribeFieldResult map. See Accessing All Field Describe Results for an sObject
There's a note saying:
The value type of this map is not a field describe ...
8
votes
Accepted
Do email results come in the same order as the list of emails
To answer the specific question, the sendEmail method returns a list of Messaging.SendEmailResult objects. This list will be in the same order as the corresponding list of emails provided to sendEmail(...
8
votes
Accepted
Pros and cons of generic SObject list vs multiple lists of objects
For cons, using SObject instead a concrete type restricts the usage of upsert by external ID, and you can't upsert generic SObject lists (the variable may be SObject[], but the actual list in memory ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
sobject × 626apex × 297
soql × 59
list × 40
rest-api × 36
map × 34
visualforce × 33
describesobject × 33
trigger × 30
json × 26
custom-object × 22
sobjecttype × 21
sobjectfield × 21
fields × 20
unit-test × 19
query × 18
soap-api × 17
schema × 17
lightning-aura-components × 15
custom-field × 15
standard-objects × 15
dynamic-apex × 15
javascript × 14
object × 13
relationships × 12