Showing posts with label Visualforce. Show all posts
Showing posts with label Visualforce. Show all posts

Tuesday, May 12, 2020

Navigate to Visualforce page from lightning web components(lwc)

To navigate to visualforce page we have to use the Navigation Service in Lightning web components.

Check below for more details about navigation service in Lightning web components

Sample Code

//Navigate to visualforce page
navigateToVFPage() {
    this[NavigationMixin.GenerateUrl]({
	    type: 'standard__webPage',
	    attributes: {
		    url: '/apex/SampleVFPage?id=' + recId
	    }
    }).then(vfURL => {
	window.open(vfURL);
    });
}

Monday, January 21, 2019

Show Toast messages in Visualforce pages

This post explains how to fire toast messages from visualforce page.
In spring 19 release salesforce introduces the sforce.one.showToast({toastParams}) function in sforce.one object.
by using the function you can show different severity like success, error, info, warning.
Note: The change applies to Lightning Experience, Lightning communities, and all versions of the mobile app.
Example:
VF page
<apex:page lightningStylesheets="true">
    <script>
        function showToast() {
            sforce.one.showToast({
                "title": "Success!",
                "message": "Welcome to salesforce code crack.",
                "type": "success"
            });
        }
    </script>
    <apex:form >
        <apex:page >
            <apex:commandButton value="Show Toast" onclick="showToast();" />
        </apex:page>
    </apex:form>
</apex:page>
Enable the Available for Lightning Experience, Lightning Communities, and the mobile app permission.
Check below screenshot
once you enable the checkbox, add the visualforce page in salesforce mobile navigation
Demo

Happy Learning!!!

Sunday, December 30, 2018

Use of Inherited Sharing in Apex Class


This post explains the use of inherited sharing in apex class
If you specify inherited sharing Keyword on an Apex class,  which allows the class to run in the sharing mode of the class that called it. Using inherited sharing enables you to pass security review and ensure that your privileged Apex code is not used in unexpected or insecure ways.

  1. An Apex class with inherited sharing runs as with sharing when used as a Visualforce page controller, Apex REST service, or an entry point to an Apex transaction.
  2. An Apex class with Inherited Sharing is being called from some other class which is having without sharing setting, then it will run in without sharing mode.
Example
This example declares an Apex class with inherited sharing and a Visualforce invocation of that Apex code. Because of the inherited sharing declaration, only opportunities for which the running user has sharing access are displayed. If the declaration is omitted, even opportunities that the user has no rights to view are displayed due to the insecure default behavior of omitting the declaration.
Apex Class
public inherited sharing class OpportunityMethods {
    public list<Opportunity> getAllTheOpps(){
        return [SELECT Id, Name, StageName FROM Opportunity];
    }
}
Visualforce page
<apex:page controller="OpportunityMethods">
    <apex:repeat value="{!getAllTheOpps}" var="record">
        {!record.Name}
    </apex:repeat>
</apex:page>

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!!!

Monday, September 24, 2018

How to Redirect to URL Based on Theme and Site Name in Salesforce

If you are Redirecting URL From Button and That Button Used In Different Places Like Community, Partner Portal, Lightning Experience.
In Lightning Experience, URL is Different Compare to the Classic Theme URL.
So First You Need to check the Current User Theme Based On Theme You Need to Redirect to The URL.
Check Bellow themes are available in Salesforce.

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 —> Salesforce1 mobile Salesforce theme
PortalDefault —> Salesforce Customer Portal theme
Webstore —> Salesforce AppExchange theme

To Check the Current theme in Visualforce page use below Global Variables

{!$User.UIThemeDisplayed}
OR
In Apex UserInfo.getUiThemeDisplayed().
To Check Site Name in Visualforce Page Use Global Variable
{!Site.Name}
In Portal or Communities URL is Different like
https://Baseurl/<portalName>/apex/apexpage/
Demo:
Check Below VisualForce Page to understand easily
<apex:page standardController="Account" showHeader="false" sidebar="false">
    <script>   
     if(typeof sforce !== 'undefined' && sforce !== null && '{!$User.UIThemeDisplayed}' === 'Theme4d') {
         window.open('/apex/ApexTestPage?id={!Account.Id}');
         sforce.one.back(true);
     }
     else if('{!$User.UIThemeDisplayed}' !== 'Theme4d' && ('{!$Site.Name}' == undefined || '{!$Site.Name}'  == '' || '{!$Site.Name}'  == null)) {
        window.open('/apex/ApexTestPage?id={!Account.Id}','_blank');
        window.top.location = '/{!Account.Id}';
     }
     else if('{!$Site.Name}' != null || ('{!$User.UIThemeDisplayed}' === 'Theme4d' && '{!$Site.Name}' != null)) {
         var siteName = '{!$Site.Name}';
         siteName = siteName.toLowerCase(); 
         window.open('/'+siteName+'/apex/ApexTestPage?id={!Account.Id}','_blank');
         window.top.location = '/'+siteName+'/{!Account.Id}';
     }
    </script>
