Tuesday, October 30, 2018

lightning:notificationsLibrary in lightning

This post explains the use of lightning:notificationLibrary in lightning component
lightning:notificationLibrary is supported in Lightning Experience, Salesforce app, and Lightning communities.
include one <lightning:notificationsLibrary aura:id=”notifLib”/> tag in the component that triggers all the notifications, and aura:id is a unique local id in the component.



Messages can be displayed in notices and toasts.
Notices
  1.  Notices alert users to system-related issues and updates.
  2. Notices interrupt the user's workflow and block everything else on the page. Notices must be acknowledged before a user regains control over the app again.
  3. To dismiss the notice, only the OK button is currently supported.

Toasts
  1. Toasts enable you to provide feedback and serve as a confirmation mechanism after the user takes an action.
  2. Toasts are less intrusive than notices and are suitable for providing feedback to a user following an action, such as after a record is created. 
  3. A toast can be dismissed or can remain visible until a predefined duration has elapsed.

Demo
To create and display a notice, pass in the notice attributes using component.find('notifLib').showNotice(), where notifLib matches the aura:id on the lightning:notificationsLibrary instance.

To create and display a toast, pass in the toast attributes using component.find('notifLib').showToast(), where notifLib matches the aura:id on the lightning:notificationsLibrary instance.

Check below code sample for more information
Lightning Component
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
    <lightning:notificationsLibrary aura:id="notifLib"/>
    <lightning:buttonGroup>
        <lightning:button variant="Neutral" label="Notice Info" onclick="{!c.showNoticeInfo}"/>
        <lightning:button variant="Neutral" label="Notice Error" onclick="{!c.showNoticeError}"/>
        <lightning:button variant="Neutral" label="Notice Warning" onclick="{!c.showNoticeWarning}"/>
        <lightning:button variant="Neutral" label="Toast Info" onclick="{!c.showToastInfo}"/>
        <lightning:button variant="Neutral" label="Toast Warning" onclick="{!c.showToastWarning}"/>
        <lightning:button variant="Neutral" label="Toast Success" onclick="{!c.showToastSuccess}"/>
        <lightning:button variant="Neutral" label="Toast Error" onclick="{!c.showToastError}"/>
    </lightning:buttonGroup>
</aura:component>
Javascript Controller
({
    showNoticeInfo : function(component, event, helper) {
        component.find('notifLib').showNotice({
            "variant": "info",
            "header": "Something has gone wrong!",
            "message": "Unfortunately, there was a problem updating the record.",
        });
    },
    
    showNoticeError : function(component, event, helper) {
        component.find('notifLib').showNotice({
            "variant": "error",
            "header": "Something has gone wrong!",
            "message": "Unfortunately, there was a problem updating the record.",
        });
    },
    
    showNoticeWarning : function(component, event, helper) {
        component.find('notifLib').showNotice({
            "variant": "warning",
            "header": "Something has gone wrong!",
            "message": "Unfortunately, there was a problem updating the record.",
        });
    },
    
    showToastInfo : function(component, event, helper) {
        component.find('notifLib').showToast({
            "variant": "info",
            "title": "Notif library Info!",
            "message": "The record has been updated successfully."
        });
    },
    
    showToastWarning : function(component, event, helper) {
        component.find('notifLib').showToast({
            "variant": "warning",
            "title": "Notif library Warning!",
            "message": "The record has been updated successfully."
        });
    },
    
    showToastSuccess : function(component, event, helper) {
        
        component.find('notifLib').showToast({
            "variant": "success",
            "title": "Notif library Success!",
            "message": "The record has been updated successfully."
        });
    },
    
    showToastError : function(component, event, helper) {
        component.find('notifLib').showToast({
            "variant": "error",
            "title": "Notif library Error!",
            "message": "The record has been updated successfully."
        });
    },
})
Let us know if you have any queries.
Happy Learning!!!

Thursday, October 25, 2018

Checking UI Theme Detection in Visualforce and Apex

UI Theme Detection can be done in one of two ways:

Accessing global variables:
$User.UITheme – Returns the theme that is supposed to be used.
$User.UIThemeDisplayed – Returns the theme that is actually being used.

Calling Apex utility methods:
UserInfo.UITheme() – Returns the theme that is supposed to be used
UserInfo.UIThemeDisplayed() – Returns the theme that is actually being used

Currently, Salesforce supports below themes
Theme1—Obsolete Salesforce theme
Theme2—Salesforce Classic 2005 user interface theme
Theme3—Salesforce Classic 2010 user interface theme
Theme4d—Modern “Lightning Experience” Salesforce theme
Theme4t—Salesforce mobile app theme
Theme4u—Lightning Console theme
PortalDefault—Salesforce Customer Portal theme
Webstore—Salesforce AppExchange theme

