Posts

Winter '21 Release Highlights - Programmatic Code

  Learn What’s New in Winter ‘21 Learning Objectives After completing this unit, you’ll be able to: Improve Apex testing with new SObject error methods. Develop flow screen components that work for multiple objects. Create custom property editors for Lightning web components. Enforce profile-based access for @AuraEnabled Apex classes. Make more powerful composite requests. Improve Apex Testing with New SObject Error Methods Track errors with the new  SObject.hasErrors()  and  SObject.getErrors()  methods without performing a DML operation to check the result for errors. Dynamically add errors to specific fields with new  SObject.addError()  overload methods. Use the  hasErrors()  method to know if an SObject instance contains errors. Use the  getErrors()  method to retrieve the list of errors for a specific SObject instance. If the  SObject.addError()  method has been called on an SObject instance, the  SObject.hasErr...

Get Hands-on With Field- and Object-Level Security and Safe Navigation Operator

Hands-on challenge solution for  Platform Developer I Certification Maintenance (Winter ‘21) @RestResource(urlMapping='/apexSecurityRest') global with sharing class ApexSecurityRest { @HttpGet global static Contact doGet() { Id recordId = RestContext.request.params.get('id'); Contact result; if (recordId == null) { throw new FunctionalException('Id parameter is required'); } //Refactored List<Contact> results = [SELECT id, Name, Title, Top_Secret__c, Account.Name FROM Contact WHERE Id = :recordId]; SObjectAccessDecision securityDecision = Security.stripInaccessible(AccessType.READABLE, results); if(!results.isEmpty()){ result = (Contact)securityDecision?.getRecords()[0]; result.Description = result?.Account?.Name; }else{ throw new SecurityException('You don\'t have access to all contact fields required to use this AP...

Using Aggregate SOQL queries/results in Batch Apex

I had created a schedulable batch apex that implements Database.Batchable<sObject> in Salesforce, but if you want to use SOQL having aggregate functions like SUM(), MAX(), COUNT() on results grouped by “GROUP BY” clause in start execution, changing to interface Database.Batchable<AggregateResult> isn’t a workable way, because it fails with the below compile error : Class must implement the global interface method: Iterable<AggregateResult> start(Database.BatchableContext) from Database.Batchable<AggregateResult> The following sample code will explain this. /* * Compile error : Class must implement the global interface method: Iterable<AggregateResult> start(Database.BatchableContext) from Database.Batchable<AggregateResult> */ global class SampleAggregateBatch implements Database.Batchable<AggregateResult> { // The batch job starts global Database.Querylocator start(Database.BatchableContext bc){ String query = 'SELECT COUNT(Id), ...

How to download the files using URL from salesforce

Note:   To download the file, require the  Content Version record Ids For Single File Use the below URL to download the single file from the salesforce. URL :    https://<YOUR_SFDC_BASE_URL>/sfc/servlet.shepherd/version/download/068XXXXXXXXXXXX   String idCntDoc = '068XXXXXXXXXXXX' ; String strURL = URL.getSalesforceBaseUrl().toExternalForm() + '/sfc/servlet.shepherd/version/download/' + idCntDoc; System.debug( 'URL ===> ' +strURL); For Multiple Files URL:  https://<YOUR_SFDC_BASE_URL>/sfc/servlet.shepherd/version/download/068XXXXXXXXXXXX/068XXXXXXXXXXXX above URL downloads the files in ZIP folder list<String> lstCntVersionIds = new list<String>{ '068XXXXXXXXXXXX' , '068XXXXXXXXXXXX' }; String strURL = URL.getSalesforceBaseUrl().toExternalForm() + '/sfc/servlet.shepherd/version/download/' ; for (String iterator : lstCntVersionIds) { strURL += iterator + '/' ; } strURL = strURL.removeEnd( '/...

File Preview in Visualforce salesforce

Ever notice how cool, the preview functionality in Salesforce Chatter is? Well, you can reuse the same component that Salesforce does.   Chatter uses a Shockwave Plugin to preview files. We can make use of the same plugin using the  <embed>  tag in VisualForce. Here's the tag: <embed src="/_swf/190003/sfc/flex/DocViewer.swf" flashvars="shepherd_prefix=/sfc/servlet.shepherd&v= <DocumentVersionId> &mode=chatter_bubble&in_tests=false" width="100%" height="100%" align="middle" id="renditionLarge" quality="high" bgcolor="#f3f3f3" name="renditionLarge" allowscriptaccess="always" allowfullscreen="true" pluginspage="http://www.adobe.com/go/getflashplayer" wmode="opaque" type="application/x-shockwave-flash"> The only thing you need to add to the link in the Document Version ID. (Not the Document ID) You get the Version ID f...

The Difference Between REST and SOAP APIs

This overview will help you understand the differences between REST and SOAP APIs, and how they are used in integrations. The words "web services" mean many things to people with different fields. For general users, it is about using online services, like surfing the internet, but for developers and webmasters, it has different meanings. Overall it is a broad term that tells us how the communication between two different set of devices or applications held over the World Wide Web (WWW). This communication system can be categorized into two types, namely Simple Object Access Protocol or SOAP, and Representational State Transfer or REST. Quite often both are considered to be the terms with same meanings but the how both works and what tools both use for communication purposes creates the fine line between two. Before highlighting the differences between two, it merits to discuss what both actually are. What Is a REST API? REST is basically an architectural style of the web serv...

System.QueryException: Aggregate query has too many rows for direct assignment, use FOR loop

This exception is talking about an “aggregate query” not an “aggregate function.” I had never heard the term “aggregate query” before, and I suspect you haven’t either. After hunting around, I found the relevant Salesforce documentation – Salesforce Apex Developer Guide: SOQL For Loops. The documentation explains that Salesforce might throw the System.QueryException when a query has a sub-query (this is the “aggregate query”), and our sub-query returns more than 200 child records. For example, if we have an Account with more than 200 AccountTeamMembers, we might get the exception when we run the following code with a query on Account with a sub-query for child AccountTeamMembers.  Code that could throw an exception if there are more than 200 child records: List<Account> accounts = [Select Id, (Select Id, AccountId , UserId From AccountTeamMembers) , (Select Id , OwnerId, AccountId from Opportunities) From Account]; for ( Account acc : accounts ) {     // Either...