Play Games

Search This Blog

Monday, September 28, 2020

How to check whether a salesforce instance is sandbox or not in apex Salesforce - Salesforce Globe For You

 How to check whether a salesforce instance is sandbox or not in apex Salesforce  - Salesforce Globe For You 

Solution: The 'IsSandbox' field of the organization object in salesforce gives the information where the instance is sandbox or production.

Name field:Name of the organization

InstanceName field:gives the instance name.

URLhttps://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_organization.htm

Go to query editor of developer console and use the following query as shown in image

Select id, IsSandbox, OrganizationType from organization

Output:



How to know which salesforce Edition the salesforce instance is - Salesforce Globe For You

 How to know which salesforce Edition the salesforce instance is  - Salesforce Globe For You 

Solution: The 'OrganizationType' field of the organization object gives the edition of the salesforce.

URL: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_organization.htm

Go to query editor of developer console and use the following query as shown in image

Select id, IsSandbox, OrganizationType from organization

Output:




How to get all the values of a picklist field salesforce - Salesforce Globe For You

 How to get all the values of a picklist field salesforce  - Salesforce Globe For You 

Here in this example getting the leadSource (picklist) field values of Lead object.

Apex class:

public class PicklistValuesController {

    public List<String> lstLeadStatus {get;set;}

    public PageReference getLeadStatusValues() {

        lstLeadStatus = new List<String>();

        Schema.DescribeFieldResult fieldResult = Lead.LeadSource.getDescribe();

        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

        for (Schema.PicklistEntry a : ple ) {

          lstLeadStatus.add(a.getValue());

        }

        system.debug('List Of Values:'+lstLeadStatus);

        return null;

    }

}

Visualforce Page:LeadStatusValues

<apex:page controller="PicklistValuesController" action="{!getLeadStatusValues}">

<b>The Lead Status Values are:</b><br/>

<apex:repeat value="{!lstLeadStatus}" var="staus">

   {!staus}<br/>

</apex:repeat>

</apex:page>

Output:



Friday, September 25, 2020

Dynamic soql to query records with a variable to define limit in apex salesforce - Salesforce Globe For You

 Dynamic soql to query records with a variable to define limit in apex salesforce  - Salesforce Globe For You 

Solution:Assume iLimit is a variable to set limit.

The query will be like this

[Select id,name from Lead limit :iLimit]; 

Note: keep space after limit and add ':'

Example:

Apex class:LimitVariableController

public class LimitVariableController {

    public static void getLeads(integer iLimit) {

        List<Lead> lstLead = new List<Lead>();

        lstLead = [Select id,name from Lead limit :iLimit];

        system.debug('Leads List:'+lstLead);

    }

}

When we run the following line from anonymous window of developer console,

LimitVariableController.getLeads(2);

the output will be like this.





How to display specific number of elements from a list in aura component - Salesforce Globe For You

 How to display specific number of elements from a list in aura component  - Salesforce Globe For You 

Problem: Need to display specified number of elements from a list instead of displaying all elements.

Solution: There is one attribute 'end' in <aura:iteration> tag which can be used for this purpose.The End attribute species the index(excluding) till which the list should iterate.

The end specifies the number of elements to display:If end is 2,it displays 2 elements and if end is 5 ,it displays 5 elements from the list.

Example:

ListIterator.cmp

<aura:component>

<aura:iteration items="1,2,3,4,5,6,7,8,9" var="num" end="4">

        {!num}<br/>

    </aura:iteration>

</aura:component>

TestApp.app

<aura:application extends="force:slds">

<c:ListIterator/>

</aura:application>

Output:



when end is change to 6,output is



How to get headers information in rest resource class Salesforce - Salesforce Globe For You

 How to get headers information in rest resource class Salesforce  - Salesforce Globe For You 

Solution: There is a property called 'Headers' in RestResource class which returns the headers that are received by the request.

Example:Assume we are passing the header 'Header1' with value '123' in the request.

Apex class:

@restResource(urlMapping='/getAccountInfo/*')