UI theme detection using global variables
check below sample code

<apex:page>
    <apex:pageBlock title="My Content" rendered="{!$User.UITheme == 'Theme2'}">
        // this is the old theme...
    </apex:pageBlock>

    <apex:pageBlock title="My Content" rendered="{!$User.UITheme == 'Theme3'}">
       // this is the classic theme ...
    </apex:pageBlock>
</apex:page>

UI theme detection Using Apex Utility Methods
Check below sample code

public with sharing class UIThemeController {
   @AuraEnabled
    public static String getUIThemeDescription() {
        String theme = UserInfo.getUiThemeDisplayed();
        return theme;
    }
}

Use the UI theme detection based on your requirement.
Let us know if you have any queries.
Happy Learning!!!

Wednesday, October 3, 2018

How to get governor limits of current org using REST API Callout

This Post explains the how to get the governor limits of current org using REST API Callout.

Some Cases we need to check the Max limit value and remaining limits values of current org in the apex.
For Example
Single Email Limit Values, Max limit value of Single Emails of Current org(Based on Licences it will depend) and How many are already invoked in a day.
Check below link to get limits by using the workbench
https://www.salesforcecodecrack.com/2018/09/how-to-check-your-org-governor-limits.html

if you need to check limits in Apex code, this post may help for you

Note: 
your salesforce instance URL must be added in your org remote site settings.

Check below class
/*
purpose: This Class gives response based on resource name you entered.
Author: Shaik Nagajani
Date: 10/3/2018
*/

public class OrgREST {
    
    public static String retriveResult(String strResourceName) {
        String response;
        String strEndPonitURL = URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v43.0/' + strResourceName;
        if(String.isNotBlank(strResourceName)) {
            HttpRequest httpRequest = new HttpRequest();  
            httpRequest.setMethod('GET');   
            httpRequest.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());        
            httpRequest.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); 
            httpRequest.setEndpoint(strEndPonitURL); 
            
            try {  
                Http http = new Http();   
                HttpResponse httpResponse = http.send(httpRequest);  
                if (httpResponse.getStatusCode() == 200 ) {  
                    response = httpResponse.getBody();  
                } 
                else {  
                    throw new CalloutException(httpResponse.getBody());  
                }   
            } 
            catch(Exception ex) {  
                throw ex;  
            }  
        } 
        return response;
    }
}
Open Developer Console, Execute below code from the anonymous window
String response = OrgREST.retriveResult('limits');
above class returns the response,
the response looks like(the small part of response) below.

"HourlyODataCallout" : {
    "Max" : 1000,
    "Remaining" : 1000
  },
  "HourlySyncReportRuns" : {
    "Max" : 500,
    "Remaining" : 500
  },
  "HourlyTimeBasedWorkflow" : {
    "Max" : 50,
    "Remaining" : 50
  },
  "MassEmail" : {
    "Max" : 10,
    "Remaining" : 10
  },
  "Package2VersionCreates" : {
    "Max" : 50,
    "Remaining" : 50
  },
  "PermissionSets" : {
    "Max" : 1500,
    "Remaining" : 1497,
    "CreateCustom" : {
      "Max" : 1000,
      "Remaining" : 997
    }
  },
  "SingleEmail" : {
    "Max" : 15,
    "Remaining" : 15
  },

Go to developer console, open execute anonymous window, copy below code and past in anonymous window
Integer iMax = 0;
Integer iRemaining = 0;
// Sending Request to the Server
String response = OrgREST.retriveResult('limits');

// deserializing the response
map<String, Object> mapJsonData = (map<String, Object>)JSON.deserializeUntyped(response);

// getting single Email max limit value and Remaining limit value 
map<String, Object> mapLimitValues = (map<String, Object>)mapJsonData.get('SingleEmail');
iMax = (Integer)mapLimitValues.get('Max');
iRemaining = (Integer)mapLimitValues.get('Remaining');

System.debug('Single Email Max Limit Vlaue====> '+iMax);
System.debug('Single Email Remaining Limit Vlaue====> '+iRemaining);

map<String, Object> mapLimitValues1 = (map<String, Object>)mapJsonData.get('DailyWorkflowEmails');
iMax = (Integer)mapLimitValues1.get('Max');
iRemaining = (Integer)mapLimitValues1.get('Remaining');

System.debug('DailyWorkflowEmails Max Limit Vlaue====> '+iMax);
System.debug('DailyWorkflowEmails Remaining Limit Vlaue====> '+iRemaining);
from above code, I checked single Email Max Limit, Remaining Limit Value and DailyworkfowsEmails invocation limit values.
Check below screenshot for more information about the result.

Output:

Based on Your Requirement, get the max limit and remaining limit values.
Let us Know if you have any queries.
Happy Learning!!!