</apex:page>

for Testing, Create Detail Page Button on Account(I Used Standard Controller as Account), Based on Your Preference create button and add the Page.
After that Add Button in Layouts based on your Preferences Like(Community, Portal, Lightning Experience).

Note: You can change the Script in Visualforce Page based on Your Requirement and you Can Extend the Functionality Further.

References:
Site Reference In Visualforce Page
sforce Methods
Let us know if you have any queries.

Happy Learning!!!

Friday, September 7, 2018

Calculate Total Sum of Column in PageBlockTable In Visualforce Using Jquery

Some Particular Scenarios we need to Calculate the total sum value Based on User Enter Values in Text Boxes in PageBlockTable.

Recently I Faced this Scenario We can Resolve The Scenario By Using JavaScript or Jquery.
This Code work on input Event, You can Use any Event Based on Your Requirement(onchange, onblur, onfocus etc).


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
         $(document).on("input", ".pricevalue", function (e) {
            var sum = 0;
            $('.pricevalue').each(function(){
                var num = $(this).val();
                if(num != 0 && !isNaN(num)) {
                    sum += parseFloat(num);
                }
            });
            $(".outputPrice").text(sum.toFixed(2));
        });
    });
    </script>
And This Is the Column Code in PageBlockTable
<apex:column headerValue="Unit Price">
  <apex:inputText value="{!wd.dUnitPrice}" styleClass="pricevalue" id="unitpric" />
  <apex:facet name="footer">
    <apex:outputText id="totalPrice" value="{!dSumValue}" styleClass="outputPrice" />
  </apex:facet> 
</apex:column>

Check Screenshot For More information
Let us know if you have any queries.
Happy Learning!

Sunday, August 26, 2018

Dynamically Add Row / Remove Row in PageBlock Table in Visualforce Page

In this Post, Explore More about the How To Add Row or Remove Row in PageBlockTable in Visualforce Page

To achieve this Functionality I Created The One Simple Apex Controller and Visualforce Page.

Apex Class
public class AddOrRemoveRowController {
    private Integer counter = 0;
    public list<WrapperData> lstWrapperData {get;set;}
    
    public AddOrRemoveRowController() {
        lstWrapperData =  new list<WrapperData>();
        addRow();
    }
    
    // Adding Rows 
    public PageReference addRow() {
        counter++;
        lstWrapperData.add(new WrapperData(new Account(), counter));
        return null;
    }
    
    // Removing The Rows
    public PageReference removeRow() {
        Integer iRemoveRow = Integer.valueOf(Apexpages.currentpage().getParameters().get('index'));
        for(Integer i=0; i<lstWrapperData.size(); i++) {
            if(lstWrapperData[i].iIndex == iRemoveRow ) {
                lstWrapperData.remove(i);     
            }
        }
        return null;
    }
    
    // Wrapper Class
    public class WrapperData {
        public Account objAcc {get;set;}
        public Integer iIndex {get;set;}
        
        public WrapperData(Account objAcc, Integer iRow) {
            this.objAcc = objAcc;
            this.iIndex = iRow;
        }
    }
}
Visualforce Page
<apex:page controller="AddOrRemoveRowController" tabStyle="Account">
    <apex:form >
        <apex:pageBlock >
            <apex:outputPanel id="showpageBlockData" >
                <apex:pageBlockSection columns="1" title="Dynamically Add or Remove Row" collapsible="false">
                    <apex:pageBlockTable value="{!lstWrapperData}" var="wd" id="pgb" >
                        <apex:column headerValue="Account Name">
                            <apex:inputField value="{!wd.objAcc.Name}" />
                        </apex:column>
                        <apex:column headerValue="Industry">
                            <apex:inputField value="{!wd.objAcc.Industry}" />
                        </apex:column>
                        <apex:column headerValue="Phone">
                            <apex:inputField value="{!wd.objAcc.Phone}" />
                        </apex:column>
                        <apex:column headerValue="Account Type">
                            <apex:inputField value="{!wd.objAcc.Type}" />
                        </apex:column>
                        <apex:column > 
                            <apex:commandLink value="Add Row" action="{!addRow}" reRender="pgb" style="color:#61afe8;" />&nbsp;&nbsp;||&nbsp;&nbsp;
                            <apex:commandLink value="Remove Row" action="{!removeRow}" reRender="pgb" style="color:red;" >
                                <apex:param value="{!wd.iIndex}" name="index" />
                            </apex:commandLink>
                        </apex:column>
                    </apex:pageBlockTable>
                </apex:pageBlockSection>           
            </apex:outputPanel> 
        </apex:pageBlock>
    </apex:form>
</apex:page>

Happy Learning!!!