Pablo Gonzalez 🇨🇷’s Post

View profile for Pablo Gonzalez 🇨🇷

HappySoup.io | AutoRABIT | Clean Apex Code

What if you wrote most of your Apex code like this? This is pure business logic. There's no mention of queries, DML operations, bypass logic, governor limits and all that. This is as close as you can get to the actual business requirement: "When an opportunity is closed won, create a contract". You can test this code with pure unit testing. If most of your code was written like this, then most of your tests wouldn't even need to call the database. This is known as the "functional core". Of course, you have to wrap this code with "infrastructure logic", which actually takes care of querying, updating the database, etc. You can learn more about modelling business logic in Apex at awaf.dev

  • text
Jannis Schreiber

Business Solution Architect and Salesforce Lead

3mo

It’s not business logic - it’s barely a business rule. No one will care about this part - that’s why it’s so easy to test. The relevant logic that anyone cares about is not the one-liner that makes the decision, but the „engine“ that executes this decision.

Alexander Vashchenko

Technical Architect @ Hyundai

3mo

createContract method should create a contract, returning results not enough;)  ClosedOpportunity as class name should also represent closed opportunity in some way. 

Shubham Joshi

Salesforce Consultant | 6X Salesforce Certified

3mo

Hi Pablo Gonzalez 🇨🇷 this is a really interesting approach! I noticed you’re using an enum in this example—just curious, is there a specific reason for choosing an enum over a simple boolean variable here? Would love to understand the design decision behind it.

EJ Bantz

Salesforce Consulting | Never Off Shored | Mulesoft Integration | Apex and LWC Coding | Wisconsin Dev Center

3mo

I'd rather have a CreateContractResponse class that has that decision property enum, but with maybe also some other related info like the recordId the response is related to.

Joán Ferreras

Lead Software Developer | Salesforce Certified Platform Developer II

3mo

Thanks for sharing this Pablo. What are your thoughts on how to handle such a Decision? I've been playing around with an idea of launching a Platform Event around certain Core Business process stages and I can see how adding a Decision as what populates the event model would be useful - especially when paired with a Transactional Outbox or similar pattern.

Vincent FINET

Senior Program Architect @ Salesforce | 23 Salesforce Certifications

3mo

The very first thing that is highlighted here is that a code should be easy to read. Now what does « easy to read » really mean? For sure it depends on who is reading… Also that approach would break the business versus technical people barriers Have you ever head: - business people saying it’s too technical they don’t know what the heck the dev team did in the app… - OR technical people saying that they implemented just a stupid business requirement which they don’t understand the value.

Simas Ramoška

Salesforce Architect at Telia Global Services Lithuania

2mo

Or just return a method that does the creation, instead of elongating bridge towards real logic that executes. Methods can also be named in readable manner, even CREATE_CONTRACT(); since apex is not case sensitive (not an advice)

See more comments

To view or add a comment, sign in

Explore content categories