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 ...
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 ...
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 ...
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.
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 } ...
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 = {};
...
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 ...
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
}
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-...
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 ...
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 ...
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 ...
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.
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....
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",
...
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 ...
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 ...
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 (...
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 ...
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
...
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 ...
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 ...
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 ...
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. ...
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 ...
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 ...
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.
...
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 ...
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 ...
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), ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
code × 282apex × 194
trigger × 49
code-coverage × 38
unit-test × 26
visualforce × 25
class × 18
marketing-cloud × 13
after-trigger × 9
javascript × 8
business-logic × 8
lightning-web-components × 7
ampscript × 7
map × 7
lightning × 6
soql × 6
api × 6
custom-object × 5
before-trigger × 5
string × 5
bestpractice × 5
community × 4
metadata × 4
failing-tests × 4
regular-expressions × 4