Questions tagged [typecast]
The typecast tag has no usage guidance.
38
questions
-1
votes
1
answer
890
views
How to cast object type to a specific sobject in apex?
I have json string when deserialized looks like this -
{a038W0000*********={Checkbox_Response__c=true, RecordId=a038W0000*********}, a038W0000*********={RecordId=a038W0000*********, Response__c=345}}...
0
votes
1
answer
255
views
Typecast to SObject list to SObjectType passed to a method
I have a list of SObject and SObjectType parameters passed to a method
sObjectTypeRecord in the below method contains Lead.SObjectType,it can also contain any other SObjectType like Account....
0
votes
2
answers
6k
views
How to Type Cast Sobject List to custom object List in Apex to access custom object fields
Let's consider the below scenario:
I have a scenario to type cast sObjectList into the List<customObject__c> so that I can access customObject fields and store it into the following:
String ...
3
votes
1
answer
9k
views
Apex cast List<Object> into List<SObject>
My method returns a list of Object. I wanted to add that to a list of specific SObject.
List<Account> accList = new List<Account>();
List<Object> objList = new List<Object>(...
3
votes
1
answer
920
views
Is it possible to cast wrapper instance dynamically?
I wonder if it is possible to cast object parameter passed from LWC Component to specific instance of Wrapper class. In Example we have WrapperB that extends WrapperA and we want to cast data passed ...
2
votes
1
answer
94
views
Why does deserializeUntyped interpret Strings starting with digits as Integers
This code
List<String> ls = new List<String>{'32', '4text', 'text5'};
Object obj;
for (String s : ls){
try{
obj = JSON.deserializeUntyped(s);
System.debug(s + ' ...
2
votes
1
answer
2k
views
Inequality operator not allowed for this type: Object
When trying to compare any two arbitrary Object values to determine order, I get the following error.
Inequality operator not allowed for this type: Object
Below is sample code that will not ...
1
vote
2
answers
2k
views
Is it possible to convert Map<Object, List<Object>> to Map<Id, List<SomeConcreteSObject>> without a loop?
As a Salesforce developer, I am frequently working with Maps and am looking to abstract the process of generating maps from SObject lists.
I've created the following helper class:
private class ...
4
votes
2
answers
11k
views
Iterate over a dynamically populated list of objects
I have a SOQL query where I don't know the FROM object, meaning it can change every time. So I dynamically populate it like so
// dynamicObject is a String.
String queryString = 'SELECT '
...
11
votes
1
answer
2k
views
How is it possible to cast Set to (Iterable<String>)?
I am pretty sure that I am missing something obvious here, but I still cannot grasp the casting operation in the following piece of code:
Set<String> mySet = new Set<String>();
mySet.add('...
3
votes
1
answer
794
views
List<SObject> is instance of SObject?
Object acclist = new List<SObject>{new Account()};
System.debug(acclist instanceof List<SObject>);
System.debug(acclist instanceof SObject);
The code above prints true true
Is List<...
2
votes
1
answer
280
views
Invalid bind expression type of ANY for column of type Decimal
I know this question has been answered in few places my scenario is a bit different I think.
I'm building a dynamic SOQL query, the WHERE clause is going to be dynamic.
I have a List<String> ...
0
votes
1
answer
793
views
Variable does not exist when trying to access 1st element of list
Below i the code :
Map<String,Object> poVals =
(Map<String,Object>)JSON.deserializeUntyped(POResponse);
system.debug('poVals' +poVals);
lstPODetails = (List<object>...
2
votes
1
answer
3k
views
Is it possible to cast an sObject to another object that is dynamic?
I'm having a problem with my code because I need to get a list of records of a dynamic type, and that is working alright, but then I need to use some of the fields and can't reach them, giving me the ...
0
votes
1
answer
48
views
How to typecast Date to String? [closed]
@isTest
public class Listexamples {
@isTest static public void main(){
List<String> name = new List<String>();
name.add('Ranjit');
name.add('Shantanu');
name....
2
votes
1
answer
477
views
Generic method to downcast SObject list and return typed list
Does anyone know if there is an approach to write a generic method that takes an SObject list and specified type as input, and return a downcasted list? Casting is straightforward but as far as I'm ...
5
votes
2
answers
5k
views
Check if object instance is of type X
How can I see if an instance of a class is of a particular type? See example code below:
IMyInterface m = new MyClass();
Type t = MyClass.class;
// how can I know if m is of type t?
Based on other ...
5
votes
1
answer
427
views
Visualforce dynamic rendering based on Object Type
Imagine you have a collection of classes all extending a base type:
public abstract class Vehicle{
public Position position{get; private set;}
public String make {get; private set;}
public ...
1
vote
2
answers
2k
views
What is the correct way to type cast a dynamic SObject?
I'm trying to get the object type conversion as below. But getting compiler error saying Invalid Type t. Any clue to correct it. Thanks. obj_Name is String and Formobject is SObject.
Type t = ...
23
votes
1
answer
2k
views
Is there a specific reason why we can't upcast Sets?
As most stories here begin, I was writing some code for a project that I was working on, and stumbled across an oddity when writing some unit tests. I was testing a method that has a Set<sObject>...
10
votes
1
answer
49k
views
Error: Invalid conversion from runtime type List<ANY> to List<Map<String,ANY>>
This seems similar to other "Can not cast List to List" questions, except in the other cases the culprit tended to be custom sObjects, whereas nothing like that is relevant here.
Why is this cast ...
4
votes
2
answers
64
views
Incorrectly Typed List - No Runtime Exception?
Grouping records is a pretty common pattern, enough so to write a simple library for it. Something like:
public class GroupBy
{
public Map<Id, List<SObject>> ids(SObjectField field, ...
1
vote
0
answers
68
views
Dynamic Object Translator
I am trying to create an object translator which will be used to translate my wrapper class object into an other object.
My class structure is as following
public class wrapperMaster
{
public class ...
19
votes
2
answers
7k
views
What is the difference between (Id)str and Id.valueOf(str)?
Say I have one string String strId = 001xa000003DIlo and I want to typecast it to an Id. So I have two options to do so:
Id idVal = (Id)strId;
Id idVal = Id.valueOf(strId);
Which is the best way to ...
10
votes
1
answer
1k
views
Most Efficient Method to Convert Set Type?
I have a twist on this question. What is the relative performance of each of the following methods?
Via Peter Knolle:
public static Set<String> deserializeToStrings(Set<Id> input)
{
...
4
votes
1
answer
937
views
Assigning an Enum to a Type Instance
I have been trying to make a generic utility and one of the things I'd like to do is pass an Enum definition to a possible Type instance.
It would look something like this:
public enum ExampleEnum
{
...
3
votes
1
answer
6k
views
Compatible Schema.DisplayType For SObject Put
I'm trying to write a method for working with sObject.put(field, value) statements in a loop where the Schema.DisplayType of the fields received varies.
The goal is to have the method return the name ...
9
votes
1
answer
6k
views
Boolean.valueOf(x) vs (Boolean)x
Is there any reason to use one over the other? Efficiency? "gotcha's"?
I am working on a major code cleanup, and my org is littered with both types, so as I'm going through, I'd like to standardize,...
0
votes
4
answers
589
views
Compiler reports invalid assignment on RecordType Query in Apex Class
I have a very simple query in a fairly simple class that is called from a trigger. The compiler is complaining on this singular LOC in the class:
String recordTypeName = [Select Name from ...
2
votes
2
answers
159
views
Developer Console throws Type Exception
I have a base class that's a mash up of the base classes here. What's important is that this class contains the following property and method:
public CommunicationBase Owner { get; set; }
public ...
12
votes
1
answer
47k
views
How to convert Id to String in Apex? [duplicate]
I've been searching the documentation for this, without much luck so far. In particular I'd like to convert an Id to a 18-char string, and ideally read about that in documentation to verify its the ...
0
votes
1
answer
1k
views
Type casting error
I am getting runtime type casting error with below code. Can someone please help?
Code:
global class batchCallUpdate implements Database.Batchable<sObject> {
global Database.QueryLocator ...
1
vote
0
answers
373
views
How do I "cast" concrete types in Visualforce?
Apex StandardSetControllers are powerful. I love the ability to new one up and dump it on a page.
Here's a contrived example, where a Contact has many Engagements with ratings etc:
public with ...
1
vote
2
answers
3k
views
Populating lookup fields using an external ID with Object type as String
My question is pretty much the same as here. But the difference is I'll have to do it generically. As in the object type is sent as text and I'll have to typecast in the code. Eg.,
//create an in-...
3
votes
1
answer
7k
views
Type casting an object from one custom class to another?
I'm working on integrating Salesforce with an external system and am having some trouble with my approach. One of the REST endpoints I've made in Salesforce is expecting a 'person' object that could ...
28
votes
4
answers
23k
views
Convert a Set<Id> to Set<String> using Apex
I have a Set<Id> which I want to convert to a Set<String>. I don't want to use a for loop to iterate over it. What is the simplest way to do this?
2
votes
2
answers
1k
views
Dynamic query on KnowledgeArticleVersion
I see a strange issue that dynamic query does not automatically typecast for knowledgearticleversion object.
Below query works fine:
List<Account> lAccount = database.query('Select id from ...
4
votes
2
answers
1k
views
How to typecast values based on fieldtypes?
How to typecast values based on field types?
I have a map of field Types and field Values(in Strings)? how to convert each field Values based on the field Types dynamically?