Tag: Apex

Apex Trigger Frameworks

As most of you already know, we should use a trigger framework in order to manage properly the logic that is executed after we create, update or delete a record by using Apex. The main reason is that we cannot control the order of execution of the different triggers that we can have per object. […]

Frameworks for testing Apex Code

I would like to share a couple of framworks that are very useful for testing Apex Code. The first one is Apex Mock and it will become essential for you when it comes to create test data for your Apex Test classes. The second framework is FinancialForce ApexMocks Framework. This one allows you to decouple complex logic […]

Get code coverage using REST API query

Let’s suppose you need to find all the classes or triggers starting with “MY_” and having 0 lines covered. The REST API query we need would be as follows: /services/data/v29.0/tooling/query/q=SELECT+ApexClassOrTriggerId,+ApexClassOrTrigger.Name,+NumLinesCovered,+NumLinesUncovered +FROM+ApexCodeCoverageAggregate +++WHERE+NumLinesCovered+=+0+AND+NumLinesUncovered+!=+NULL +AND+ApexClassOrTrigger.Name+like+’MY_%25’+ORDER+BY+ApexClassOrTrigger.Name Obviously, it will return a JSON string with the classes and triggers which match the criteria. There are a lot of online […]

Retrieving blob objects from Azure with Apex

Maybe you need to retrieve blobs stored in Azure Storage Services and save them as Salesforce attachments. Let’s suppose we have an attachment base64 encoded stored as a blob in Azure. We could retrieve it with the following snippet: String blobName = ”; String urlQueue = ‘https://.blob.core.windows.net//’ + blobName; string storageKey = ”; Datetime dt […]

Sending e-mails from Salesforce using Amazon SES

SFDC has increased from 1000 to 5000 the number of e-mails that can be sent to external e-mail addresses within Winter ’17 release. This is really good news but still, in some cases, can be insufficient. I want to show you how you can send e-mails, in both HTML and text plain versions and with […]

Named credentials using OAuth

Sometimes you need to use the Rest API in your Apex Code to make callouts to other Salesforce Orgs or even to the same Org where you are executing your code. You could use Named Credentials to avoid storing user and password credentials (normally in an unsecure way) with OAuth. We will need the following […]

Setting RecordType for new records without query

My godness, how many queries would force.com save if all of us knew this? insert new Task ( Subject = ‘Your appointment is at noon’, RecordType = new RecordType (Name = ‘SMS’) ); How come has this gone unnoticed? Anyway, better late than never!

CreatedDate in test classes

Finally Salesforce fills the gap and allows you to specify the createddate field within test classes. You only have to use the Test.setCreatedDate method introduced with Spring ’16. @isTest private class CreatedDateTest { static testMethod void test() { Contact c = new Contact(FirstName =’Marc’, LastName = ‘Benioff’); insert c; Test.setCreatedDate(c.Id, DateTime.newInstance(2016, 1, 26)); Test.startTest(); Contact […]

Manage UserTerritory table through REST API

UserTerritory is one of the few objects that don’t allow DML Operations. However, we can manage it through the REST API. We could assign a User to a Territory by using the following APEX code: HttpRequest req = new HttpRequest(); Http http = new Http(); String baseUrl = URL.getSalesforceBaseUrl().toExternalForm(); req.setEndpoint( baseUrl + ‘/services/data/v34.0/sobjects/UserTerritory/’); req.setHeader(‘Authorization’, ‘OAuth […]

Defensive future calls

I would like to talk about the wonderful and simple pattern for making future calls. This pattern was presented by Dan Appleman at one of his sessions at Dreamforce this year. It looks like this: public void CallFuture2() { if(System.isFuture() ||system.isBatch()) defensiveFutureCallSync(); else { if(Limits.getFutureCalls() < Limits.getLimitFutureCalls()) defensiveFutureCallAsync(); else { // error handling } } […]