Questions tagged [sobject]

An sObject represents an object, such as an Account or Campaign. An sObject is the base object for all objects in Apex.

Filter by
Sorted by
Tagged with
59 votes
1 answer
113k views

Using sObject.get() to fetch a field value from a related parent object

I have a contact in a generic sObject in apex and while I'm able to easily get detail field values using sObject.get('MyContactField__c') I'm not able to get fields off the related parent object, ...
greenstork's user avatar
  • 14.7k
47 votes
6 answers
78k views

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

I have created sObject and I need all the fields and I have many. Is there a way I can directly get all of them rather then typing each one of them.
NewBei's user avatar
  • 473
41 votes
1 answer
20k views

Infering sObject Type from Id or collection of Id's

If I have an instance of an Id variable that has been populated with a value, is there way to infer the sObject type of that Id using a describe call. So if I did: Set<id> parentIds = new ...
pchittum's user avatar
  • 19.6k
35 votes
1 answer
32k views

Error: MIXED_DML_OPERATION on setup and non-setup objects

In my test class i have the creation of a dummy custom setting and a dummy lead: Group g1 = new Group(Name='group name', type='Queue'); insert g1; QueuesObject q1 = new ...
Enry's user avatar
  • 3,696
33 votes
1 answer
7k views

Generic Apex Trigger

So I was kind of throwing round the idea of making a generic apex trigger that is callable by multiple objects. Say you have the following sObjects: Account Sales__c Sales_Contacts__c And I wanted ...
gfppaste's user avatar
  • 845
32 votes
2 answers
67k views

Apex Cast sObject list dynamically to a specific sObject Type

The upsert() DML operation requires a specific List based on a specific sObject type, for instance:List<Account> lstAccount = new List<Account>(); However, we are trying to generate a ...
Robin Wijnen's user avatar
25 votes
3 answers
2k views

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

In Dan Appleman's book Advanced Apex Programming, he explains how using SObjects as Map keys (i.e. Map<SObject, *value*>) is supported but it's poor practice, since the key is actually ...
Brian Mansfield's user avatar
23 votes
2 answers
3k views

Does 'default value' do anything if the object is created through Apex?

I created a currency field on OpportunityLineItem that has a default value set to 0.00. In my test method I create a record and insert it. I then query it back out and try to assert that the field is ...
Ryan Elkins's user avatar
  • 7,637
18 votes
2 answers
2k views

Adding an error on a dynamic referenced field

Is there a way to add an error on a dynamically referenced field? Let's take this simple example: opp.get('Name').addError('Invalid name'); This is not working, I'm looking for a similar minded way. ...
jpmonette's user avatar
  • 4,400
18 votes
4 answers
10k views

Utility Methods flexible enough for all sObject Types

QUESTION I am trying to create a few utility methods that can work with any sObject that gets passed to it and return dynamic data-types. Unfortunately I'm starting to wonder, is this even possible? ...
Xtremefaith's user avatar
  • 4,510
16 votes
2 answers
38k views

How to convert a string to sObjectType

I have a object A where I am saving the name of the object as Strings and the fields also as String. Eg: If I have object Account, contact and opp objects. I am saving object Account as object Name ...
Anu's user avatar
  • 3,817
16 votes
1 answer
28k views

Get SObject by Id

I have an Id and I want to know if it's possible to get the SObject with this unique ID ? Something like : SObject object = getSObjectById(myId); I didn't find a method allowing that !
SF_user's user avatar
  • 3,596
16 votes
2 answers
4k views

What are the advantages of Light Application objects

I was hoping that the introduction of "Light Application object" would do something about the data storage of junction objects, but it doesn't seem to do anything about it. The documentation seems ...
pjcarly's user avatar
  • 7,441
15 votes
3 answers
4k views

How to send wrapped generic SObjects from Lightning to Apex

I have a lightning component with an apex controller that wraps generic records in an inner class. When I send the wrapped records to apex and receive them as generic SObjects, I get this error ...
Xenia's user avatar
  • 153
15 votes
1 answer
8k views

Finding a Custom Object Id (01I...) to get the Setup page URL

I'm trying to find a way to get the "Id" for a custom object's setup page. So I'm NOT trying get a record Id. I'm working on a custom visualforce page and I'm setting up a link so that when the user ...
user7702's user avatar
  • 343
15 votes
1 answer
4k views

Bad rightOperand type: got SObjectScriptRow

I just want to know what is SObjectScriptRow! All I can find about it is this question. I'm building an invocable method to be used from a process, where I have one input parameter: a list of an ...
Jose's user avatar
  • 2,102
14 votes
2 answers
15k views

Generic SObject update pattern

