Questions tagged [casting]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
15
votes
3answers
992 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....
11
votes
1answer
1k 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('...
10
votes
2answers
6k 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
28k 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 ...
6
votes
1answer
344 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 ...
5
votes
3answers
11k 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
370 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 ...
4
votes
1answer
218 views

Must I cast a JSON deserialization into an object?

The Apex Developer Guide discusses the deserialize(jsonString, apexType) method of the JSON Class here, with the given example: Decimal n = (Decimal)JSON.deserialize('100.1', Decimal.class); System....
4
votes
2answers
306 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
267 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
2answers
1k 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 ...
3
votes
3answers
4k 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
3k 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
1answer
4k 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 ...
2
votes
3answers
1k 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
51 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
1answer
72 views

Avoiding String.ValueOf on Map<String, Object> from JSON

I'm working on a JSON response that is a REST api call making a Query to another Salesforce Org. Important points: I'm trying to avoid using JSON2APEX if I can. So far I have had good success, and if ...
2
votes
1answer
2k 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
102 views

How to get id from concatenation of string in loop

I am looping trough some records and want to get the related id. This works fine as invoice.BillingAccount__r.BillToId__c. In my case BillToId__c is a field that I get from a custom metadata setting. ...
1
vote
2answers
695 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
33 views

How to pass the parameter to which an object needs to be cast to

I have the following piece of code where I wish to cast a certain object to a specific SObjectType. public static void someMethod(Object obj, Schema.DescribeSObjectResult objectType){ ... ...
1
vote
1answer
2k 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
243 views

Invalid date and time error [duplicate]

I have a @restResource that's receiving a JSON payload as a string. I am taking that payload and placing it into a Map<String, Object> variable. I'm trying to save the date/time into a variable ...
1
vote
1answer
2k 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
52 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
726 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
364 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
52 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
37 views

Syntax for adding child records to List in APEX

Trying to work with a Master-Detail relationship in apex. The following code will produce a Case with several child records, and I can use list methods to access the records (i.e. the system.debug ...
0
votes
1answer
23 views

How to retrieve custom field 3 levels deep from custom metadata

I retrieve a field 3 levels deep which is working currently. The field which I currenly retrieve is going to become a dynamic field retrieved from custom metadata. How do I cast this to an id? I ...
0
votes
1answer
31 views

Dynamic Casting Error

public class DynamicSOQL { public static List<Sobject> Soql (String ObjectName, List<String> apiNames,Set<Object> IdsToQuery ) { String query; ...
0
votes
2answers
4k 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
1answer
437 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
1answer
48 views

How To access a field from a Map Value?

I have an Object (ObjectA), which I have an important field called Motive. For every value in this field 'Motive', I'll have to send this 'ObjectA' to an external endpoint, changing accordingly with ...
0
votes
3answers
190 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
150 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(); ...
0
votes
0answers
72 views

invalid conversion from runtime type Map<ANY, ANY> to Map<String, ANY>

this is what I'm sending to apex: filter: {value: "1"} That is exported via this function in my js controller: @api exportData(){ let data = {}; if(this.isInput || this.isSelect || ...
0
votes
1answer
15 views

How can I concatenate different fields type in a String field

I have to add some fields values in a string. The fields are Id, date, string and integer. The point is I can do this in that way: String s; s= record.Id + record.Name + record.Date__c + record....
0
votes
0answers
44 views

JSON deserialization issues

I have reviewed a lot of the other deserialize issues and I keep running into problems. What I have been able to gather is that if I have a response coming back with a reserved word in the JSON, I can ...