Questions tagged [generics]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
0answers
10 views

Salesforce Keyboard shortcuts

Can we use any keyboard shortcuts in Salesforce Apps? Not Console apps. I only found from: https://help.salesforce.com/articleView?id=accessibility_keyboard_shortcuts_lex.htm&type=5 e -> edit / -> ...
0
votes
0answers
58 views

How to accept generic “SObject” in Lightning Web Component for Flows?

I've got a custom built LWC to be used within a screen component in a flow. My component basically renders a collection of SObjects in a distinct way. It can also deal with different types of sObjects....
1
vote
2answers
79 views

Generic function - Custom meta data type

I would like to do a generic function to get custom meta data type records and return it as a Map. In a first time with just one field , that means Map<String,String> and in a second time with ...
0
votes
1answer
40 views

How to get recordtypeid from generic object

I want to be able to pass an object, and recordtypelabel as string into a controller and get the recordtypeId. How do I achieve this. I currently get variable does not exist for the custom object. @...
0
votes
2answers
51 views

Is it possible to create a generic Map swapper?

I have a method: public Map<Object, Object> swap(Map<Object, Object> valueByKeyMap) { Map<Object, Object> keyByValueMap = new Map<Object, Object>(); for (Object key : ...
1
vote
2answers
273 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 ...
-2
votes
1answer
40 views

How to create a Map with generics Id and String?

How to create a Map<Id, String>([SELECT Id, Name from Account]);? The query returns Map<Id, Account>. I want to get a Map<Id, String> without using loops and without having to ...
1
vote
1answer
76 views

Dealing with Generics in Apex

I have some code that i want to try and emulate generics for. I know there are no generics in Apex but is there something we can do instead? Let me explain (String) sfBorr.get('SSN__pc'); sfBorr is ...
8
votes
1answer
226 views

If I know the API Name, how can I get and work with the Schema.ChildRelationship?

Like many SFDC developers, we have SObjects which are related to other custom relationships. So, for example, MyCustomObject__c could have a relationship with MyCustomRelationship__c which in Apex we ...
5
votes
2answers
254 views

In Apex, can I detect if an Object is a Map, regardless of parameterized types?

When I declare a Map, I need to give the types, e.g.: Map<Id, SObject> sObjectByIdMap = new Map<Id, SObject>(); We can assign map to generic values as Objects: Object value = ...
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; ...
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 ...
4
votes
2answers
224 views

How can I upsert a homogeneous List<SObject>, in a good way? [duplicate]

I have some abstracted code, which at the end of some process will create a homogeneous list of SObjects. It could be a list of Accounts, it could be a list of contacts, it could be a list of some ...
1
vote
1answer
311 views

Generic SObject Utility to dynamically set field values from Map

I'm trying to create a generic utility method which takes a List of sObjects and a Map of fieldNames to fieldValues. I'd like to, for example, do the following: List<Lead> leadList = [SELECT Id ...
6
votes
3answers
839 views

Does apex support generics?

I don't know if Apex supports generics but how can I make the below class more generics where I should be able to pass any data type? below is the example: the problem I have is this: public class ...
4
votes
1answer
648 views

Pros and cons of generic SObject list vs multiple lists of objects

For one of the requirements I have to write code to update multiple records of different objects. Originally I was planning to do the obvious: get needed records, change some value, update these lists,...
11
votes
2answers
5k views

How to write a generic list<t> method that will cast at runtime?

I recently tripped across the need to take a list of objects (instances of a class--not sobjects) and split them into smaller lists of 200 members each. The unit test below shows the code inside a ...
5
votes
3answers
2k views

Getting a type of the generic object

How to get a type of the generic object? I have a method that gets as parameter some object. It can be realy everything. In my example it is a list of strings. But it can be a map or set or some other ...
5
votes
1answer
3k views

Is it possible to create a method that uses generics? [duplicate]

... or at least pass in a specific class? Here is what I have right now: private static invoice_response GetResponse(String jsonResponse) { invoice_response deserialized = (invoice_response) ...
3
votes
1answer
1k views

Is it possible to write a method that accepts both SObjects and Apex Class Instances?

I have an Apex method that takes a list of SObjects, a source field name, and a destination fieldname. For each SObject in the list, it looks at the source field, does some work, and populates the ...
33
votes
1answer
6k views

Generic Apex Trigger

So I was kind of throwing round the idea of making a generic apex trigger that is callable by multiple objects. Say you have the following sObjects: Account Sales__c Sales_Contacts__c And I wanted ...
13
votes
1answer
4k views

How do I use Type.forName to deserialize a generic list of SObjects?

When the new Apex type methods were added in Summer '12, it was possible to do Type t = Type.forName('MyClass'); MyClass newObj = (MyClass)t.newInstance(); What is the equivalent when I want to ...
5
votes
1answer
71 views

How to get the old Test Class UI back? Or possible workarounds with the new UI?

Is there anyone else who is sorely missing the old test class UI where it shows the entire list of Apex classes covered by running one test class? Another good feature of the old UI was that you could ...
5
votes
2answers
667 views

What is “Skip to main content”?

In any Salesforce page, if no element is in focus and you press Tab⇄, a hidden element Skip to main content is put into focus. What does Skip to main content do?
10
votes
1answer
8k views

What is the correct JSON to deserialize a map of sObjects?

I'm trying to store a map of sObjects in a static resource. I want to be able to store default values for test setup methods. I was able to get it working with the following JSON in the static ...
9
votes
4answers
2k views

How do you deserialize json into the correct virtual class?

Let's say I have a virtual class and two classes that extend it and I am getting a JSON response that could be either one of those classes. Is there a way to deserialize the JSON into the appropriate ...
26
votes
2answers
11k views

Can you use generics in Apex?

While researching the possibility of using generics in apex I can across a blog with this: "Generics are partially supported in Apex, you must be using them with Collections and Batch Apex." ...