Navigation Menu
Search code, repositories, users, issues, pull requests..., provide feedback.
We read every piece of feedback, and take your input very seriously.
Saved searches
Use saved searches to filter your results more quickly.
To see all available qualifiers, see our documentation .
- Notifications
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Illegal assignment from List<AggregateResult> to List<AggregateResult> #2763
eq-dsolari commented Nov 25, 2020
lcampos commented Nov 30, 2020
Sorry, something went wrong.
no-response bot commented Dec 7, 2020
No branches or pull requests
Type Casting in Apex
Hello devs,
What are Upcasting and Downcasting? How can you cast Apex Map, List, and Set? What is a supertype, and what is the difference between SObject and Object? Answers to these questions and much more can be found in this post!
Let's begin.
Before We Start
I am using different emojis to grab your attention:
- 🚨 - Unexpected casting behavior.
- ✅ - Code that works.
- ❌ - Code that does not work.
What supertype is?
A supertype is an abstract/virtual class or an interface that is used by a child class.
The Parent interface is a supertype for the Child class.
A class can implements many interfaces, meaning that a class can have many supertypes .
The Parent abstract class is a supertype/superclass for the Child class.
A class can extends only one abstract class, but it can still implements many interfaces.
The Parent virtual class is a supertype/superclass for the Child class.
A class can extends only one virtual class, but it can still implements many interfaces.
Upcasting vs Downcasting
You know what supertype is, it's necessary to understand the difference between Upcasting and Downcasting .
To better understand upcasting and downcasting, we need some examples.
We have two classes (it can be also class and interface), Parent and Child .
- Parent is a virtual class and has a virtual method.
- Child extends the Parent class and override the virtual method.
If you don't understand what virtual means, you can refer to the Abstract, Virtual, Interface in Apex post.
- Child extends the Parent class. It means that Parent is a supertype/superclass for Child . Because of this, the following syntax: Parent parent = new Child(); is valid.
- Casting a subclass ( Child ) to a supertype/superclass ( Parent ) is called Upcasting .
- Upcasting can be either explicit or implicit.
Implicit Upcasting
Upcasting can be done implicitly.
Explicit Upcasting
Code should be as simple as possible (KISS rule), so the better approach is to use implicit upcasting.
The parent variable has access to:
- Parent public variables.
- Parent public methods.
- Child ONLY overriden methods.
Downcasting
- Casting a supertype/superclass ( Parent ) to a subclass ( Child ) is called Downcasting .
- Upcasting can be done ONLY explicitly.
Implicit Downcasting
Downcasting CANNOT be implicit. Why?
- There can be many children of the Parent class. e.g public class Child2 extends Parent { ... } .
- Apex needs to know to what type the variable should be cast to.
Explicit Downcasting
You have to cast explicitly (Child) so the compiler checks in the background if this type of casting is possible or not. If not, you will get System.TypeException: Invalid conversion from runtime type Parent to Child .
The child variable has access to:
- Child ALL public methods and variables.
- SObject Class
SObject is a supertype for:
- all standard objects (Account, Contact, etc.)
- all custom objects (MyCustomObject__c)
Object is a supertype for:
- all standard objects
- all custom objects
- all apex classes
- all collections (List, Set and Map)
- all apex types (Integer, String, Boolean)
- Methods inherited from Object class
SObject and Object
Sobject and object - instanceof.
- SObject is an instance of Object . Object is a supertype for SObject .
SObject and Object - casting
Object to sobject.
- Object needs to be explicitly cast to SObject .
- Object is a supertype of SObject .
- Conversion from Object to SObject is called Downcasting .
- As you already know from Downcasting section. Downcasting needs to be explicit (SObject) .
SObject to Object
- SObject can be explicitly or implicitly cast to Object .
- Conversion from SObject to Object is called Upcasting .
- As you already know from Upcasting section. Upcasting can be done explicitly or implicitly.
Inherited methods
- Interestingly, Object is a supertype for SObject , but SObject does NOT inherit Object methods like toString(), equals(), hashCode(), clone() . SObject has all of the following methods SObject class methods. .
However, all Apex Classes inherit Object methods ( toString() , equals() , hashCode() , clone() ).
Standard/Custom Object
Standard/custom object - instanceof.
- Standard/Custom Object is an instance of SObject and Object .
- SObject and Object are supertypes for Standard/Custom Objects .
Standard/Custom Object to SObject - casting
Standard/custom object to sobject.
Why it works?
- SObject is a supertype for Account , Contact , and other standard or custom objects.
- Conversion from Standard/Custom Object to SObject is called Upcasting .
SObject to Standard/Custom Object
- Conversion SObject to Standard/Custom Object is called Downcasting .
- As you already know from Downcasting section. Downcasting can be done ONLY explicitly.
Standard/Custom Object to Object - casting
Standard/custom object to object.
- Object is a supertype for Account , Contact , and other standard or custom objects.
- Conversion from Standard/Custom Object to Object is called Upcasting .
Object to Standard/Custom Object
- Conversion Object to Standard/Custom Object is called Downcasting .
Primitive Types
Primitive types - instanceof.
- All Primitive Data Types are instanceOf Object class.
- Object is a supertype for all primitve types.
Primitive Types - casting
- Primitive Types can be implicilty cast to Object .
- Object is a supertype of all Primitive Types.
- Conversion from Primitive Type to Object is called Upcasting .
List<SObject>
List<sobject> - instanceof.
- List<SObject> is an instance of concrete Standard/Custom Object!
- List<SObject> is NOT an instance of List<Object> .
List<SObject> - casting
List<sobject> to list<object>.
- List<SObject> is NOT an instance of List<Object> (based on instanceOf ), but you can still assign List<SObject> to List<Object> . Do not trust instanceOf !
List<Standard/Custom> to List<SObject>
- List<SObject> is a supertype for List<Standard/Custom> .
- Conversion from Standard/Custom Object to List<SObject> is called Upcasting .
List<SObject> to List<Standard/Custom>
Upcasting/Downcasting (?)
- List<Standard/CustomObject> is an instance of List<SObject> , and List<SObject> is an instance of List<Standard/CustomObject> .
- List<Standard/CustomObject> is a supertype for List<SObject> , and List<SObject> is a supertype for List<Standard/CustomObject> .
- You can do implicit casting.
List<Object>
List<object> - instanceof.
- SObject is always an instance of Object , hovever List<SObject> is NOT an instance of List<Object> .
List<Object> - casting
- We can cast List<ConcreteType> to List<Object> ,
- List<Object> is a supertype of List<ConcreteType> .
- Conversion from List<ConcreteType> to List<Object> is called Upcasting .
- There is no need to cast explicitly by adding (List<Object>) .
- List<SObject> is NOT an instance of List<Object> (based on instanceOf ), but you can still assign List<SObject> to List<Object> .
List Summary
List casting in Apex is a bit unusual.
What is common:
- List<ConcreteType> is an instance of List<Object> .
- List<Object> is a supertype for List<ConcreteType> .
What is unexpected:
- Based on instanceOf . List<SObject> is not an instance of List<Object> , but you can still assign List<SObject> to List<Object> .
- You can even assign List<SObject> to List<Object> . Do not trust instanceOf !
- Even more unexpectedly, List<SObject> is an instance of concrete List<Standard/Custom> Object!
Set<SObject>
Set<sobject> - instanceof.
- Set<Standard/CustomObject> is NOT an instance of Set<SObject> .
Set<SObject> - casting
Set<sobject> to set<object>.
- You CANNOT cast Set<SObject> to Set<Object>
Set<Standard/Custom> to Set<SObject>
- You CANNOT cast Set<Standard/Custom> to Set<SObject>
Set<SObject> to Set<Standard/Custom>
- You CANNOT cast Set<SObject> to Set<Standard/Custom>
Set<Object>
Set<object> - instanceof.
- Other than List , Set<Object> is NOT a supertype for Set<ConcreteType> .
Set<Object> - casting
- We CANNOT cast Set<ConcreteType> to Set<Object> ,
Even if you explicitly add casting (Set<Object>) it will not work.
Set Summary
Map<sobject>, map<sobject> - instanceof.
I skipped cases where SObject is a key. SObject should never be a key in the Map.
- Map<Object, Account> is an instance of Map<Object, Object> , which means that Map<Object, Object> is a supertype of Map<Object, Account> .
- Map<Object, Account> is an instance of Map<Object, SObject> , which means that Map<Object, SObject> is a supertype of Map<Object, Account> .
Map<SObject> - casting
- Map<Object, Object> is a supertype for Map<Object, Standard/CustomObject> .
- Conversion from Map<Object, Standard/CustomObject> to Map<Object, Object> is called Upcasting .
Even explicit casting will not work. The error is different.
How to fix Map downasting?
Map<Object>
Map<object> - instanceof.
Maps are the same instance only in the following cases:
- new Map<MyType, MyType>() instanceOf new Map<MyType, Object>
- new Map<Object, MyType>() instanceOf new Map<Object, Object>
Key Type must be the same.
Map<Object> - casting
You can cast implicitly only when:
Map Summary
Starting from Summer 23', Set implements Iterable interface as List does.
Iterable List
Iterable list - instanceof.
Not surprisingly, a List of concrete types is also an instance of Iterable<Object> .
- Instance of List<Object> and Set<Object> are always an instance of System.Iterable<Object> .
Iterable List - casting
It's really interesting.
e.g List<String> is an instance of Iterable<Object> , but you CANNOT use Iterable<Object> as a supertype .
You need to assign List<String> to List<Object> and after it to Iterable<Object> .
Iterable Set
Iterable set - instanceof.
- An interesting thing is that you're getting an error Operation instanceOf is always true... .
- Set<ConcreteType> is also an instance of Iterable<Object> , but not Set<SObject> .
Iterable Set - casting
- You CAN use Iterable<Object> as a supertype for Set , which you CANNOT do with a List .
- You CANNOT assign Set<SObject> to Iterable<Object> .
It's quite a big deal. Let's check an example:
How to fix it?
- You need two methods ( isIn(Iterable<Object> inList) and isIn(List<Object> inList) ) in the interface that will be covered by one method ( isIn(Iterable<Object> inList) in concrete class!
Iterable Summary
- You CANNOT use Iterable<Object> as a supertype for List<ConcreteType> . You need to assign List<ConcreteType> to List<Object> and after it to Iterable<Object> .
- Set<ConcreteType> is instance of Iterable<Object> .
- Set<Sobject> is NOT instance of Set<SObject> .
Casting Rules
Cast to everything.
The problem with the solution below is performance.
Casting Cheat Sheet
Do not trust instanceof.
Based on the type system is broken with regards to collections :
Pretty much the entire "Type" system that governs Maps, Sets, and Lists is broken. Do not trust instanceOf, use your own logical assessment to determine if something is safe or not.
As shown in the post, there are cases where instanceOf does not work correctly.
List<SObject> and List<Object>
instanceOf says that " List<SObject> is never an instance of List<Object> ", but you can assign List<SObject> to List<Object> .
List<ConcreteType> and Iterable<Object>
instanceOf says that List<ConcreteType> is an instance of Iterable<Object> , but you CANOT assign List<ConcreteType> to Iterable<Object> .
SObject Casting
- Casting from Standard/Custom Object to SObject is called upcasting and can be implicitly.
- Casting from SObject to Standard/Custom Object is called downcasting and it needs to be explicit.
- SObject is instance of Object . Object is a supertype for SObject .
- Casting from SObject to Object is called upcasting and can be implicit.
- Casting from Object to SObject is called downcasting and it needs to be explicit.
Object Casting
Primitive types casting.
- All Primitive Data Types are instanceOf Object . Object is a supertype for all primitve types.
- Casting from Primitive Data Types to Object is called upcasting and can be implicit.
List Casting
- All List<ConcreteType> are instance of List<Object> . List<Object> is a supertype of List<ConcreteType> .
- Based on instanceOf method List<SObject> is NOT an instance of List<Object> , but still you can assign List<SObject> to List<Object> . Do not trust instanceOf .
- Casting from List<ConcreteType> to List<Object> is called upcasting and can be implicit.
- List<Standard/Custom> is a supertype for List<SObject> .
Set Casting
- You CANNOT cast Set<ConcreteType> to Set<Object> .
Map Casting
- Map<Object, Account> is instance of Map<Object, Object> , it means that Map<Object, Object> is a supertype for Map<Object, Account> .
- Map<Object, Account> is instance of Map<Object, SObject> , it means that Map<Object, SObject> is a supertype for Map<Object, Account> .
- To fix System.TypeException: Invalid conversion from runtime type Map<ANY,SObject> to Map<ANY,Account> dynamic Map initiation is needed.
Iterable Casting
- Set<ConcreteType> is instance of Iterable<Object> , but Set<SObject> is NOT instance of Iterable<SObject> .
- Unexpected Iterable behavior in Apex
- Classes and Casting
- Using the instanceOf Keyword
- Upcasting Vs Downcasting in Java
You might also like
Advanced Salesforce LWC debugging with Chrome Developer Tools
LWC debugging and development with Chrome Developer Tools – learn how Salesforce LWC developer can debug and develop Lightning Web Components effectively without console.logs.
Beyond If Statements: Ways to avoid IFs – Polish Dreamin’ 24
Code is a representation of business requirements. Business requirements vary for each company, but most of them have an IF-THEN structure. It’s quite common to see IF statements in our code. However, an increasing number of IFs can make our code hard to read and understand.
Illegal assignment from String to Decimal
- 役に立った順で並び替え
Spot on for Amit
String temp = '12.4567';
Decimal myDecimal = Decimal.valueOf(temp);
System.assertEquals(12.4567,myDecimal);
IMAGES
VIDEO
COMMENTS
A query returns a list no matter what, but can be cast to a single sObject when it results in a single record. But you can't reference the field directly, as you're doing. With the query in it, line 4 could be:
I have a requirement to fetch Contact Region from Contact and update the same on User. Here is my logic: list<user> user = [SELECT name,Region__c from User where isactive = true]; Set<st...
//use the describe class to get Opportunity record type info Map<String, Schema.RecordTypeInfo> RT = Opportunity.SObjectType.getDescribe().getRecordTypeInfosByName(); //List of record types to look for, note the label is used not the api name List<String> recordTnames = new List<String>{'record type label','another record type label', 'etc ...
Salesforce: Illegal assignment from List to StringHelpful? Please support me on Patreon: https://www.patreon.com/roelvandepaarWith thanks & praise to God, a...
Salesforce: Illegal assignment from list to list (OpportunityContactRole to String)Helpful? Please support me on Patreon: https://www.patreon.com/roelvandep...
OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community. Learn more
The Apex Language server shows an AggregateResult type returned by a SOQL statement and then assigned to a List as an illegal assignment. Steps To Reproduce: Create a new project, link to your org; Pull Apex class from the org or create new one; Author any SOQL statement with return type AggregateResult and assign it to a List. Expected result
Salesforce: Illegal assignment from List<SObject> to String Using Database.queryHelpful? Please support me on Patreon: https://www.patreon.com/roelvandepaar...
If you want, for instance, a list of names you could assemble them by iterating through the list produced by the SOQL query and appending names to the string. Alternately, if what you really want is just a list of events, you can change the type of your Sessions variable and the return type of 'getSessions' to Event__c[] or, equivalently, List ...
Illegal assignment from List<OpportunityContactRole> to List<String> When I try to query from OpportunityContactRole and store it into a new list. I'm pretty new to apex, so I'm kind of confused where I'm going wrong. Here's my code:
All Primitive Data Types are instanceOf Object class.; Object is a supertype for all primitve types.; Primitive Types - casting. Upcasting. Primitive Types can be implicilty cast to Object.; Why? Object is a supertype of all Primitive Types.; Conversion from Primitive Type to Object is called Upcasting.; As you already know from Upcasting section. Upcasting can be done explicitly or implicitly.
4 件の回答. You can call methods in apex classes from your trigger. I just did some refactoring on the code you provided. Please let me know if this helps you. Public class Job_Description_Transfer {. public string cleanseOpportunityDescription (string oppDescription) String RegEx =' (</ {0,1} [^>]+>)'; return oppDescription.replaceAll ...
1. Here you are setting string to the return_x. // start - specify the response you want to send. ParkService.byCountryResponse response_x =. new ParkService.byCountryResponse(); response_x.return_x = 'USA'; // end. But this variable is declared as List of strings. public class byCountryResponse {.
Illegal assignment from String to Decimal. Hello, I need to convert string to decimal Code //upsert the existing case to add the fields Case newC = new Case (); newC.Amount_of_Bill__c = AmountofBill1; I thought I was converting to String... //fetch all the fields from the first block Map<String,Object> BCB1 = (Map<String,Object ...
Address Inheritance Does Not Copy Addresses with Master Record Type in CRM; All Action Items Within an Account Plan Are Not Displaying on the Account Plan Dashboard In CRM; ... Illegal Assignment from String to Boolean. Root Cause: The org is not on the most recent Veeva Version. The Apex Classes and Triggers used for account creation are outdated.
If Exp is a list of integers this will fail as CloseDate is a date. If it were a list of opportunities it would still fail. If it were a list of opportunities it would still fail. The line update Exp; should not be occupying within the for loop.
When I try to parse JSON.CreateParses I get these error: Illegal assignment from System.JSONParser to JsonParser. I try to make API that show weather in London. public with sharing class JsonToApex {@future(callout=true) public static void parseJSONResponse() { String resp; Http httpProtocol = new Http(); // Create HTTP request to send.
In the scenario below, SearchFields__c is a single String (e.g. Name), but it won't let me assign it to a String. Any help would be appreciated. Thanks in advance.
The correct syntax here should be as: Id voterId = [select ID from Vote__c where suggestion__c =: recordId LIMIT 1].ID; If you want to get the List of ids, then you should have something as below (there are other approaches too) and then get the Id of the records: List<Vote__c> voterIds = [select ID from Vote__c where suggestion__c =: recordId];