Hot answers tagged

36 votes
Accepted

Can we access class properties dynamically with Object class?

Unfortunately, that feature is not supported via default Apex. But there is something, which is possible to do in this case -- create custom get method, and extend your classes from your base custom ...
kurunve's user avatar
  • 4,279
16 votes

Can we access class properties dynamically with Object class?

Using an internal map for this purpose has the advantage that get/set access performance doesn't degrade as more fields are added: public class XYZ { private Map<String, Object> m = new ...
Keith C's user avatar
  • 135k
16 votes

LWC get url parameters in Community

Another approach with using URL.searchParams: The searchParams readonly property of the URL interface returns a URLSearchParams object allowing access to the GET decoded query arguments contained ...
Oleh Berehovskyi's user avatar
15 votes
Accepted

Is there an Apex Linter out there?

There's PMD Apex, which has a bunch of linting, plus various rules for SOQL inside loops, etc. That's the only one I'm aware of today.
sfdcfox's user avatar
  • 481k
13 votes

LWC get url parameters in Community

The best way to get params from URL is use CurrentPageRefference. currentPageReference.state allows you to get params. import { LightningElement, wire } from 'lwc'; import { CurrentPageReference } ...
Piotr Gajek's user avatar
  • 2,555
12 votes
Accepted

LWC get url parameters in Community

If the question was if the LWC Framework provide an utility - then no not that i am aware of. Which means you can use every js snippet you will find somewhere in the web like parameters = {}; ...
Renji-xD's user avatar
  • 2,791
8 votes

How do I cover 'addError' in test class

