Skip to content
Groovy 37 – Few Recommendations when scripting

Groovy Script Tips

1 - Avoid using Webservices inside Sales Cloud in your Groovy.

Try to always find a way to use only Groovy and avoid as much as possible usage of Webservices. Pure Groovy scripts generally execute faster.

2 - Avoid using non-indexed attributes in your queries.

It's best to use a primary key or a foreign key when running a query using the newView() function. Usually, these attributes are Ids like OptyId, CustomerID, and for custom DCL fields, it's DCLname_Id_c.

3 - If you hit an error like "Post threshold limit reached," try this:
       
        if (API_NAME_FIELD != value) {
            setAttribute('API_NAME_FIELD', value)
        }
       
        
4 - Avoid using the newView() API if you already have a Related Accessor.

If you have a Related Object or Related Collection for the object you want to access, use those instead of newView().

5 - Be careful when using formula fields.

Formula fields can execute multiple times during the page/form execution. If you have some query written in a formula field, that query can execute several times while you open a page or navigate through the subtabs.

6 - Don't use too many triggers.

Try to create the logic of your customization with the minimum necessary number of triggers. Triggers push data to the database, so having too many may cause performance issues.

7 - Use the right tool for the right task.

For example, if you want to make a validation on a single field, make it at the level of the field, not at the level of the object. This is much more efficient.

8 - Try not to create chain reactions.

Avoid situations where a trigger fires and triggers the execution of a workflow, and that workflow triggers another workflow or trigger, and so on. This can lead to errors like "Record was locked by another user."

9 - Updatable/required behave like formula fields, so they will execute multiple times. Be careful with the code written in them. 10 - Remember, OSC Groovy console is not a full-fledged IDE development environment like Eclipse, Visual Studio, NetBeans, Codeblocks, etc.

It has its limits, and not all JAVA classes will work.

These are just a few things to keep in mind when making your scripts and customizations. Hope this helps you avoid unpleasant situations.