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
28
votes
1answer
19k 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 ...
27
votes
2answers
29k 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 ...
34
votes
5answers
32k 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.
22
votes
3answers
812 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 ...
39
votes
1answer
64k 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, ...
16
votes
1answer
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. ...
2
votes
1answer
592 views

How to use GetSObject to retrieve related data in a null safe manner?

I have a lookup relationship. If I just do the following query it works fine: Sobject c = [select field__r.fieldchild__c from custom_obj where Id = :someId]; system.debug('c:' + c); However with ...
11
votes
2answers
699 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....
33
votes
1answer
5k 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 ...
8
votes
3answers
2k 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'...
13
votes
4answers
6k 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? ...
7
votes
3answers
4k 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?
12
votes
2answers
6k 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 ...
6
votes
1answer
3k 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 ...
1
vote
1answer
2k views

List has No Rows for Assignment to SObject: Error

I have below trigger code where I get "List has No Rows for Assignment to SObject" Apex trigger: trigger CallVisibility on Compliance_Incident_Transaction_abv__c(before insert, before update) { ...
1
vote
2answers
453 views

Unable to retieve dynamic sObject field value

Trying to retrieve value of field from any given query on sobject. But when i have to traverse across multiple objects to fetch the value, I am not getting expected result. Ex: to get value of owner....
22
votes
2answers
1k 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 ...
11
votes
1answer
14k 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 !
11
votes
4answers
4k 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 = '...
6
votes
1answer
908 views

Unable to read SObject error in Lightning Component when record has been queried from the database

I had encountered the "Unable to read SObject" error when attempting to pass an SObject record from Lightning to an @AuraEnabled Apex method. Based on this question, I first tried setting the "...
5
votes
1answer
279 views

Deserialize History record with read-only fields?

I'm trying to serialize, then deserialize an AccountHistory record. However, whenever I include a read-only field, such as NewValue, in the JSON, when I try to deserialize the record, I get this error:...
3
votes
3answers
14k views

Invalid conversion from runtime type LIST<ANY> to MAP<String,ANY>

I'm trying to make a webcall to pull data from an external webservice. After that call I would like to insert my data that comes in the form of JSON into my custom object called Company. However I am ...
7
votes
2answers
1k views

Why does JSON serialization give different results when the Sobject is created from a SOQL query and when the Sobject is instantiated normally?

I am getting some very odd behavior from the JSON serializer where if the Sobject was originally instantiated from a SOQL query it will exclude any fields that were not part of my query from the ...
15
votes
2answers
2k 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 ...
7
votes
4answers
5k 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 ...
3
votes
1answer
68 views

Quote.class doesn't return the Quote type, but Account.class does. Why?

I'm trying a library that acts like an object factory. It works with serialization, so it is possible to specify formula fields without DML, which is pretty handy with tests. My issue is that I'm ...
2
votes
3answers
5k views

System.QueryException: List has no rows for assignment to SObject

following apex code public String getLastUpdatedAt() { String timeStr; DatadoctorStatus__c last = [SELECT LastUpdated__c FROM DatadoctorStatus__c ORDER BY LastUpdated__c DESC LIMIT 1]; if (last == ...
1
vote
1answer
7k views

Calling Apex class from trigger - issues with passing a list

I am trying to call my Apex class from a trigger: trigger newCrossSellAccounts on Account (before insert) { crossSellModule.insertAfterAccount2(trigger.new); } On the class I have: public void ...
1
vote
1answer
1k views

Visualforce page to list all objects and fields in two drop downs side by side

Looking to build a custom workbench which should list all the objects in the first drop down, depending on the selected object, the second drop down need to show respective fields. Edit Thanks ...
0
votes
1answer
799 views

How to check that SObject field is present in SOQL query

When you try to attempt to the field that not present in SOQL query, you get error like that: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: ...
37
votes
1answer
14k 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 ...
11
votes
1answer
7k 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.
7
votes
2answers
28k 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 ...
5
votes
1answer
5k 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 ...
4
votes
3answers
7k 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?
8
votes
2answers
5k 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 ...
6
votes
2answers
8k views

How to add a lookup field value to an sObject list

Since I started coding in Apex, I've gotten used to working with lists of IDs of objects not the objects themselves. This time around, instead of creating a List<ID> of account ids, I created a ...
3
votes
1answer
2k views

how to get all fields from any object and its parent object in apex?

I have tried this code but i am only getting particular object fields but not its parent fields. any suggestions what i am missing? public with sharing class FilterCriteriaComponentcontroller { ...
9
votes
3answers
3k 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. ...
6
votes
2answers
2k views

How to create nested JSON object from custom sObject?

I am trying to create some config for front end based on config objects. I have one custom object where I specify options that I need. Each option is separate custom object of this type. I want to ...
5
votes
4answers
2k views

Why am I unable to pass an Opportunity from a Lightning Component into an @AuraEnabled Apex controller method?

I have a Lightning Component with an Opportunity attribute. The controller calls a helper method, which obtains the Opportunity from the component and attempts to pass it into Apex to be saved. I am ...
4
votes
1answer
1k views

recalculateFormulas() causing UnexpectedException: Unable to create/update fields: IsDeleted

When I update my custom Record__c a trigger runs, and it calls recalculateFormulas() on the record. This results in the following error: System.UnexpectedException: Unable to create/update fields: ...
4
votes
3answers
16k views

sObject ' ' type is not supported

Hey everyone I've been seeing this a lot lately and am trying to figure out why this error is thrown when querying the SOAP API directly. I've tried several very basic SOQL queries such as SELECT ...
2
votes
2answers
2k views

Can someone please explain what is meant by sObject?

I am very new to the Apex programming first.And I know about Standard & Custom objects. And also the the custom objects have an extension of '__c'. I observed in the programming lot of reference ...
11
votes
2answers
492 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 ...
8
votes
2answers
146 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 ...
7
votes
1answer
3k 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 ...
6
votes
4answers
865 views

Determine sobject Type From Id Using SOAP API

I'm using C# and would like to determine the type of an sObject based on it's id. I would like to pass an id to my app and have it return a list of accounts related to it. I need to identify the ...
6
votes
1answer
3k views

Instantiate an instance of the SObject type

Is there a way to instantiate an SObject without a SOQL query? This works : Type t = Type.forName('Account' ); Account o = (Account) t.newInstance(); But this doesn't : Type t = Type.forName('...
4
votes
1answer
522 views

How can I dynamically transverse SObject fields in Apex?

I am trying to dynamically create a CSV file from Case records. The fields for the CSV file will be determined in custom settings (and therefore shouldn't be hardcoded). Moreover, the fields may ...