Play Games

Search This Blog

Wednesday, December 5, 2018

How to convert 15 digit salesforce record Id to 18 digit Salesforce record Id in Apex

Solution: We just need to declare a variable of type Id and then assign the 15 digit number to that variable.

Example:

String s15DigitLeadId = '00Q0o00001OoyzD'; //Assume this is record Id of Lead of 15 digits
Id i18DigitId = s15DigitLeadId;
system.debug('Eight Digit Id:'+i18DigitId);

Output: Eight Digit Id:00Q0o00001OoyzDEAR


Monday, November 5, 2018

How to include lightning component in visual force page

Solution:

Step 1: create lightning component as shown below.

TestCompInPage.cmp

<aura:component >

Lightning Component Successfully loaded into visual force Page

</aura:component>



Step 2: create a lightning app by extending ltng:outApp as shown below

AppInVfPage.app:

<aura:application extends="ltng:outApp">

<aura:dependency resource="c:TestCompInPage"/>

</aura:application>



Step 3: create the visualforce page as shown below.

<apex:page showHeader="false" sidebar="false">

    <apex:includeLightning />

    <div id="lightning" />

    <script>

        $Lightning.use("c:AppInVfPage", function() {

             $Lightning.createComponent("c:TestCompInPage",

          { label : "" },

          "lightning",

          function(cmp) {

          });

        });

    </script>

</apex:page>

Output: 




Friday, August 31, 2018

Error: Compile Error: Comparison arguments must be compatible types: Datetime, String

This error generally arise when you are comparing datetime field value to string.
Example
DateTime dt = system.now();
if(dt != null && dt !='') { // This line gives error as '' is a string
}

Solution : Just remove the condition dt != '' .
DateTime dt = system.now();
if(dt != null) { // This line checks if datetime field is not null.

}

Error: Compile Error: Illegal assignment from Datetime to Date

This error generally arise when you are assigning datetime field value to Date.
Example :
Assume submitted_Date__c is a date field created on Account.

Account acc = new Account();
DateTime dt = system.Now();
acc.submitted_Date__c = dt;// This line gives error as dt is a datetime field assigning to Date field(submitted_Date__c)

Solution:Convert DateTime to Date and then assign it to other date field.

use date() to convert DateTime to date.
Account acc = new Account();
DateTime dt = system.Now();
acc.submitted_Date__c = dt.date();

Thursday, August 23, 2018

How to move to next line with in the cell in Excel

Problem: When we are entering data with in the cell in excel,if we want to display text in multiple lines,then unable to move to next line.

Solution: Use ALT+ Enter to move to next line

How to check the type of Sandbox in salesforce - Salesforce Globe For You

Problem: We are not able to know what type of sandbox are we using whether fullcopy,Partial sandbox or Developer pro etc as we don't have access to production.

Solution: If you want to know 'Sandbox ABC' type,then login in to that sandbox and go to Setup --> Deployment Settings --> Continue.


Under this,you will find 'This Organisation: Sandbox Type'.

Wednesday, May 9, 2018

Dynamic SOSL Example in Apex

Apex Code:
public class DynamicSOSLExample {
    public static List<List<SObject>> searchContact(String searchField) {
        List<List<SObject>> lstObjResults = new List<List<SObject>>();
        String searchQuery ='FIND \'' + searchField+ '\' IN ALL FIELDS RETURNING Contact(FirstName,LastName)';
        lstObjResults = search.query(searchQuery);
        system.debug('List of Contacts:'+lstObjResults);
        return lstObjResults;
    }
}

Output: Run the following line in Execute anonymous window.

DynamicSOSLExample.searchContact('Smith');

Friday, April 20, 2018

How to delete photo of a user in salesforce through code.

Apex Code:
public Class RemovePhotoFromUserRecord {
    public static void removePhoto() {
    //Replace 2nd parameter with the required userId. Right now I am doing it for loggedin user
    ConnectApi.UserProfiles.deletePhoto(null,userInfo.getUserId());
   
    }
}

Output: call RemovePhotoFromUserRecord.removePhoto() from anonymous window of developer console.
You may observe the photo gets deleted.

Friday, March 9, 2018

How to get Unique Id fields of an object in Apex

Solution: We can check if a field is unique field by using isUnique() of Schema.DescribeFieldResult.

Sample Apex Code:
In this example We have a custom field 'Unique__c' field created on lead object which is made unique by clicking unique Id checkbox.

List<String> lstUniqueField = new List<String>();
Map<String, Schema.SObjectField> describeFields = Schema.SObjectType.Lead.fields.getMap();
for(String field : describeFields.keyset()){
Schema.DescribeFieldResult desribeResult = describeFields.get(field).getDescribe();
if(desribeResult.isUnique()) {
lstUniqueField.add(field);
}
}
System.debug('Unique fields of Lead Object'+lstUniqueField );