A more complete example based on d_k answer, but handles the exception not being thrown, as pointed out by Adrian Larson. @isTest public static void testMethod() { // arrange Boolean ...
Robs's user avatar
  • 9,187
7 votes
Accepted

How to cover 'addError' in a test class?

You need to run as a user who is not a System Administrator. You already set one up, now you just need to call system.runAs. system.runAs(u) { // delete here and it should fail }
Adrian Larson's user avatar
  • 149k
7 votes

Method does not exist or incorrect signature: void format(String) from the type Date

Date does not have a format() method accepting a string. That method is available only on DateTime. You can create a DateTime from a Date and simply use a format string that does not include time-...
David Reed's user avatar
  • 92.1k
7 votes
Accepted

Explanation of an notation List<name.test> nameList

This is probably because the clas Name contains the definition of another inner class like this: public class name{ public class test{ } } If so, you have to specify the parent class name ...
Martin Lezer's user avatar
  • 7,296
6 votes

Indenting multiple apex code and pages

Illuminated Cloud's integrated formatter can reformat multiple Apex classes/triggers, Visualforce pages/components, and XML metadata files with a single keystroke and is highly configurable (>100 ...
Scott Wells's user avatar
6 votes
Accepted

Can't Get Unit Test Coverage Above 7/16 (43%)

Your test class can't see existing records unless you annotate with @isTest(SeeAllData=true) See salesforce doc. Even though the CollaborationGroup exists in your org, your test class can't access it ...
Jason's user avatar
  • 236
6 votes

How can I hide my code in visualforce page?

You'll want to refer to the ISVForce guide, but in summation, if you're using a managed package, your Apex Code will be hidden from subscribers, but your Visualforce and JavaScript cannot be hidden.
sfdcfox's user avatar
  • 481k
6 votes
Accepted

How do I cover 'addError' in test class

Do this in catch block : Boolean expectedExceptionThrown = ex.getMessage().contains('Script-thrown exception') ? true : false; System.assertEquals(expectedExceptionThrown, false); OR System....
d_k's user avatar
  • 1,291
5 votes
Accepted

Nested IF Statements

You can use CASE function and try something like this CASE ( Order_Status__c , 1, "Pseudo Machines – not sold", 2, "Sale Confirmed – waiting for spec", 3, "Spec complete – BOM creation", ...
Vamsi Krishna Gosu's user avatar
5 votes
Accepted

Is this Apex coding similar to Java scripting?

Apex is more similar to Java then javascript. Take a look at the docs here, What is Apex: Apex is a strongly typed, object-oriented programming language that allows developers to execute flow ...
Jenny B's user avatar
  • 10.5k
5 votes

Tools for code Evaluation

You can try out CheckMarx tool for source code analysis. I have used that and it is very useful. For more information refer checkmarx It verifies: Cross-Site Scripting (XSS) Cross-Site Request ...
Santanu Boral's user avatar
5 votes

Test Classes are included in code coverage

I ran in to this issue. What could have happened is, test class was created without @isTest annotation (at this point the class has been added to the code coverage table) and @isTest was added later (...
Avinash's user avatar
  • 2,081
5 votes

Not able to cover catch block in test class

The simple answer here is that you need to cause an exception to be thrown. The golden rule of unit testing is you only gain coverage for code that is executed as part of a test method. No exception ...
Derek F's user avatar
  • 59.3k
5 votes

Accessing Object properties

It is (or, at least, used to be) faster to use a variable if you're going to use the field lots of times. See this video for lots of experimentation: https://www.youtube.com/watch?v=w6QnalRWlEE ...
Aidan's user avatar
  • 13.5k
5 votes
Accepted

Avoiding multiple If statements

Let's assume that all tests are equalities You could create a simple two-level map. Note that map keys can be sobjects. The map key will be an sobject possessing only the fields+vals for a given ...
cropredy's user avatar
  • 69.9k
5 votes

How to Document and correct indentation on apex

While I know some folks are happily and successfully using uncrustify, the formatting route that's currently recommended with the Visual Studio Code Extension Pack for Salesforce is Prettier. The ...
David Reed's user avatar
  • 92.1k
5 votes
Accepted

How does js files behave when same lwc is repeated in a component?

What sort of behavior is this? Is this expected? These are a type of "static" variables, formally called "module-scoped variables". A static variable is one that is defined, and ...
sfdcfox's user avatar
  • 481k
4 votes

How can I hide my code in visualforce page?

The way to create this is to create a managed Salesforce package. That way your customers can use your controller and VF page, but they will not be able to access the source code of the Apex class. ...
mkorman's user avatar
  • 4,026
4 votes
Accepted

How to check the field value is same for all the records in the list?

Just cache the first one and then compare equality. It's worth noting that this approach as written below performs a case-insensitive comparison. If you want case-sensitivity, you're going to need to ...
Adrian Larson's user avatar
  • 149k
4 votes
Accepted

Ghost Text in Apex Class Editor

I have run into this issue before and fixed it by zooming in or zooming out to adjust the display. You can see that this issue has been around since at least 2013: In-Page Editor Line Distortion. For ...
Adrian Larson's user avatar
  • 149k
4 votes

Offline tool that can show dependencies between different metadata files

There are a number of tools outlined in Apex static code analysis, although most of them are online. In particular the post by Robert Sösemann on the static code analyzer that integrates with Eclipse. ...
Daniel Ballinger's user avatar
4 votes

Code Coverage=0%

This outcome is explained by considering the relationships between ContentDocument and ContentDocumentLink. It's documented Salesforce behavior that cascade-deletes do not invoke apex triggers on any ...
Morgan Marchese's user avatar
4 votes

Code Coverage Failure trigger deploy on production

To deploy a trigger to production , you will need to ensure your trigger has a test class and the class needs to cover at least 75% of the lines of code . Looks like you are writing the trigger for ...
Mohith Shrivastava's user avatar
4 votes

How to Document and correct indentation on apex

Uncrustify It only works if you enable an override (by default, it works on other languages). I found this blog post that explains more. You need to create a configuration file (uncrustify.create), ...
sfdcfox's user avatar
  • 481k

Only top scored, non community-wiki answers of a minimum length are eligible