Posts

Update Custom Metadata Types using Metadata Api

Image
With the introduction of Custom Metadata Types , the use of Custom Settings have been marginally reduced. Custom Metadata Types gives more flexibility in context of creating/maintaining Metadata, however when it comes to the point of updating Custom Metadata Types it involves some manual steps to be done from the Admin side which is a bit pain! With the help of Metadata Api this process can be made more simple just by writing some Apex. Lets go ahead and put some code to make use of Metadata Api and update Custom Metadata Types. Step 1: Create a new Custom Metadata Type "US_States" with one custom field "State__c". Step 2: Create few records for the above Custom Metadata Type using "Manage" button. Step 3: When we say that we update Custom Metadata Types using Metadata Api, behind the scenes it does an actual deployment and we use below class to track those deployment results. Create an Apex class "CustomMetadataCallback.apxc" u...

REST Service to Expose Attachments as Public CURL

Image
My use case behind this blog post is to expose Salesforce Attachment (Image/Pdf/Video) as a public accessible CURL, without having to authenticate to Salesforce. And we are going to use REST Web-services and Public Sites to do this! Step 1: Create an Apex REST class with below code snippet. /* * Purpose : Apex rest webservice to return attachment body for a given id * * Developer: SFDC_Dev * */ @RestResource (urlMapping = '/Document_V1/*' ) global class DocumentV1 { @HttpGet global static void docBody() { RestRequest request = RestContext . request; RestResponse res = RestContext . response; try { // Retrieve attachment id from the request url String docId = request . requestURI . substring(request . requestURI . lastIndexOf( '/' ) + 1 ); // Query attachment sObject to get the specific attachment using attachment id ...

Few Apex Scenario Based Programming Questions

Image
Recently at one of the local Hackathon I came across some scenario based Apex programming questions and I thought I would go ahead and blog on them. Below are three scenarios on which we are going to write some Apex code! Scenario 1: Create a Method which accepts List of String and returns a Map with count on how many times a specific value is repeated in a given List. Example:- List should result in "{A=3, B=2, C=1}" Approached Solution: Below is my Apex class with a method to accept List and returns a Map. /* * Purpose: Hackathon Programming Scenarios * * Developer: HackathonDev */ public class HackathoScenarios { // Method to return a map with string occurence count that was passed in a list public static Map < String, Integer > scenario1(List < String > lstStrng) { Map < String, Integer > mapVals = new Map < String, Integer > (); for (String s: lstStrng)...

Lightning RecordForm - An enhanced Lightning Data Service

Image
As always with every release Salesforce introduces bunch of new base components and this time they have "Lightning: recordForm" which suppresses using "lightning:recordEditForm and lightning:recordViewForm" separately to handle record view and edit. Lets go ahead and put some sample code to create a Lightning Component using lightning recordForm! RecordFormComp.cmp: <aura:component implements= "force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasSObjectName,force:hasRecordId,force:lightningQuickAction" access= "global" > <aura:attribute name= "fieldsList" type = "String[]" default= "['Id', 'Name', 'BillingAddress', 'AnnualRevenue']" /> <div> <center> <h1><b>Lightning Record Form<br/> Object_Name: { ! v.sObjectName} - Record_Id: { ! v.recordId} ...

Using Component.find vs Document.getElementById to Dynamically Hide or Show Content in Lightning Components

Image
Business Scenario: To dynamically Show or Hide content in Lightning Components. Using "Document.getElementById" Below are Component and Controller code to show or hide content by using Document.getElementById HideShow.cmp <aura:component implements= "force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access= "global" > < ! --Set component attribute--> <aura:attribute name= "valSet" type = "Set" /> <center> < ! --Button to display Set values--> <lightning:button onclick= "{!c.getSetValues}" label= "Set Values" /> </center> < ! --Construct HTML Table using SLDS--> <table class= "slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout" > <thead> <tr c...

Collections In Lightning Components

Image
Collections are building blocks for any object oriented programming language. In Apex we use LIST, SET, MAP collection types in order to work with records! Here in this blog, we are going look at some basic code on using Collection Types in context with Lightning Components..... First we create an Apex Controller with two methods to return List and Map respectively. We will use these methods to access from our Lighting Component and display them on the Home Page. public class LightningCollectionTypes { @AuraEnabled public static List < Account > lghtList() { List < Account > lstAccnts = [SELECT Id, Name FROM Account LIMIT 22 ]; return lstAccnts; } @AuraEnabled public static Map < Id, Account > lghtMap() { Map < ID, Account > m = new Map < ID, Account > ([SELECT Id, Name FROM Account LIMIT 22 ]); return m; } } LIST Below are component and...