global class HeaderInfoInRestResource {

    @HttpGet

    global static Account getAccount() {

        RestRequest req = RestContext.request;

        RestResponse res = RestContext.response;

        Map<String,String> mapHeader = new Map<String,String>();

        mapHeader = req.Headers;

        system.debug('mapHeader'+mapHeader);

        String header1Value ='';

        if(mapHeader.containsKey('Header1')) {

            header1Value = mapHeader.get('Header1');

            system.debug('header1Value'+header1Value);

        }

        return [Select id,name from Account limit 1];

    }

}

Open workbench https://workbench.developerforce.com/login.php and sign in with your salesforce instance credentials.

Go to Utilities tab and click on 'Rest Explorer'.

Use Get Request,add the header header1 as shown in the image and then execute.

Output:



Thursday, September 24, 2020

Salesforce release cycle information(spring,summer and winter readiness and release dates) - Salesforce Globe For You

 Salesforce release cycle information(spring,summer and winter readiness and release dates)  - Salesforce Globe For You 

Salesforce  delivers multiple innovative features 3 times a year during their seasonal releases: Spring, Summer, and Winter.

In general, Spring Feb-May,Summer June-Sep and Winter October-Jan.

To know the important dates about each release information,go to 

https://status.salesforce.com/products/Salesforce_Services

and then select the region and instance as shown in the image below.



Go to the maintaince tab.



In this tab you will find the information about each release.

Its always better to go through this https://status.salesforce.com/products/Salesforce_Services url atleast once in every 2 months to know the releases information better.

Wednesday, September 9, 2020

How to upload files from aura component salesforce Lightning - Salesforce Globe For You

 How to upload files from aura component salesforce Lightning  - Salesforce Globe For You 

Solution:Use <lightning:fileUpload> component in aura component to upload files.

To associate the uploaded file to salesforce record,assign the recordId of that record to the 'recordId' attribute of <lightning:fileUpload> component.

Example:

fileUpload.cmp

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,

forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global"><aura:attribute name="recordId" type="String" />

 <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

<div class="slds-page-header">

 <div class="slds-align_absolute-center slds-text-title_bold" style="height:5rem">File Upload</div>

 <lightning:fileUpload label="Attach File" name="fileUploader" multiple="true"  recordId="{!v.recordId}" onuploadfinished="{!c.handleUploadFinished}" />

 </div>

</aura:component>

fileUploadController.js

({

doInit : function(component, event, helper) {

 },

handleUploadFinished : function(component, event, helper) {

var recordId = component.get('v.recordId');

var str = "Files uploaded to Notes and Attachment under this record"+recordId;

alert(str);

 }

})

You can drag and drop this component to any record detail page to test.

In this example I dragged this component to Lead record page and uploaded the file.

Output:





Sunday, September 6, 2020

How to lock a salesforce record or records in Apex? - Salesforce Globe For You

How to lock a salesforce record or records in Apex?  - Salesforce Globe For You 

Solution: Use 'FOR Update' keyword after the SOQL query to lock the records.

Example:If you want to lock the records returned by the following query

List<Lead> lstLead = new List<Lead>();

lstLead = [Select id,name from Lead];

then modify the query as shown below

List<Lead> lstLead = new List<Lead>();

lstLead = [Select id,name from Lead For UPDATE];

When records are locked,no other user is allowed to update this records until the lock gets released.The lock automatically gets released after transaction completes.

Note:You cannot use 'Order By' when you are using 'FOR UPDATE' in SOQL query.

How to get size of a map in Apex Salesforce - Salesforce Globe For You

 How to get size of a map in Apex Salesforce  - Salesforce Globe For You 

Solution:Use size() to get size of map.

Example:

Map<String,String> alphabetMap = new Map<string,String>();

alphabetMap.put('A','Apple');

alphabetMap.put('B','Ball');

alphabetMap.put('C','Cat');

system.debug('Map size:'+alphabetMap.size());

Output:


How to loop through map in order to get each element key and value in Apex Salesforce - Salesforce Globe For You

 How to loop through map in order to get each element key and value in Apex Salesforce  - Salesforce Globe For You 

Solution:First get list of keys of a map using Keyset() and then iterate over each key and use map.get(Key) to get value.

Example:

