Using PMD for Static code analysis for apex

PMD is very well known source code analyzer for Java, android and many more languages. It supports now Apex. You might be thinking how can we make PMD as part of our daily life ?

There are many ways

  1. We can run static code analysis standalone
  2. It can be part of ANT build to generate error reports
  3. Eclipse/IntelliJ IDEA IDE can use it as a plugin to show the bugs/violations while you are working on it.

Permanent link to this article: https://salesforcebuddy.com/2019/01/using-pmd-for-static-code-analysis-for-apex/

Record is read-only : Trying to update in after insert trigger

If you have encountered this error while working on Triggers. This is because once you are in an after insert/update trigger and the records are read only in that context as they have been written, but not committed, to the database.

Unfortunately your trigger is relying on the ids of the records, which means you won’t be able to use before insert as the ids won’t be populated at that time (as the records haven’t been written to the database at that point so while you can write to them, database generated fields aren’t populated).

In this instance you’ll need to clone the record (or query anew via SOQL) and make changes to the new copy of the record, then execute the update against the new copies

Permanent link to this article: https://salesforcebuddy.com/2019/01/record-is-read-only-trying-to-update-in-after-insert-trigger/

Salesforce – Spanning Relationships

Each Object in Salesforce is allowed to have upto 15 unique relationships with other objects.

Objects can be related to other objects using a relationship field. When objects are related to each other, user can create formulas that reference fields on the other object. For instance:

  • You have a custom object called MyObject1
  • You have another custom object called MyObject2
  • MyObject1 has a lookup relationship with MyObject2
  • When creating formula fields on MyObject1, you can reference fields found on MyObject2

A formula that references a field on another object is known as a spanning relationship.

When creating a formula on MyObject1, you are allowed to reference another object, like this:

LookupMyObject5__r.SaleAmount + LookupMyObject5__r.FinalDiscount / LookupMyObject5__r.Tax Rate

The limit of spanning relationships per object is 15. This means, that an object, can only contain up to 15 different object references.

The count is based on the number of unique relationships referenced in formulas in the following components of an object:

  • formula fields
  • workflow rules and field update actions
  • approval processes
  • validation rules
  • assignment rules
  • escalation rules
  • auto-response rules

Please keep in mind if I have Program Approver, Fiscal Approver as both Lookup(User) on any custom object, any formula using these 2 objects will consume 2 spanning relationship although it is on same Object (User)

In case you exceed maximum spanning relationship, you will get an error like this.

spanningrelation

You will then have to revisit your formulas and see if it can be optimized.

Ways to Reduce Spanning Relationships

https://help.salesforce.com/articleView?id=000171213&language=en_US&type=1

Permanent link to this article: https://salesforcebuddy.com/2019/01/salesforce-spanning-relationships/