How to make a REST callout/integration from Salesforce to Github REST API

Usually to integrate with an external system that supports REST API, you need to make REST callouts from Salesforce. This blog demonstrates how to make a REST callout from Salesforce to Github.

Demo



Code Sample


Steps

  • Add remote site settings
  • Create GithubApi class with a method to make API call
  • Copy code from code snippets and save class
  • Execute the class in developer console using System.debug(GithubApi.getGithubUserDetails('SalesforceCodes'));

How to customize Salesforce lightning community layout - Custom Theme layout

Salesforce lighting community comes with a number of predefined layouts. But most of the times these layouts do not match your requirements/company branding. In this blog we will go through how we can customize the layout of a Salesforce lightning community. It is done by creating "custom theme layout" using aura components.

Use cases

  • Provide better user experience and branding through Salesforce lightning community
  • Build a custom, mobile responsive layout that is not available in standard predefined templates like Napili template.


Explanation

Here we are creating a custom theme layout that we can use to customize the layout of community pages. First step is to create an aura component that implements forceCommunity:themeLayout interface.
Now as part of the layout, you might have some things that are static between different pages (For example company logo on top left). Also you might have some content that changes between different pages in the community that uses the layout. All static content that doesn't change between pages like logo can be directly coded into the layout itself. Then you need to create attributes that are array of aura components to make some sections dynamic. In the above example we are defining a header attribute at line 2 as an array of Aura.Component. Then we use that attribute on line 10 using the syntax {!v.header}. This simply says that When the layout is renderd in community builder, provide a drag and dropable area of lightning components at line 10.

How to use

  • Create an aura component with name MyCustomLayout
  • Copy paste above code into the component.
  • Go to the design file of the aura component (MyCustomLayout.design) and give a title you like in the opening tag like design:component label="My Responsive Theme Layout"
  • Now go to your lightning community builder theme settings and define a theme layout, Setup => All communities => Builder => Gear icon in left floating menu => Theme => Configure Defining Theme Layout
  • Once you have defined a custom theme like this, you will be able to use that in any existing community builder pages or new community builder pages that you create
  • To use the new layout in an existing community page, simple select settings of the page and check "Override the default theme layout for this page" option at the bottom.
    Changing theme layout of existing page

How to create a scratch org with "Non Profit Starter Pack (NPSP)"

Salesforce scratch orgs makes it very easy to track metadata changes. But if you are developing an app that involves non profit starter pack, it is usually tricky to setup a scratch org with NPSP packages. In this blog, we will go through different steps needed to setup a scratch org with NPSP packages.
NPSP is basically a combination of multiple appexchange packages. But to install these your sandbox/scratch org needs to meet certain conditions. These prerequisites are adding certain recordtypes to Account and Opprotunity and adding an Opprotunity sales process. Metdata needed for these can be found in the link - https://github.com/SalesforceFoundation/NPSP/tree/master/unpackaged/pre. If you wish to run below bash script directly, you need to copy individual metadata files from above NPSP github repo into a single npsp-dependencies folder in deployable format with a combinted package.xml file.

Use cases

  • Easy development of enhancements to NPSP in scratch orgs
  • Other approaches like using Cumulus CI are usually more time consuming and difficult to setup


Explanation

Above shell script will work for Mac and Linux users only. If you are a windows user, you might need to copy-paste and run individual commands separately from windows command line. As part of this script we are first creating a scratch org. Then we open the scratch org in browser. After that at line 14 we install all the prerequisites for NPSP package. Then we install 6 NPSP appexchange packages one after another. Please note that in future Salesforce might upgrade these packages and the package IDs will change. In that case find package ID corresponding to the upgraded version and change in the script.

How to use

Make sure that you are inside an sfdx project folder. If not, create a SFDX project using vscode or SFDX cli sfdx force:project:create -n projectName. As first step, make sure that you have copied all dependencies from https://github.com/SalesforceFoundation/NPSP/tree/master/unpackaged/pre to a folder with name npsp-dependencies inside your repository. Make sure you have set a devhub environment as default that you can use for spinning off scratch orgs( sfdx force:config:set defaultdevhubusername=productionUsernameHere ). Copy above code into a shell script file. *Example createScratchOrg.sh) Make sure that your shell script file has proper permissions by running chmod u+x createScratchOrg.sh. Finally run the shell script from terminal using ./createScratchOrg.sh.
Since it is installing 6 different appexchange packages as part of NPSP, the whole process might take some time. You can see the progress.

How to connect to Salesforce metadata API from a Visualforce Page

Meatadat API allows you to connect to Salesforce to create, modify and delete Salesforce metadata. This API is designed for external systems/clients to connect to Salesforce. But there will be use cases where you need to connect to metadata API from Salesforce itself and make changes to the metadata in same org/sandbox.

In this blog we will go through creating a custom visualforce page that you can use to connect to Salesforce metadata API and create objects in Salesforce.

Use cases

  • Update metadata(create objects, create fields, page layouts etc) from visualforce
  • To provide a single user interface for admins to do different actions that otherwise would require going through different setup sections (Example :- post installation setup of complex appexchange apps)


Explanation

We are using jsforce library to interact with Salesforce metadata. In this particular example we are creating a custom object in Salesforce. First we load jsforce library from CDN using apex:includeScript tag. Then we initialize salesforce connection in jsforce library with logged in user's session id. createObject function reads label and API name of desired object from HTML input tags and calls jsforce method available on connection instance conn.metadata.create. We are passing metadata type, object metadata as a JSON array and a callback function to this method. Once Salesforce has processed the request, callback will be called with error/success response. To simplify things we are just alerting the result we are getting in the callback. But we can definitely build a good user interface and provide complex metadata manipulation functionalities using the ability to interact with metadata.

Creating an object is just an example usecase. You can do a lot more things with jsforce library. Examples can be found in jsforce documenation. jsforce metadata


How to use

  • Create a visualforce page in your org
  • Copy paste above code into your page. There is no controller or other dependencies.
  • Go to below url to see the resulting page
    {{yourSalesforceBaseUrl}}/apex/{{NameOfThePageYouCreated}}