Question I know that it is possible to update a generic of List<SObject>, But what is the best approach to generate that list without knowing the SObjectType, only the ids ? Sample: List<...
Martin Borthiry's user avatar
14 votes
1 answer
4k views

Proper method to get an Sobject instance from an Id

What is the "proper" way of getting an object from an Id? The method I use at the moment is SOQL, and that feels wrong..... Example: Account account = [SELECT Id FROM Account WHERE Id=:inserted....
Mattisdada's user avatar
  • 1,071
14 votes
2 answers
1k views

Case-Insensitive Maps?

How is it that describes seem to return case-insensitive maps? For instance, if I create a new custom setting, the following succeeds: Map<String, SObjectField> fields1 = SObjectType....
Adrian Larson's user avatar
  • 149k
13 votes
1 answer
12k views

Get all Parent and child objects related to the current object

I have a string which contain the name of a sobject. Now I want to get all the objects related to this object. Also want to get which kind of relationship it have with these object.
Anu's user avatar
  • 3,817
12 votes
4 answers
10k views

Best Approach/Way to Query Single SObject record

To query a single record we have below two approach : Approach 1. Assign record to a single Account variable as shown below: Account accountToGet = [SELECT Name From Account where name = '...
Ashish's user avatar
  • 2,362
12 votes
1 answer
214 views

Archaeology: what *was* object type 004?

So we've all eyeballed the various standard object Key Prefixes at some point and noted: 001 is Account 003 is Contact etc I've always wondered: what was 004? What about 002?
Matt and Neil's user avatar
11 votes
1 answer
1k views

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

We have a bunch of custom objects, some of which implement a specific set of custom fields of the same type. (e.g., UniqueID, ExternalID, etc.). I want to be able to write one method that takes and ...
Jorjani's user avatar
  • 2,192
11 votes
2 answers
4k views

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

I'm using Salesforce Survey for one of the requirements. I would like to write a trigger on SurveyResponse object. I've written the trigger and apply the logic that I need. Now, I'm trying to write a ...
Anurag Bhardwaj's user avatar
11 votes
2 answers
789 views

Null record Date field displays todays date

I have a VF page where I am rendering out some subscription information and a couple of the fields are date fields. The issue (sort of) I am seeing is when a record is null and the VF markup calls a ...
Jesse Milburn's user avatar
10 votes
2 answers
43k views

How do I get the list of fields of sObject

My code is myOutput = SELECT location__c, level__c, group__c, count__c from resource__c public sobject[] data = Database.query(myOutput); myOutput is an user input value, based on this query I ...
staticraj's user avatar
  • 103
10 votes
4 answers
11k views

How do I SOSL across objects, matching all records with FIND clause?

To all the expert SOSL gurus lurking out there, I thought this will be an interesting one: I have some SObjects of different types and I want to return their Name values in a single query. In ...
Matt and Neil's user avatar
10 votes
2 answers
1k views

Does the `getSObjects` method on `SObject` work with Person Account child relationships?

Our app uses a lot of dynamic SOQL, and in a client org I'm currently getting an error which I can't explain when we're performing a large query against account, including subqueries for all related ...
Matt Lacey's user avatar
  • 25.3k
10 votes
4 answers
3k views

Required Field on VisualForce Triggers "SObject row does not allow errors" message?

I must be missing something here..I have a visualforce page, that enables a user to create a Campaign - I have a pageblock/section with the Campaign Fields, including name, and Name is marked required....
BritishBoyinDC's user avatar
9 votes
2 answers
14k views

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

I am trying to clone the records of a custom object. I know that I can use the deepClone() method of the List class to do this. List<MyCustomObject__c> originalList = [Select Id, Name, Age From ...
Yogesh D's user avatar
  • 580
9 votes
1 answer
3k views

How to initialize sObject with Relational fields?

In Lightning component we have relational fields (e.g. CustObjA.CustObj__r.Name) which we use in lighthing component. This is on component as, <ui:inputText class="slds-input" value="{!v.Item....
Pasan Eeriyagama's user avatar
9 votes
1 answer
5k views

Using a generic SObject in VisualForce Page

I have a wrapper class that I want to reuse for Both Accounts and Contacts public class SelectableSObject{ public Boolean selected {get; set;} public SObject obj {get; set;} } In the ...
NSjonas's user avatar
  • 10k
9 votes
3 answers
9k views

Cast a Map <Id, sObject> to Map <Id, Opportunity>

I'm trying to implement the trigger framework outlined here: The triggerhandler methods take lists or maps of sObjects as their inputs. In his example he casts list <sObject> as list <...
krewllobster's user avatar
9 votes
2 answers
2k views

Strange Behavior with null sObject in Apex Class

I've observed some strange behavior with null sObjects which are members of an Apex class. Consider the following anonymous block: public class test { public Contact tc; public test(){} } ...
IllusiveBrian's user avatar
9 votes
3 answers
5k views

Is it possible to Upsert a List<SObject> with external ID specified

I'm getting an error when attempting to compile my Apex: Upsert requires a concrete SObject type In my code, I'm unable to create a concrete declaration for the container which I want to upsert. ...
jangiz's user avatar
  • 133
8 votes
1 answer
5k views

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

I know I can add an error to a field as follows: Account someAccount = [select id, name from Account limit 1]; someAccount.name.addError('Bad Name'); How can I do that if the field name is only ...
Kirill Yunussov's user avatar
8 votes
2 answers
933 views

Values of SObject map are lost after a DML call

Can anyone explain the comments in this snippet? Map<Account, Contact> m = new Map<Account, Contact>(); m.put(new Account(Name='Test'), new Contact(Lastname='Test')); SObject[] records = ...
Mossi's user avatar
  • 3,906
8 votes
2 answers
7k views

Set Contains method is not working correctly after overriding equals() and hashcode() methods

I have created one class name like Test and have overriden its equals() and hashcode() methods and then created its 2 objects like below - Test obj1 = new Test (); Test obj2 = new Test (); and now ...
Pramod Kumar's user avatar
  • 5,300
8 votes
3 answers
4k views

Is it possible to pass sObjects to the REST api

I only found solutions that pass objects or primitiv datatypes to the REST API, so do I need to pass these to rebuild my sObject or is it possible to pass it as an sObject JSON? I tried to, but I can'...
itsmebasti's user avatar
  • 6,359
8 votes
1 answer
11k views

Find out the default record type of any Object in Apex

I've to find out the default record type of an Object so that I can use in Apex to insert/update records. Found out that it is at the user level(user profile based). Is there any other way which I ...
Vignesh Damodharan's user avatar
8 votes
3 answers
6k views

Get object sharing setting on Organization-Wide Defaults

Is it possible to know, via apex code, if a certain object is Private, Public Read-Only or Public Read/Write?
Ivo Rocha's user avatar
  • 425
8 votes
1 answer
690 views

Name field of Group sObject is Not Nillable but returns null

I request the metadata for the sObject Group via: GET /services/data/v32.0/sobjects/Group/describe/ HTTP/1.1 In the resulting JSON i see the following metadata at the fields property: { ... ...
kamahl's user avatar
  • 133
8 votes
2 answers
6k views

SObject constructor signature?

when instantiating an SObject type, I can set initial field values by passing them to the constructor. For example like this: Account a = new Account(name = 'Acme', billingcity = 'San Francisco'); I'...
Jan Ruusuvuori's user avatar
8 votes
1 answer
2k views

Pros and cons of generic SObject list vs multiple lists of objects

For one of the requirements I have to write code to update multiple records of different objects. Originally I was planning to do the obvious: get needed records, change some value, update these lists,...
Novarg's user avatar
  • 5,236
8 votes
6 answers
8k views

Can you limit the visibility of fields based on record ownership?

I know with permission sets and sharing rules you can limit visibility and how users can interact with entire records. Is there any way to limit visibility of fields? We are looking to make the basic ...
Ryan Elkins's user avatar
  • 7,637
8 votes
2 answers
5k views

Apex JSON.serialize() with null values (RELOADED)

I need to serialize a list of SObject without omission null values. This string I need for a later usage in JavaScript. Structure is crucial, because I need to construct a table out of it. The ...
Uwe Heim's user avatar
  • 28.3k
8 votes
1 answer
793 views

Access modifiers on sObject properties

Consider the following two classes. The first has simply a private Test__c and its constructor fills it in. The second class uses the first one and creates a Test__c object before instantiating the B ...
zdropic's user avatar
  • 1,178
8 votes
1 answer
5k views

No such column on sobject error occurring on sObject that does have that column

When attempting to update a custom object using SF default API endpoints (/services/data/v43.0/sobjects/MyCustomObject__c/customId__c/4003) I am getting a weird error telling me that a column doesn't ...
BlondeSwan's user avatar
  • 1,078
8 votes
1 answer
1k views

"System.SObjectException: Contact.AccountId does not belong to SObject type Contact" error often in Summer '16

We are seeing a test failure in a couple of our Continuous Integration builds (that deploy the same code) with the error: System.SObjectException: Contact.AccountId does not belong to SObject ...
Keith C's user avatar
  • 135k
7 votes
3 answers
23k views

Is it possible to convert an SObject to a Map?

Is it possible to convert an SOBject to a Map in a very few short lines of codes?
dublintech's user avatar
  • 4,223

1
2 3 4 5
13