Map<String,String> alphabetMap = new Map<string,String>();

alphabetMap.put('A','Apple');

alphabetMap.put('B','Ball');

alphabetMap.put('C','Cat');

//iterate the map to get each element key and value

for(String alphabet:alphabetMap.keySet()) {

    System.debug('Key:'+alphabet+' Value:'+alphabetMap.get(alphabet));

}

Output:



System.NoAccessException: Apex approval lock/unlock api preference not enabled - Salesforce Globe For You

System.NoAccessException: Apex approval lock/unlock api preference not enabled  - Salesforce Globe For You 

Problem:when you try to use lock/unlock/lock status(isLocked) related methods of Approval class,you get the above error.

Example:run the below piece of code ,it will result in the above error if 'Enable record locking and unlocking in Apex' checkbox is false.

List<Lead> lstLead = new List<Lead>();

lstLead = [Select id,name from Lead For UPDATE];

System.debug('lstLead'+lstLead);

Map<Id,Boolean> recordMap = new Map<Id,Boolean>();

recordMap = Approval.isLocked(lstLead);

Solution:Enable the 'Enable record locking and unlocking in Apex' checkbox by navigating to Setup--> Create -->Process Automation Settings as shown in the image below.



Tuesday, September 1, 2020

Create Salesforce record using aura component Salesforce lightning - Salesforce Globe For You

 Create Salesforce record using aura component Salesforce lightning  - Salesforce Globe For You 

In this example,we will be creating form to create Lead Record.

Apex Class:

public class LeadCreationController {

    @AuraEnabled

    public static void createNewLead(Lead objLead) {

        insert objLead;

        system.debug('objLead:'+objLead);

    }

    @AuraEnabled

    public static Lead initializeLead() {

        Lead objLead = new Lead();

        system.debug('objLead:'+objLead);

        return objLead;

    }

}

LeadCreationForm.cmp

<aura:component controller="LeadCreationController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">

    <aura:attribute name="newLead" type="Lead"/>

    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

    <div aria-labelledby="newLeadForm">

        <fieldset class="slds-box slds-theme--default slds-container--small">

            <legend id="newexpenseform" class="slds-text-heading--small 

                                               slds-p-vertical--medium">

                Create Lead

            </legend>

            <form class="slds-form--stacked">

                <lightning:input aura:id="firstName" label="First Name"

                                 name="FirstName"

                                 value="{!v.newLead.FirstName}"/>

                

                <lightning:input aura:id="lastName" label="Last Name"

                                 name="LastName"

                                 value="{!v.newLead.LastName}"

                                 required="true"/>

                

                <lightning:input aura:id="company" label="Company"

                                 name="Company"

                                 value="{!v.newLead.Company}"

                                 required="true"/>

                <lightning:button label="Create Lead" 

                                  class="slds-m-top--medium"

                                  variant="brand"

                                  onclick="{!c.createLead}"/>

            </form>

        </fieldset>

    </div>

</aura:component>

LeadCreationFormController.js

({

  doInit : function(component, event, helper) {

        

        var action = component.get("c.initializeLead");

        action.setCallback(this, function(response) {

            var state = response.getState();

            if (state === "SUCCESS") {

                console.log('Success');

              component.set("v.newLead", response.getReturnValue());

            }

        });

        $A.enqueueAction(action);

    },

    

    createLead : function(component, event, helper) {

        var action = component.get("c.createNewLead");

        action.setParams({

            objLead:component.get("v.newLead")

        });

        action.setCallback(this, function(response) {

            var state = response.getState();

            if (state === "SUCCESS") {

                console.log('Success');

                alert("Lead Record Created Successfully");

                var action1 = component.get("c.initializeLead");

                action1.setCallback(this, function(response) {

                    var state = response.getState();

                    if (state === "SUCCESS") {

                        console.log('Success');

                        component.set("v.newLead", response.getReturnValue());

                    }

                });

                $A.enqueueAction(action1);

                }

        });

        $A.enqueueAction(action);

    

  }

})

TestApp.app

<aura:application extends="force:slds">

  <c:LeadCreationForm/>

</aura:application>

Output: