Tuesday, November 19, 2019

How to Convert Salesforce Classic Notes into Lightning Notes

As now everyone is migrating to Lightning, Salesforce supports lightning Notes(ContentNote) instead of Classic Notes. So while migrating to lightning, we have to convert all classic Notes to lightning Notes. Salesforce provide an appexchange app called magic mover for this purpose.

If you don't want to install this app and would like to perform this conversion, then you can do it with very simple apex script.

If you want to understand How to Convert Salesforce Attachments into Salesforce Files then refer below URL:

Convert Salesforce Attachments into Salesforce Files


I have created a batch class which will convert all Notes related to object (based on query that you pass) to ContentNote and you will receive an email with all details once batch job is completed.

Suppose you want to convert all the notes related to Account to Lightning Notes, then use below script in execute anonymous in developer console.

string qstring='Select id from Account';
SK_ConvertNotesForLightningBatch newjob= new SK_ConvertNotesForLightningBatch(qstring);
database.executeBatch(newjob);

Below is snapshot of csv file which you receive after this batch job completed.

Hope this will help!!

Thursday, November 14, 2019

Pre-populate Field Values on Standard Pages in Lightning

In Salesforce classic, we used to specify the field values in URL parameters (URL hacks) to pre-populate the field values in standard page layouts.

In lightning, URL hack don't work. So if we have to open standard page in lightning with pre-populated field values, then we can use "e.force:createRecord" event to open new record page with default field values.

In this blog, I will be creating a lightning component which will be added to account page layout. This component will display button "Create Quick Contact" to create new contact. When user will click on this button, standard new contact page will open with default field values.

Below is complete code:

Now you add this component on account page layout.

Now when you click on Create Quick Contact, standard page will open for new contact creation with pre-filled field values specified in controller.js.


Here you can see recordtypeId, phone, email and account are pre-populated.

Hope this will help!!




Thursday, November 7, 2019

How to Convert Salesforce Attachments into Salesforce Files

As now everyone is migrating to Lightning, Salesforce supports files instead of attachment. So while migrating to lightning, we have to convert all attachments to files. Salesforce provide an appexchange app called magic mover for this purpose.

If you don't want to install this app and would like to convert attachments to files, then you can do it with very simple apex script.

I have created a batch class which will convert all attachments related to object (based on query that you pass) to files and you will receive an email with all details once batch job is completed.

Suppose you have to convert all attachments of Contact to files, then use below script:
SK_ConvertAttachmentsToFilesBatch newjob= new SK_ConvertAttachmentsToFilesBatch('select id from Contact');
database.executeBatch(newjob, 1);




I will suggest to run this batch job with minimum batch size (recommended size is 1). Suppose if you run this script with more batch size and one parent record have multiple attachments and attachment size is large, then you may face heap size error in apex script. So it is advisable to run this script with batch size as 1.

Hope this will help!!

Saturday, November 2, 2019

Migrate Attachments from one Salesforce to another Salesforce Org

Through this blog, I will be sharing simple apex code through which you can fetch the attachment from another org and save it in your current org as attachments. This process includes 2 API calls as mentioned below:
  • First API call to fetch attachment details like attachment name etc.
  • Second API call to fetch attachment body.  Attachment body will be returned in binary format so get the response body as blob and then use it to insert attachment in your current org.

Below is code snippet which you can use to migrate attachment:


In order to test above script, run below mentioned script in developer console:

string sourceOrgUrlAttachmentId='00P0K00001ZvWpe';
string sourceOrgURL='https://sk02-dev-ed.my.salesforce.com';
string access_Token='0XXXXXXXXXXXXXXXXXX8ADac';
string currOrgParentRecid='00190000004Awot';
string result=SK_AttachmentMigrationHelper.findAttachmentDetails(sourceOrgUrlAttachmentId,sourceOrgURL,access_Token,currOrgParentRecid);
system.debug('***attachment Id after migration:'+result);

Instead of access_token, you can also use sessiond of user from source Org.

Refer below URL to understand how to get access token from salesforce:

ACCESS TOKEN USING OAUTH 2.0 IN SALESFORCE


How to get all attachments related to parent record Id

Specify endpoint URL as mentioned below while performing callout:

string parentRecId='001xxxxxxxxxxx';
EndPointURL= EndPointURL +'/services/data/v45.0/query/?q=select+id,parentid,ContentType,+name+from+attachment+where+parentid=\''+parentRecId+'\'';


How to migrate bulk attachments from one salesforce org to another

Before migrating the attachments, you will be migrating the parent records from source org to target org. So while migrating store the source org record Id in new custom field in target org.

Now write a batch class in target org and take references from above code to process records one by one. Execute the batch class with 1 batch size.

Hope this will help!!

Tuesday, September 24, 2019

Custom Label : Fetch all Custom Label Information through Execute Anonymous Script

CustomLabel is available in Tooling API and can be used to get all CustomLabel Information. With single callout, REST API return 2000 records and then provide "nextRecordsUrl" attribute which contain URI for next set of custom label.

If you want to create class in your org and execute it methods to get custom label information, then refer below URL:

Custom Label : Fetch all Custom Label Information using Tooling API

But if you don't want to deploy new class to production or any org to get custom label information, then you can run below apex script in execute anonymous window in developer console to get custom label information.

Hope this will help!!!

Monday, September 23, 2019

Box API : How to Regenerate Access Token from Refresh Token

Box and Salesforce can be integrated to store files on box and link them with salesforce records. You can also write apex script to extract files or attachments from Salesforce and upload it into box folder.

For basic understanding on how to generate access token from box, refer below URL:
Box and Salesforce Integration

Whenever we perform handshake with box using OAuth, box returns access token along with refresh token. Below is sample JSON response for access token:

{
"access_token": "h91xxxxxxxxxxxxxxxxxxxx8",
"expires_in": 4012,
"restricted_to": [],
"refresh_token": "bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxM",
"token_type": "bearer"
}

here expires_in specifies the duration for which this access token is valid. After this duration, user will recieve 401 error so we need to regenerate the access token using refresh token.

Below is apex code which can be used to regenerate new access token and refresh token. In order to regenerate you need clientid, client secret and refresh token.

Above mentioned code is self explainatory.


Hope this help!!!


Tuesday, September 3, 2019

Custom Label : Fetch all Custom Label Information using Tooling API

Considering the organization which have huge metdadata, its very difficult to fetch custom label information using Metadata API. When I tried using Metadata API in org which has more than 15000 custom label, then I was getting "IO Exception: Exceeded max size limit of 6000000" error.

CustomLabel is available in Tooling API and can be used to get all CustomLabel Information. With single callout, REST API return 2000 records and then provide "nextRecordsUrl" attribute which contain URI for next set of custom label.

I have created below code snippet which can be used to fetch all custom labels in csv file. User will receive email with attachment which contains Custom Label Id, Name and Value.

After saving this apex class, run below script in developer console and you will receive email with csv file with all custom label information.

SK_CustomlabelUtility.fetchAllCustomLabels();

Below is snapshot of csv file:



This utility can be helpful to take backup of custom labels in sandbox or production or find dependency or reference of any metadata component like fields in custom label.

Hope this will help!!

Looking forward for everyone comments and feedback.