Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [typecast]

The tag has no usage guidance.

1
vote
2answers
205 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
2answers
523 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 ' ...
9
votes
1answer
213 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('...
2
votes
1answer
195 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
1answer
117 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
1answer
138 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
1answer
624 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
1answer
16 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....
1
vote
1answer
275 views

How can I convert String To Decimal dynamically in VF Page?

I've a map in controller. public Map<ID, String> mapOfChargeTypeToItems{get; set;} The String(Value part) may contain a String(Alphanumeric) mapOfChargeTypeToItems.put(objChargeType.id, ...
1
vote
1answer
281 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 ...
4
votes
2answers
1k 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
1answer
185 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
2answers
850 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 = ...
19
votes
1answer
454 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>...
4
votes
1answer
10k 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
2answers
50 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
0answers
52 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 ...
12
votes
2answers
3k 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
1answer
399 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) { ...
2
votes
1answer
226 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 { ...
1
vote
1answer
2k 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 ...
0
votes
2answers
4k views

Illegal assignment from Object to Decimal

Here is the code which I'm using for deserialize a JSON response. Opportunity opp=[select Id,Name,Account_Id__c,Sync__c from Opportunity where id=:opptyid]; Map<String, Object> m = (Map&...
8
votes
1answer
2k 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
4answers
370 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
2answers
142 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 ...
7
votes
1answer
17k 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
1answer
758 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
0answers
348 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
2answers
2k 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
1answer
4k 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 ...
22
votes
4answers
9k 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
2answers
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
2answers
778 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?