Output:


How to get external Id fields of an object in Apex

Solution: We can check if a field is externalId field by using isExternalId() of Schema.DescribeFieldResult.

Sample Apex Code:
In this example We have a custom field 'lead_external_id__c' field created on lead object which is made external by clicking external Id checkbox.

List<String> lstExternalField = new List<String>();
Map<String, Schema.SObjectField> describeFields = Schema.SObjectType.Lead.fields.getMap();
for(String field : describeFields.keyset()){
Schema.DescribeFieldResult desribeResult = describeFields.get(field).getDescribe();
if(desribeResult.isExternalId()) {
lstExternalField.add(field);
}
}
System.debug('ExternalId fields of an Lead object'+lstExternalField);

Output:

Sunday, February 18, 2018

How to pass trigger.new or trigger.newMap or trigger.oldMap to future method from trigger in salesforce.

In Salesforce we can use serialize and deserialize methods to pass required data from trigger to future method.

Simple Example below.
Apex Trigger Code:
trigger updatePhone on Account (after update) {
    String triggerNewList = JSON.serialize(Trigger.new);
    String oldMap = JSON.serialize(Trigger.oldMap);
    UpdatePhoneOnContact.updatePhone(triggerNewList,oldMap);
}

ApexClass Code:
public class UpdatePhoneOnContact {
    public static void updatePhone(String triggerNewList,String oldMap) {
        Set <Id> accountIdSet = new Set <Id>();
        Map <Id,String> accountMap = new Map <Id,string> ();
         List<Account> lstAccount = (List<Account>) JSON.deserialize(triggerNewList,List<Account>.class);
         Map<id,Account> accountOldMap = (Map<id,Account>) JSON.deserialize(oldMap,Map<id,Account>.class);
        for(Account acc:lstAccount) {
            if(accountOldMap.get(acc.Id).phone != acc.phone) {
                accountIdSet.add(acc.id);
            }
            if(acc.phone != null) {
                accountMap.put(acc.id,acc.phone);
            }
        }
        List <contact> contactList = new List <Contact>();
        for(Contact con:[select id,accountId,Phone from contact where accountId in:accountIdSet]) {
            if(accountMap != null && accountMap.containsKey(con.accountId)) {
                con.phone = accountMap.get(con.accountId);
                contactList.add(con);
            }
        }
        if(contactList.size() >0) {
            update contactList;
        }
    }
}

Enjoy Coding...

Simple trigger to update phone number in related contacts when phone number changed in its parent account

Apex Trigger Code:
trigger updatePhone on Account (after update) {
    Set <Id> accountIdSet = new Set <Id>();
    Map <Id,String> accountMap = new Map <Id,string> ();
    for(Account acc:trigger.new) {
        if(Trigger.oldMap.get(acc.Id).phone != acc.phone) {
            accountIdSet.add(acc.id);
        }
        if(acc.phone != null) {
            accountMap.put(acc.id,acc.phone);
        }
    }
    List <contact> contactList = new List <Contact>();
    for(Contact con:[select id,accountId,Phone from contact where accountId in:accountIdSet]) {
        if(accountMap != null && accountMap.containsKey(con.accountId)) {
            con.phone = accountMap.get(con.accountId);
            contactList.add(con);
        }
    }
    if(contactList.size() >0) {
        update contactList;
    }

}

Output: Go to any Account and update phone number.The phone number in its related contacts automatically gets updated.

Coding is easy.Just Explore it...

How to prevent password from expiring?

Salesforce provided an option to keep salesforce login password active forever without expiring.

Go to Setup --> Security Controls --> Password Policies 
In 'User Passwords expire in' dropdown we can select 'Never expires' option to prevent password from expiring and click save as shown in the image below.


Friday, February 9, 2018

Apex Code to get previous Monday based on today's date Salesforce

Sample Code:

DateTime previousMondayDate = system.now();
while(!previousMondayDate.format('EEEE').equalsIgnoreCase('Monday')){
previousMondayDate = previousMondayDate.addDays(1);
}
System.debug('Previous Monday Date:'+previousMondayDate);

Output:

Salesforce login URL to login to system directly on single click

If the instance is Sandbox:

https://test.salesforce.com/login.jsp?pw=instancepassword&un=instanceUsername

For example if instance username is test@gmail.com and password is India123 ,then URL to login will be

https://test.salesforce.com/login.jsp?pw=India123&un=test@gmail.com

If the instance is production:
https://login.salesforce.com/login.jsp?pw=instancepassword&un=instanceUsername

For example if instance username is test@gmail.com and password is India123 ,then URL to login will be

https://login.salesforce.com/login.jsp?pw=India123&un=test@gmail.com