Skip to main content
30 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()) ...
cropredy's user avatar
  • 72.1k
26 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 ...
dana's user avatar
  • 1,167
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 ...
Adrian Larson's user avatar
  • 151k
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')); // ...
Adrian Larson's user avatar
  • 151k
16 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 ...
Keith C's user avatar
  • 136k
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, ...
Adrian Larson's user avatar
  • 151k
14 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 ...
cropredy's user avatar
  • 72.1k
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> ...
Adrian Larson's user avatar
  • 151k
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":"...
gNerb's user avatar
  • 9,510
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()....
sfdcfox's user avatar
  • 497k
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: ...
Adrian Larson's user avatar
  • 151k
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, ...
Keith C's user avatar
  • 136k
11 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 ...
sfdcfox's user avatar
  • 497k
10 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 ...
Kasper's user avatar
  • 2,907
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 = '...
Oleksandr Berehovskyi's user avatar
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 ...
David Reed's user avatar
  • 93.2k
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 ...
Oleksandr Berehovskyi's user avatar
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....
sfdcfox's user avatar
  • 497k
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:...
psun's user avatar
  • 371
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()), '...
Bart Juriewicz's user avatar
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 ...
sfdcfox's user avatar
  • 497k
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 ...
Adrian Larson's user avatar
  • 151k
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 ...
Matt Lacey's user avatar
  • 25.7k
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 ...
sfdcfox's user avatar
  • 497k
8 votes
Accepted

Big Object not deploying to Salesforce

When you deploy "Check Only", you are just validating, not actually committing your changes. Remove that selection (i.e. Select only Single Package) and you will be good to go. Note: As stubborn for ...
Sebastian Kessel's user avatar
8 votes

enableChangeDataCapture' not valid in version 38.0

The enableChangeDataCapture property is only available as of API version 41.0. Errors like this typically happen when the package.xml you use to retrieve metadata with has a different version than the ...
John Towers's user avatar
  • 3,873
8 votes
Accepted

Benefits of using sObject.clone versus creating a new record

There was a question a while back that can provide some context here: How to avoid instantiating object inside a loop? To summarize: Cloning is very fast Using the SObject constructor to set name-...
Derek F's user avatar
  • 62.7k
8 votes

How would I test a trigger on non-writable SurveyResponse object?

This isn't possible today. I've reached out to the Surveys team to ask them either mark the object type as not available for Apex triggers, or make it possible to test Apex triggers on it.
ca_peterson's user avatar
  • 23.1k
8 votes

How to accept generic "SObject" in Lightning Web Component for Flows?

I thought I would come back to this question because this is now possible I believe using propertyType. <targetConfig targets="lightning__FlowScreen"> <propertyType name="T&...
brovasi's user avatar
  • 5,685
7 votes

Setting field values generic SObject

If you want to put values into fields, you can use the generic put methods: SObject record = Id.valueOf('a0Ci000000vd7xA').getSObjectType.newSObject(); record.put('emp_name__c', 'test'); Or you can ...
Adrian Larson's user avatar
  • 151k

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