Keith C
  • Member for 9 years, 2 months
  • Last seen this week
Iterating over a map (Apex)
Accepted answer
53 votes

These methods are available for all maps. So one way to code it is to use a keySet() method of the Map. Firstly you will get all Id's (keys) from the map and then will iterate over that set of keys. ...

View answer
How do I start to debug my own Visualforce/JavaScript?
Accepted answer
49 votes

Visualforce pages work like this. The browser makes a request and Salesforce uses Visualforce tags to generate HTML/CSS/JavaScript that is sent back to the browser. Most communication is in a simple ...

View answer
Input placeholder in Visualforce page
Accepted answer
46 votes

You can use the recently introduced pass through attributes for this: <apex:inputText value="{!username}" id="username" html-placeholder="Username"/> <apex:inputSecret value="{!password}" id=...

View answer
How to deserialize a JSON list in Apex
Accepted answer
41 votes

Assuming that the JSON data is from a List<Account> in the first place (as it appears to be) you do not need to define your own class but instead can use the Account object type: String s = res....

View answer
What is the purpose of Global and Final modifiers in Batch Apex?
Accepted answer
35 votes

A Database.Batchable class has to be a top-level class (not an inner class) but can now be public. When Database.Batchable was first available, implementors had no choice but to use the global access ...

View answer
How to reference a map's keySet() and individual map elements within Visualforce? Get 'Unknown function' error
Accepted answer
34 votes

The Visualforce for this situation is this: <apex:repeat value="{!myMap}" var="fieldKey"> key: {!fieldKey } value: {!myMap[fieldKey]} </apex:repeat> because the map key is ...

View answer
How to correctly return data and errors from REST annotated methods?
Accepted answer
31 votes

Having written a Salesforce managed package app making use of the automatic serialization and deserialization based on the method signature, in future I will avoid that pattern. The primary reason is ...

View answer
How to deserialize a JSON String to Apex
30 votes

I suggest you paste your JSON into http://json2apex.herokuapp.com/ and try the generated code. This tool generates simple Apex classes with a field per JSON field and then you can parse with a single ...

View answer
Convert DateTime to Date
26 votes

Datetime objects have this date method: date() Returns the Date component of a Datetime in the local time zone of the context user. so your code can use it like this: Datetime dt = (Datetime) ...

View answer
How to truncate a string to a specific length if it is longer?
25 votes

As an indicator to the user that the string has been shortened, also adding '...' to the end can be helpful. There is a String method that does that: String newString = sizeString.abbreviate(120); ...

View answer
How to create mock class for multiple Callouts in single class
Accepted answer
24 votes

Here is perhaps the simplest form of a mock that deals with multiple requests. It is declared as an inner class of the test class so the idea is that it handles all the cases that the outer test ...

View answer
How to run Apex anonymous code from Visual Studio Code
Accepted answer
23 votes

In the command palette, if you type "sfdx ex" you will be offered this if you have some Apex text selected: SFDX: Execute Anonymous Apex with Currently Selected Text or this: SFDX: Execute ...

View answer
Inline list of object initialisation
Accepted answer
23 votes

This cuts down on noise in the code and so is worth doing whether or not there is any performance benefit: List<Special_Codes__c> listOfCodes = new List<Special_Codes__c>{ new ...

View answer
SFDX 6.21.0-9db990b3c9 AND CURRENT Update - Error - sfdx no longer works
Accepted answer
22 votes

This happens to me every couple of days on a Mac. A workaround is to delete the directory /Users/xxxx/.local/share/sfdx/client and then sfdx-cli will automatically upgrade on the next command your ...

View answer
How to initialize List/Set values by construction in apex?
22 votes

The problem is that the Apex list add method returns void a Boolean rather than the list itself. So it isn't possible to append to the list and initialize in one line. You can save one line of code ...

View answer
Feasibility of Angularjs in visualforce pages?
Accepted answer
22 votes

Yes it is feasible. Just think of Salesforce as a (quirky) web server where you have to wrap your Angular index web page in: <apex:page showHeader="false" sidebar="false" standardStylesheets="...

View answer
JSON.deserializeUntyped() not parsing the whole JSON
Accepted answer
21 votes

You can make the code work with an extra step: String s = '{"totalResults": 2, "startIndex":0, "pricing":[{"price":10.80,"cost":9.22,"gp":12},{"price":5.50,"cost":4.00,"gp":24}]}'; Map<String, ...

View answer
Can we use try catch inside a test class? Is that a best practice?
Accepted answer
20 votes

This answer addresses the try/catch part of your question only. If an exception is thrown from the code being tested (or the test code) then the test is a fail and that is normally what you want to ...

View answer
Howto wildcard SELECT * to query all fields of objects in APEX?
19 votes

I suggest that rather than relying on some utility class, you become familiar with the platform's APIs so you can compose the appropriate solution each time you want to query all the fields, ...

View answer
@testSetup vs DataFactory?
Accepted answer
19 votes

A major reason to use @TestSetup is the situation where you have many tests that require the same baseline of data. The @TestSetup method runs once and all its data is available to all the test ...

View answer
Parent Field in Lightning Datatable
Accepted answer
19 votes

Doing the "flattening" as mentioned in Winter 18 <lightning:datatable> does not get values from a parent record can be simple if everything is hard coded: if (state === "SUCCESS") { ...

View answer
Too many if statments?
Accepted answer
18 votes

If you are just looking for a bit of code refactoring of this, creating a list of the fields you want to copy and then looping over the list and making use of methods like SObject.get and SObject.put ...

View answer
How to Get Id of Inserted Record via Apex
18 votes

The platform allocates an ID value and sets it in an SObject's ID field implicitly as part of the insert operation: Account a = new Account(Name = 'Acme'); // a.Id is null here insert a; // a.Id ...

View answer
need to process an excel file with apex
17 votes

A .xls file is in a proprietary binary format that takes a mountain of detailed code to process. For example in Java there is POI-HSSF and POI-XSSF - Java API To Access Microsoft Excel Format Files. ...

View answer
Why Set<Sobject> doesn't support DML operation
17 votes

One reason may be that Set<SObject> is a risky mechanism to use: equality is based on all the fields (so is expensive) and if fields are changed logic can easily break. Same problem using ...

View answer
How to create csv files in controller method from list
17 votes

Rob's link is interesting and worth considering, though it looks like you have to pull the report data by making HTTPS requests which apart from code involves setting up Remote Site Settings. ...

View answer
Retrieve custom fields from standard objects using sfdx force:source:retrieve
Accepted answer
16 votes

This syntax was what I needed sfdx force:source:retrieve -m CustomField:Claim.CveClaim__c to retrieve my custom field CveClaim__c that I added to the (standard in FSC object) Claim. The -m ...

View answer
How to Reduce Repeated If/Else Logic in Code
Accepted answer
16 votes

Where you have repeated code, there are a number of ways of avoiding the repetition that all amount to extracting the common part and still allowing the varying part to vary. See e.g. Avoiding ...

View answer
How to call parent Lightning component's method in child component?
16 votes

You can pass a reference to the parent into the child, so in the parent component: <aura:method name="parentMethod" .../> <c:child parent="{! this }" ... /> and in the child component ...

View answer
Why Are Data Silos Important in Unit Tests?
16 votes

Another point to add to cropredy's excellent list is that tests have significant value as a specification of what the code being tested does. But that only works if you can see the inputs (often the ...

View answer
1
2 3 4 5
106