Questions tagged [casting]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
15
votes
3answers
881 views

Is Apex Casting is not transitive?

Seems as though casting is not transitive in some cases. Consider trying to get an Integer from an SObject number field. Integer myInt = (Integer) mySObject.get('My_Field__c'); Throws: System....
10
votes
2answers
2k views

What is the best way to cast/convert Map<String, SObject> to Map<Object, Object> and back again?

To reduce code duplication, at the risk of type safety, I have some method: public Map<Object, Object> doSomething(Map<Object, Object> inputMap) { Map<Object, Object> outputMap; ...
9
votes
1answer
380 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('...
5
votes
3answers
7k views

Dynamic casting of SObject objects?

I have a simple method that I've created as part of an SObjectFactory class. The makeLead() method shown below simply returns a new Lead object. /** * @param valuesByField * Map of ...
5
votes
1answer
13k 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 ...
5
votes
1answer
244 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 ...
5
votes
1answer
66 views

Map values not recognized as an SObject list for DML

One of my fellow developers was working on a class that converted feedback from a REST callout into SObjects. Part of the class was storing the feedback in a map, and then attempted to upsert the map ...
4
votes
2answers
156 views

Cast arrays in Apex

Its been a long time since I casted an array and cant remember if it can be done the way Im hoping. I have an array of type Row (Row[] rows) that is returned from a method. The method I need to pass ...
3
votes
1answer
239 views

Notes & Attachments Wrapper class not sorting by LastModifiedDate correctly

I am using the below code with the Comparable interface to build a combined list of all the attachments and notes on a record, sorted in descending order of LastModifiedDate (in constructor of main ...
3
votes
3answers
1k views

Cannot convert list of objects to specific type

I've created a method to extract a specific field from a list of sObjects, and since it accepts any sObject, I return a list of Object too, since field types can vary. So the method is as follows: ...
3
votes
1answer
2k views

Convert Map to List using SObject field values

I have a Map of Financial__c records, stored like so: Map<Id, Financial__c> idWithParent = new Map<Id, Financial__c>(); The Id part belongs to a different objects record, like a service ...
2
votes
3answers
72 views

casting a map of Id, SObject to a map of Id, customObject

Map<Id, Segment__c> noLockedRecordsMap = (Map<Id, Segment__c>)new LockedRecordHandler().removeLockedRecords(newMap); The above compiles fine. Get this error at runtime: Invalid ...
2
votes
1answer
49 views

How can I abstract building maps from SObject Lists in a good way? [duplicate]

I frequently find I want/need to map a list of SObjects by a value other than it's own Id. So, I'd like to abstract this process to make it reusable. For example, I might want to map Contacts by ...
2
votes
2answers
516 views

Dynamic method to take in a list of sObjects, cast and loop through them and perform field logic?

We currently have 2 different classes of code doing essentially the same thing on both the lead and account objects- grab the country and postal code and assign a territory. We are looking to combine ...
2
votes
1answer
866 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 ...
1
vote
1answer
812 views

Invalid conversion from runtime type String to Double

Background I am getting this error: Invalid conversion from runtime type String to Double On this line of code: line.put(targetField, (Double)fieldValue); The variable fieldValue is of type ...
1
vote
2answers
365 views

How to convert a 15 char Id in Lightning ($SObjectType.CurrentUser.Id)

I'm using $SObjectType.CurrentUser.Id to find out if the current User is the owner of the current record. The issue here is that the method is providing a 15 char user Id. <aura:if isTrue="{!v....
1
vote
1answer
1k views

Illegal assignment from List<SObject> to String Using Database.query

New to apex to please bear with me. I'm using database.query for a SOQL statement from a Custom Setting. I'm then trying to retrieve data from that Custom Setting, but it seems everything is an ...
1
vote
1answer
816 views

Casting Trigger.New to List<Case> in Trigger Framwork

We are using a trigger framework by Kevin O'Hara and I have a basic question about it. Why when I use Trigger.New inside one of the methods I have to cast it to a List of cases in order to use it? ...
1
vote
1answer
45 views

Trying to dynamically access sObject data defined by a SOQL query returning error

So, I've been struggling with this for a few hours now and I think I need a different pair of eyes which I have a feeling will be able to tell me my mistake in a flash! I've queried for some data as ...
1
vote
1answer
584 views

How to sort a custom list of Notes and Attachments by LastModifiedDate

In my VF Page I have been asked to show a list of all the Notes and Attachments in one table, and sorted by the common LastModifiedDate ... So I have a wrapper class in my custom controller: public ...
1
vote
1answer
308 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 ...
0
votes
1answer
51 views

Would it be better to use Serialized/Deserialize or Lists for generalized Set creation?

As a Salesforce developer, I find I frequently need to extract Sets from List<SObject>, so I'm looking to create a generalized way of creating Sets. The problem is, Salesforce thinks Set<...
0
votes
1answer
150 views

Compare the String field against other Datatype fields

I have a custom field: strField of String datatype. I also have Case c, which has fields of type Date, Integer, String, Picklist, currency, etc. For a use case, I need to compare strField with a Case ...
0
votes
2answers
1k views

How do I create a Sobject instance in JavaScript, on a Lightning Component controller, to pass to the Apex controller?

I have a lightning component from which I pass some values to a controller. I want to pass the object as a variable and update a field on this object. How do I make the object variable? See my ...
0
votes
3answers
55 views

unable to convert string to integer datatype

I'm trying to convert String to Integer datatype. But I'm unable to convert. I know its simple, but getting me problem here. It giving me below error FATAL_ERROR|System.TypeException: Invalid ...
0
votes
1answer
47 views

Access value of Email field through Lookup

So I need to access the value of an email field which is set by a workflow. However this needs to be done through a lookup relasionship as seen below. Customobj1__c newRecord = new Customobj1__c(); ...