Posts

Showing posts with the label Apex

Small things in Apex which we can achieve in a smart way! - Part 2

Hope you guys enjoyed reading Part1. Here we go! Below mentioned are few things which you might have missed while coding. Happy reading!!. Mixed Save Actions on sObject Lists:                While working in Trigger or an ApexClass, we generally prepare different lists of multiple object types and perform DML operations for each sObject list. For example, to update accounts, contacts & cases we prepare List<Account>, List<contact> & List<case>, then we update each list separately which consumes unnecessary DML operations.                Instead, we can simply put all the three lists into a single sObject list and perform the DML only once.                Let's get into details on how this works. In the above code snippet contactList   contains 3 records say c1, c2, c3,  accountList has 2 records say a1, a2 and caseList has...

Small things in Apex which we can achieve in a smart way! - Part 1

This article discusses on the small things that we overlook while writing code in Apex. You might have got habituated in doing the below explained items in your own usual way, but there is still a better way to achieve them. Try it! 1. Sending records to @future method:                   We all know that future methods run in asynchronous mode, and will not accept Sobject types as parameters. Consider this scenario, where we need to send List<Account> to a future method. As we can't send the List<Account>, we usually send the record ids to future method, and then query the records in future method.                  An alternate way to do this is we can send the the serialized list to future method. We can serialize the List<Account> using Json.serialize() method. We can deserialize the list in future method using Json.deserialize(). Below is the syntax to serialize ...

Export Salesforce standard Report in Visualforce page

Many of us might be developing some custom code to export data to excel. You can use this simple hack to generate excel from Salesforce standard report. String reportDeveloperName = 'Sample_Report'; reports = new List ([select developername,id from report where developername= :reportDeveloperName]); mailLogReportUrl = URL.getSalesforceBaseUrl().toExternalForm() + '/' + reports[0].id + '?pv0=' + pv0 + '&export=1&enc=UTF-8&xf=xls';