Showing posts with label Apex. Show all posts
Showing posts with label Apex. Show all posts

Tuesday, December 26, 2023

Export all Custom Metadata Records in CSV file using Apex

Using apex, we can export all custom metadata records in CSV files. This is sometime needed if we want to review all custom metadata records after sandbox refresh or to check the dependency of any field or attribute in all custom metadata records.


Below is apex class which can be used for this and once you execute its method from developer console, you will receive email with csv that contain all custom metadata records.

Open developer console and open execute anonymous window. Execute below script:

SK_CustomMetadataUtility.exportAllCMRecordsInCSV();


Below is snapshot of csv file:


Note:

  • This script will specify the custom metadata field api name and its value seperated by "&SK&" delimiter.
  • All fields and its values are seperated by comma.
  • You can modify this method to fetch all custom metadata related to installed packages by specifying a parameter for namespace.


Hope this will help!!

 

Thursday, August 3, 2023

Automatically Remove Special Characters from Filename while Uploading Files in Salesforce

 As we all know that we can perform URL hack to download all files related to parent record from salesforce. We have observed that sometime downloaded file name is changed to contentversion recordId by salesforce instead of actual file name. 

It is my observation that if file name contains special characters, then when we perform download All, file name is changed to "068xxxxxxxxxxxxxx.pdf".

I have added 2 files to account record for reference:



Now I can generate download URL if I have contentversionId of these 2 files. In my case the download URL will be:

https://xxxx/sfc/servlet.shepherd/version/download/0680K00000kLSdbQAG/0680K00000kLSfwQAG

where xxxx is you org domain URL.

You can use below script to generate download all files URL if you know the parent record Id.


If I open this URL, I will zip file which will contain both files and when I will extract it, it will appear as shown below:


As my observation, if any special character is present in file name then salesforce changes the file name. If "-" character is present, then salesforce won't change file name.

For this kind of scenarios, I recommend to have script in place which will remove all special characters from file name whenever file is being uploaded. You can write trigger on contentVersion object to remove special characters from file name. Below is code snippet which can help:



Hope this will help!!!!



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

Thursday, June 6, 2019

Fire Platform Events from Batch Apex using Database.RaisesPlatformEvents Interface

With Summer'19 release (API version 44 or later), now it is possible to fire platform events from batch apex. So whenever any error or exception occurs, you can fire platform events which can be handled by different subscriber.

Batch class needs to implement "Database.RaisesPlatformEvents" interface in order to fire platform event.

global class SK_AccountProcessBatch implements Database.Batchable<sObject>,Database.RaisesPlatformEvents{
   //batch logic
}

To understand more about platform events please refer below links

Platform Events : Way to Deliver Custom Notifications within Salesforce or to external Application

Here I will be writing simple batch class which process account records and if any errors occurs during update, then all account record Ids will be published using platform event.

I have created platform event with below mentioned fields:


Below is batch apex code which will fire platform event and I wrote a trigger which will subscribe to platform event.

I have used Database.Stateful interface to store the record ids which are getting failed in each batch execution. In finish method, I am firing platform event with all failed records Ids.

Below is logs generated by trigger which will get executed whenever platform event is fired.


In order to see debug logs for platform events subscription, add a trace flag entry for the Automated Process entity in Setup. The debug logs aren’t available in the Developer Console’s Log tab.

Navigate to SetUp --> Debug Logs --> New
  • For Traced Entity Type, select Automated Process.
  • Select the time period to collect logs and the debug level.
  • Click Save.
I have also created a lightning component which will subscribe to platform events and will display the event message as shown below in snapshot:

Please refer below link to understand how to use platform events in lightning components:

Handling Platform Events in Lightning Components


Hope this will help!!!


Monday, April 22, 2019

Platform Events : Way to Deliver Custom Notifications within Salesforce or to external Applications

Salesforce provides Platform events which can be used create custom notification which can be used within Salesforce app or can be send to external applications. These notification are secure and scalable.

Platform events are part of Salesforce’s enterprise messaging platform. It utilizes publish and subscribe flow. One or many subscribe to same event and carry out different actions.

In lightning events, we define event and then create attributes through which information can shared between lightning components. Similar to this, you create platform event through point and click and then create custom fields which will be used to publish information by using platform events and subscriber can utilize this information.

Platform Events v/s Streaming API

  • In Streaming API (using Push Topics), subscriber receive notification based on changes or updates to records.
  • You can not edit or delete platform event records. You can only create or insert platform events.
  • Visualforce and Lightning component apps can subscribe to platform events using CometD similar to push topics.
  • You can not view platform event in Salesforce user interface and cannot query it through SOQL.
  • You can handle or subscribe platform events through triggers by using after insert.
  • Only "AfterInsert" event is available for platform event objects.
  • Platform events can be published by Process builder, Process Flow, apex, REST API by inserting an sObject.
  • Platform events persist for 24 hours only.
Permission for platform events is controlled through profile or permission sets.

Platform events can be used to publish information and has no dependency on existing salesforce records.

In order to create platform events, navigate to Setup --> Platform Events --> New Platform Event.

I have created a platform event "Demo Event" and going to use apex in order to publish event and subscribe this event through triggers.

When you create a platform event, the system appends the __e suffix to create the API name of the event.

How to fire/publish platform event
  • You have to create record in order to fire platform event.
To publish event messages, you create an instance of the event and pass it to the EventBus.publish method. Use below code snippet to publish event:

// Create an instance of the Demo event 
Demo_Event__e demoEvent = new Demo_Event__e(
           Event_Info__c='Demo event is fired using Apex', 
           Is_Event_Valid__c=true, 
           Event_Publisher__c='Apex Code');
// Call method to publish events
Database.SaveResult sr = EventBus.publish(demoEvent);
// Inspect publishing result 
if (sr.isSuccess()) {
    System.debug('Successfully published event.');
} else {
    for(Database.Error err : sr.getErrors()) {
        System.debug('Error returned: ' +
                     err.getStatusCode() +
                     ' - ' +
                     err.getMessage());
    }
}
  • You can create record using process builder or process flow.
  • You can create event using REST API. Below is details of HTTP Request:
Endpoint- /services/data/v45.0/sobjects/Demo_Event__e/
body
{
   "Event_Info__c" : "Demo event is fired using REST API CALL",
   "Is_Event_Valid__c" : true,
   "Event_Publisher__c" : "REST API CALL"
}
Method - POST
HTTP Response:

{   
   "id" : "e00xx0000000ccf",
   "success" : true,
   "errors" : [ ],
   "warnings" : [ ] 
}

How to Subscribe Platform Events using Apex Triggers

Whenever a record is created for platform event, after insert trigger gets fired. You can use below code to subscribe for platform event and perform logic as per your need:

trigger DemoEventTrigger  on Demo_Event__e (after insert) {
List<Task> taskList = new List<Task>();
for (Demo_Event__e event: Trigger.New) {
if (event.Is_Event_Valid__c == true) {
//perform logic based on event information
}
   }
}

Below is snapshot explaining Platform Events.
Hope this will help!!!




Friday, August 24, 2018

Way to get list of Scheduled reports and Jobs from Salesforce

This is very common ask from business admins or end users to get list of schedule reports or schedule jobs (batch jobs, scheduled jobs etc) from Salesforce.

There is no standard view provided by Salesforce to view list of scheduled reports or reports. Users have to navigate to Monitor section under SetUp.


This section display list of jobs either schedule or completed jobs but does not provide complete list of jobs.

By using apex, you get the list of schedule reports or jobs in csv format via email. 

Please find below the apex code for that:


Now if you have to get list of scheduled reports in your org, then just execute below script in developer console:

  • For getting list of scheduled reports:         SK_ScheduleJobUtility.findScheduledJobDetails('Report Run');

  • For getting list of scheduled batch jobs:         SK_ScheduleJobUtility.findScheduledJobDetails('Batch Job');

  • For getting list of scheduled apex jobs:        SK_ScheduleJobUtility.findScheduledJobDetails('Scheduled Apex');


Important Use case

Sometime reports are scheduled by user which becomes inactive, then all system admin start getting email saying:

"The user account that runs the report (the running user) is inactive."

This utility will send email along with user details which will help in identifying all inactive user for whom reports are scheduled as running user.


Hope this will help!!

Looking forward for everyone's comments and feedback.

Thursday, August 9, 2018

How to get User Session Id in Future Method or Batch Class

Sometime it is required to perform Callout in future method and Batch apex class. If we use UserInfo.getsessionId() method inside the future method or batch apex, then it will return null.

Below are different option to get sessionId:

Option 1:

We can either pass sessionId as parameter to future method or to batch apex class.

Option 2:

Create a VF page and use {!$Api.Session_ID} in body.  In apex, get VF page content which will contain session Id.

Below is code snippet explaining the Option 2 by using future method. Same can be used in Batch Apex.

Run below code snippet in execute anonymous to check the output in debug logs:

SessionIdUtility.checkIfUserSessionIdExistInFutureCall();



Hope this help!!!

Thursday, July 26, 2018

How to Schedule Apex class to Run Instantly or Run After Some Duration from Developer Console

We create apex scheduler job which can be scheduled to run at specific time. If we want to run job instantly to either check the functionality or want to force start scheduled job then below code sample will help you.

Suppose you have scheduled apex class:

global class SK_JobsSchedulerHelper implements Schedulable {
global SK_JobsSchedulerHelper(){

}
       global void execute(SchedulableContext sc) {
//your business logic
}
}

Now if you want to run it instantly from developer console, then use below script:

SR_JobsSchedulerHelper sch = new SR_JobsSchedulerHelper();
sch.execute(null);

Now if you want to run this job after 5 minutes, then you can use below script:

SR_JobsSchedulerHelper sch = new SR_JobsSchedulerHelper();
Datetime dt = Datetime.now().addMinutes(5);  
String schCronExp = dt.format('s m H d M \'?\' yyyy');
Id schedId = System.Schedule(' scheduling this job at'+System.now().format(),schCronExp,sch);

Hope this help!!


Sunday, July 8, 2018

Create Custom Metadata Types Using Metadata API

As we know, custom metadata is also considered as metadata due to which it helps to migrate predefined settings between different Organizations. If we use custom setting, then we need to upload custom records separately after migrating custom settings.

By using, Metadata API we can create apex script to create custom metadata.

You can either download Metadata API WSDL from Salesforce and generate apex class by clicking on Generate from WSDL button.


Or you can download the MetadataService class from below link:

MetadataService.cls

Below is custom metadata created for explaining the apex code:



Below is code snippet to create custom metadata record.


Execute this static method from execute anonymous and check if records created or not.

 MetadataAPICMUtility.createCustomMetadata();

Below is snapshot of record created


You can use above code sample to create records in custom metadata. You can maintain .csv file with all custom metadata records and parse it with apex and then create record in custom metadata.

In order to understand when to use custom setting or custom metadata type and their implementation tricks, please refer below URL:

Custom Metadata Types and Custom Settings Implementation Tricks


Hope this will Help!!!

Wednesday, July 4, 2018

Create Update Custom Label by Using Metadata API

We usually update custom labels from UI. After sandbox refresh, we update all custom labels so that these don't point to production URLs or values.

By using Metadata API, we can write automated apex script through which we can update all custom labels.

You can either download Metadata API WSDL from Salesforce and generate apex class by clicking on Generate from WSDL button.


Or you can download the MetadataService class from below link:

MetadataService.cls

I have created 2 separate static methods, one for creating new custom label and another for updating existing custom label. Below is class code:


Now by running below code in execute anonymous in developer console, you can create custom label:

MetadataAPIUtility.createCustomLabel('SFDC_Blog_URL','My test label from metadata api','en_US','http://www.sfdcstuff.com/',false);



You have to specify custom label values as a parameters in static method.

In order to update custom label, execute below code:

MetadataAPIUtility.updateCustomLabel('SFDC_Blog_URL','My test label from metadata api','en_US','https://www.sfdcstuff.com/search/label/Lightning',false);



Hope this will help!!




Friday, June 1, 2018

Alternatives to OnClick Javascript buttons : Way to replace OnClick Javascript buttons across all objects with Single VF page

We always prefer onclick javascript button to perform different operations like updating fields on same records or on other records with single click by user. In order to do this, we either use ajax toolkit to either perform logic or to call apex method.

We can also perform these operation even by redirecting user to VF page on click of button and call apex method on VF page load (by specifying action attribute on <apex:page>) and redirecting User back to record details page.

Developers usually not use this approach as they do not want to create new VF page every time for these kind of requirements.

After the launch of lightning, onclick javascript buttons are not recommended and everyone is finding alternative for onclick javascript buttons functionality but sometime these alternative results in more than 1 click for end user.

Through this blog, I just to share one approach through which we can utilize single VF for all custom buttons. We just need to pass recordId and apex class name which contains your logic for operation in URL. This approach also involves single click from user and user will get notified about success and failure of operation (in onclick javascript we user alert to notify).

Different steps involved for this approach:
  • Create a VF page and controller. Create wrapper in this controller to store redirect url (to redirect user after performing operation), success and failure message which you want to display to user.
  • Create an Interface with method name executeLogic (return type will be wrapper of VF controller).
  • Create a new apex class which implements interface and will contain logic in executeLogic method.
  • Create a custom button and specify URL. pass recordid and apex class name where you have written your logic.

Sample Requirement:

I need to create a custom button on Account. When user click on that, I need to check if billing country and billing postalcode in not blank, then change the type field to 'Prospect'.

Traditional Approach:

Create a onclick Javascript button and call apex method from that to perform operation. Dispaly success/error message using alerts.

New Approach:

Below are code related to new approach:

Now create custom button on Account as mentioned in below image

URL will be like:

/apex/OnClickJSUtilityVF?rid={!Account.Id}&cname=AccountUtility

here "AccountUtility" is apex class name which contain logic.

If we use this approach then, going forward we just need to create apex class with method and implement the interface.

If we consider the both approaches, the amount of code that we need to write for any new custom button functionality will remain same.

Demo snapshots:

When I click on Change to Prospect button with billing country and billing postal code as blank, getting below message:

When clicking on Change to Prospect button after specifying values for billing country and postal code, getting below message:

These success and error message are specified in apex method in apex class. So you can specify different message based on different requirements.

Also you can specify the redirect URL in apex method where user will navigate after performing the requested action.

You can style your VF page as per your org need and all users will have same user experience.

Hope this will help!!!

Looking forward for everyone comments and suggestions.

Monday, April 2, 2018

Sorting Wrapper List in ASC or DESC order based on Wrapper Property

We get these kind of scenarios in which we have List of wrapper available and want to sort it either in ASC or DESC order based on any member variable wrapper class.

Suppose we have wrapper class as mentioned below:

public class SK_sortUtilityTestWrapper {
public string category{get;set;}
        public decimal count{get;set;}
    public SK_sortUtilityTestWrapper(string ss,decimal sk){
             this.category=ss;
             this.count=sk;
    }
}

Now imagine if we have list<SK_sortUtilityTestWrapper>  and want to sort list based on count value then we can do that by using below code.
I have added a static method which can be called from developer console to see the output for reference.



From developer console execute below script:

SK_WrapperSortUtility.testWrapperListSorting();

You will see below output in debug logs:


 Hope this will help!!!!


Wednesday, August 16, 2017

Creating Apex Rest Services and Testing it with POSTMAN

In this post, I am going to explain how to validate Apex Rest Service created in SFDC using POSTMAN (google chrome plugin).

First we will create a very basic Apex Rest Service.


After creating this class, now we can access this API by using below endpoint URL:

https://xxxx.salesforce.com/services/apexrest/RestAPIDemo
where xxxx is your domain URL or say instance URL like na15, cs30 etc.

Here we have created Rest API which accepts GET & POST request.Whenever we will perform GET request, API will return Account records based on search string passed as parameter in endpoint URL.

Now I will explain how to test this API Service by using POSTMAN.

In order to invoke Apex REST API, we need to have access token. So first we will use Oauth 2.0 to get access token from SFDC in order to perform API calls.

Steps to get access token from SFDC:

  • Create a connected App in SFDC.
  • Perform POST request from POSTMAN to below mentioned endPoint URL:

https://login.salesforce.com/services/oauth2/token?grant_type=password&client_id=CLIENTID&client_secret=CLIENTSECRET&
username=SFDCUSERNAME&password=SFDCPASSWORD_SECURITY_TOKEN

Where 
       CLIENTID = consumer key from created connected app
       CLIENTSECRET= consumer secret from created connected app
       SFDCUSERNAME = SFDC username
       SFDCPASSWORD_SECURITY_TOKEN = password + security_token

While performing POST request, specify header in which key will be "Content-Type" and value will be "application/x-www-form-urlencoded"



You will recieve JSON response in which access_token will be specified. Copy and save it.

Note:
If you have enabled my domain then you can use domain URL as base URL in endpoint URL. If you are not using domain, then in JSON response you will get instance_URL which you can use base URL whenever you call APEX REST API's.


How to perform GET request from POSTMAN to fetch data from SFDC
  • First create Endpoint URL(GET request).
          https:// BASE_URL/services/apexrest/RestAPIDemo?searchString=test
          where 
          BASE_URL is instance URL returned in JSON while requesting Access token.
          RestAPIDemo is REST Resource we specify while creating class.
          searchString : parameter passed to REST API which will return matched accounts
  • Specify Below headers
          [{"key":"Content-Type","value":"application/json"}]
          [{"key":"Authorization","value":"Authorization: Bearer Access_token"}]
  • Perform Get Request from POSTMAN


How to perform POST request from POSTMAN to create new record

  • Create a JSON through which you send account information.
       {
  "accName":"SKtest By POSTMAN",
   "aType":"New Customer",
   "aIndustry":"IT"
        }   
  • Specify endpoint URL
       https://xxxx.salesforce.com/services/apexrest/RestAPIDemo
       where xxxx is domain of your org
  • Specify headers
          [{"key":"Content-Type","value":"application/json"}]
          [{"key":"Authorization","value":"Authorization: Bearer Access_token"}]
  • Send POST request through POSTMAN.

Note:

If your POST method contains parameter, then you have to pass JSON in different ways. For example POST method is:

@HttpPost   
    global static String createNewAccount(accountWrapper accDetails) {
        string returnString='';
        try{
            Account acc=new Account();
            acc.Name= accDetails.accName;
            acc.Type = accDetails.aType;
            acc.Industry =accDetails.aIndustry;
            insert acc;
            returnString = 'Account created successfully with record Id:'+acc.id; 
        }catch(exception ex){
            returnString = 'Request got failed: Error details-'+ex.getmessage();
        }
         return returnString;
    }

In this scenario, JSON which needs to sent should be in below format:

{
"accDetails":{
"accName":"SKtest By POSTMAN2",
"aType":"New Customer",
"aIndustry":"IT"
}
}

Hope this will help!!!!

More Blogs>>: 
USING DATABASE.UPSERT WITH EXTERNAL ID   
DYNAMIC APEX IN SALESFORCE   
SOQL INJECTION IN SOQL   
CUSTOM METADATA AND CUSTOM SETTINGS IMPLEMENTATION TRICKS   
SMART TABLE USING ANGULARJS IN VISUALFORCE PAGE   
REST API TUTORIAL FOR SALESFORCE   
VISUALFORCE COMPONENT FOR RECORD STATUS BAR   
DYNAMICALLY CREATING AND DESTROYING LIGHTNING COMPONENTS    
RAISING AND HANDLING CUSTOM EVENTS IN sALESFORCE lIGHTNING    
WHY TO USE DESIGN RESOURCE AND HOW TO ADD DYNAMIC OPTION TO DATASOURCE    
PASSING INNER WRAPPER CLASS TO LIGHTNING COMPONENT    
LIGHTNING COMPONENT FOR RECORDTYPE SELECTION FOR ANY SOBJECT    
FETCHING FILE FROM EXTERNAL/PUBLIC URL AND STORING IT IN SALESFORCE   

Friday, July 28, 2017

Language Translation in VisualForce Page

Salesforce provide functionality through which you can create single VF page and that can be translated in different languages based on language or locale preference of current logged in user.

You need to upload the translation of all custom fields which you are going to use in VF page and can utilize custom labels to display warning, error information on VF page.

In order to render the VF page in particular language, use Language attribute on <apex:page> tag. You can also bind this values with controller variable so that you can render VF page in different language.

<apex:page controller="SK_LocalizationTestController" language="{!selectedLang}">

I have created a very simple VF page to illustrate this functionality by displaying account information. I have dropdown on VF page through which user will select language and VF page will re render to display page in selected language.





Note:
  • If you are displaying page message using <apex:pageMessages>, then use custom label on apex class to display message. Upload all translation for custom label in salesforce.
  • For custom fields also you need to upload language translation. Please refer Translation Workbench under Set Up.
  • The language attribute does accept ISO country codes plus an optional locale like en,en_US,de,de_DE etc.
  • For displaying header or pageblock section title, use custom labels.

More Blogs>>: 
USING DATABASE.UPSERT WITH EXTERNAL ID   
DYNAMIC APEX IN SALESFORCE   
SOQL INJECTION IN SOQL   
CUSTOM METADATA AND CUSTOM SETTINGS IMPLEMENTATION TRICKS   
SMART TABLE USING ANGULARJS IN VISUALFORCE PAGE   
REST API TUTORIAL FOR SALESFORCE   
VISUALFORCE COMPONENT FOR RECORD STATUS BAR   
DYNAMICALLY CREATING AND DESTROYING LIGHTNING COMPONENTS    
RAISING AND HANDLING CUSTOM EVENTS IN sALESFORCE lIGHTNING    
WHY TO USE DESIGN RESOURCE AND HOW TO ADD DYNAMIC OPTION TO DATASOURCE    
PASSING INNER WRAPPER CLASS TO LIGHTNING COMPONENT    
LIGHTNING COMPONENT FOR RECORDTYPE SELECTION FOR ANY SOBJECT    
FETCHING FILE FROM EXTERNAL/PUBLIC URL AND STORING IT IN SALESFORCE   

Wednesday, June 28, 2017

How to find Salesforce API version of your Org in Apex

Sometimes it is required to find out current API version of your Org (Sandbox/Dev Org/Production) in order to perform some operations like making HTTP request to SFDC URI which contains api version.

As of now, I was not able to find any method in apex through which I can get my org current api version So I have created utility class which uses Tooling API to get all version related to org and returning latest/current api version like 39.0,40.0 etc.

Below is complete code for this.


You can run below scripts in developer console to get current API version after saving above class in your org.

Decimal currentAPIVersion = UtilityClassForSFDC.findAPIVersionOfOrg();
system.debug('***************currentAPIVersion:'+currentAPIVersion);

After running you may get "Unauthorized endpoint url". Just add your SFDC base URL in remote site setting to avoid this exception.

Hope this will help!!!
Looking forward for your comments and suggestions..


More Blogs>>: 
USING DATABASE.UPSERT WITH EXTERNAL ID  
DYNAMIC APEX IN SALESFORCE  
SOQL INJECTION IN SOQL  
CUSTOM METADATA AND CUSTOM SETTINGS IMPLEMENTATION TRICKS  
SMART TABLE USING ANGULARJS IN VISUALFORCE PAGE  
REST API TUTORIAL FOR SALESFORCE  
VISUALFORCE COMPONENT FOR RECORD STATUS BAR  
FETCHING FILE FROM EXTERNAL/PUBLIC URL AND STORING IT IN SALESFORCE  

Thursday, April 20, 2017

Using Database.upsert with external ID field

External Id plays very important role if you want to update records without knowing the record Ids or want to relate the child record with parent record without knowing the parent record Id.

As a best practice, you should always make External Id unique. If you are performing upsert with External Id, then following situations will occur:

  1. If no record is found in table with provided External Id, then it will create record in table.
  2. If 1 record is found in table with provided External Id, then it will update record in table.
  3. If more than 1 records is found in table with provided External Id, then system will throw an error.

I am going to cover 2 different aspect of using external Id in apex.

  • Updating a record with External Id

Create a External Id field on Account as Account_Unique_Number__c and mark it as External Id and unique while creating it.Now we will create a new record using upsert.

Execute below command in developer console

List<Account> acclist=new list<Account>();
acc.name='Demo test1';
acc.Account_Unique_Number__c='00001';
acclist.add(acc);
Schema.SObjectField ftoken = Account.Fields.Account_Unique_Number__c;
Database.UpsertResult[] srList = Database.upsert(acclist,ftoken,false);
for (Database.UpsertResult sr : srList) {
    if (sr.isSuccess()) {
        // Operation was successful
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : sr.getErrors()) {
            System.debug('error has occurred.' + err.getStatusCode() + ': ' + err.getMessage());                    
            System.debug('fields that affected this error: ' + err.getFields());
            
        }
    }
}

As there is no record in Account with Account_Unique_Number__c as 00001, system will create a new record.

Now again we will run same script in developer console and will specify some more field values:

List<Account> acclist=new list<Account>();
Account acc=new Account();
acc.name='Demo test1';
acc.Account_Unique_Number__c='00001';
acc.type='Other';
acc.Industry='Banking';
acclist.add(acc);
Schema.SObjectField ftoken = Account.Fields.Account_Unique_Number__c;
Database.UpsertResult[] srList = Database.upsert(acclist,ftoken,false);
for (Database.UpsertResult sr : srList) {
    if (sr.isSuccess()) {
        // Operation was successful
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : sr.getErrors()) {
            System.debug('error has occurred.' + err.getStatusCode() + ': ' + err.getMessage());                    
            System.debug('fields that affected this error: ' + err.getFields());
            
        }
    }

Now you will see that system will update the record as it was able to find a Account record with Account_Unique_Number__c as 00001

  • Relating a child record with parent record by using parent record Id

In order to understand this, we will create contact record and will relate to account using Account_Unique_Number__c. Execute below code in developer console:

List<Contact> conlist=new list<Contact>();
Contact con=new Contact();
con.lastname='Kumar';
con.Firstname='Kumar';
con.email='sunil02kumar@gmail.com';
Account acc=new Account(Account_Unique_Number__c='00001');
con.Account=acc;
conlist.add(con);
Database.UpsertResult[] srList = Database.upsert(conlist,false);
for (Database.UpsertResult sr : srList) {
    if (sr.isSuccess()) {
        // Operation was successful
    }
    else {
        // Operation failed, so get all errors                
        for(Database.Error err : sr.getErrors()) {
            System.debug('error has occurred.' + err.getStatusCode() + ': ' + err.getMessage());                    
            System.debug('fields that affected this error: ' + err.getFields());
        }
    }
}

This will create a new contact for Account which have Account_Unique_Number__c as 00001.

In above code snippet, you can see that in order to relate contact with account, we are not specifying the account 15 or 18 digit record id. We are just specifying the external Id of account and system will maintain the relationship.

If you refer custom object as parent object then refer it with __r. For example in above scenario, if i have to relate contact with custom object say Parent_Obj__c, then I will use below code:

Parent_Obj__c  obj = new Parent_Obj__c(Unique_Number__c='00001');
con.Parent_Obj__r = obj;

Why it is recommended to mark External Id as unique?

Imagine you are creating a contact and specified External Id of parent. Suppose there are 2 records in account table with same value, then system will not able to identify with whom it needs to relate the contact and will throw error saying more than 1 match found.

Same is applicable when you update the record with External Id.



More Blogs>>: 
DYNAMIC APEX IN SALESFORCE
SOQL INJECTION IN SOQL
CUSTOM METADATA AND CUSTOM SETTINGS IMPLEMENTATION TRICKS
SMART TABLE USING ANGULARJS IN VISUALFORCE PAGE
REST API TUTORIAL FOR SALESFORCE
VISUALFORCE COMPONENT FOR RECORD STATUS BAR
FETCHING FILE FROM EXTERNAL/PUBLIC URL AND STORING IT IN SALESFORCE

Tuesday, March 28, 2017

Different ways of testing HTTP callout in apex

As we all know that test methods do not support HTTP callout, so all test method performing callout will fail.


In order to avoid the test class failure, we mainly use Test.IsRunningTest method in apex class. By using this method, we make sure that particular block of code performing HTTP callout should not run when called by test class methods.

if(!Test.isRunningTest()){
// apex code for HTTP callout
}
but this approach will reduce your code coverage. As per salesforce, you need to have atleast 75% coverage for all your apex code.

We will now go through different options through which we can test HTTP callout and increase our code coverage.

  • Using static resource
You can store the response of your HTTP callout in text file and upload it static resource. Now you can use built in apex class StaticResourceCalloutMock or MultiStaticResourceCalloutMock to build mock response and get response from static resource.

First create an instance of StaticResourceCalloutMock:

StaticResourceCalloutMock mockCallout = new StaticResourceCalloutMock();
mockCallout.setStaticResource('StaticResourceForCallout');
mockCallout.setStatusCode(200);
mockCallout.setHeader('Content-Type', 'application/json');

Now set mock callout mode in test method by using below method:

Test.setMock(HttpCalloutMock.class, mockCallout );

After this call method which perform callout. As we have set mock test callout, apex will not perform callout and will return response from static resource.

Note: MultiStaticResourceCalloutMock helps you test different HTTP callout with different endpoint URL. you can create instance of this class and specify callout response in different static resource for different endpoints and then set this as mock callout in test method.

  • Generating mock response in test by implementing the HttpCalloutMock Interface 
Create a apex class which implements HttpCalloutMock interface. In this interface, you can specify response which will be returned if test method perform callout.

@isTest
global class HTTPMockCallout implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest req) {
        // specify the response here
        // return response.
    }

Note:
       1. Class should be public or global which implements HttpCalloutMock
       2. You can use @IsTest on this class as this will be used only through test class. In this way it                   will not count against organization code size limit.

Now you can set mock response in test method before calling method which perform HTTP callout.

Test.setMock(HttpCalloutMock.class, new HTTPMockCallout());

Below is sample code to provide code coverage to "CalloutUtility" apex class using both approaches mentioned above


Hope this will help!!!!


More Blogs>>: 
DYNAMIC APEX IN SALESFORCE
FETCHING FILE FROM EXTERNAL/PUBLIC URL AND STORING IT IN SALESFORCE
SOQL INJECTION IN SOQL
CUSTOM METADATA AND CUSTOM SETTINGS IMPLEMENTATION TRICKS
SMART TABLE USING ANGULARJS IN VISUALFORCE PAGE
VISUALFORCE COMPONENT FOR RECORD STATUS BAR

Friday, March 10, 2017

Generating random number between two numbers and finding string value from list of values randomly

As we know in apex, math.random() function gives random number greater than or equal to zero and less than 1. This function can be used to generate random number in apex.

I have created a apex class which can be used to find a random between 2 numbers and this can be extended later to find random string from list of strings values or to select random picklist value for picklist field while creating test data in test class.

Below is apex class code:


In order to find random picklist value, use apex describe to store all picklist values in List and then pass that list to "findRandomStringFromList" method in RandomUtility apex class.


Hope this will help!!!


More Blogs>>: 
DYNAMIC APEX IN SALESFORCE
FETCHING FILE FROM EXTERNAL/PUBLIC URL AND STORING IN SALESFORCE
SOQL INJECTION IN SOQL
CUSTOM METADATA AND CUSTOM SETTINGS IMPLEMENTATION TRICKS
SMART TABLE USING ANGULARJS IN VISUALFORCE PAGE
VISUALFORCE COMPONENT FOR RECORD STATUS BAR

Saturday, February 25, 2017

Fetching file from external/public URL and storing it into Salesforce

If you have file URL for file stored outside the salesforce, then you can fetch file information from external URL by performing a call out to external system and store the file in salesforce.

For Demo purpose, I have uploaded a pdf file in Google drive and shared it with link with people. Now I will fetch this file and will store it as attachment in salesforce under account record.

File URL-  https://drive.google.com/file/d/0ByXILxflqQ2jWGpNVmI1WW9uYTQ/view?usp=sharing


Below is apex class which will help us to perform this activity:



Now you can run below code in developer console to test this:

//you can specify any record Id where you want to store file as attachment
String RecordId='0019000000ld4kN'; 
String fileContentType='pdf';
String extFileURL='https://drive.google.com/file/d/0ByXILxflqQ2jWGpNVmI1WW9uYTQ/view?usp=sharing';
blob fileBlob=FileDownLoadUtility.fetchFileFromExternalUrl(extFileURL);
Id attachmentId = FileDownLoadUtility.createAttachment(fileBlob, RecordId , fileContentType);
system.debug('*****attachmentId:'+attachmentId);

Note: 

  • If you are trying to fetch file from external source which is authenticated, then pass authorization parameters in HTTP Request headers
  • You should specify the file content type before creating attachment in order to properly view file.
  • You can add additional parameter as fileName in "createAttachment" method, if you you want specify the attachment name while creating attachment.

While trying above code you may get below error:

For this you need to add external URL in Remote Site Settings. For this example, I have added "https://drive.google.com/" in remote site settings.




More Blogs>>: BOX AND SALESFORCE INTEGRATION    
INTEGRATING BOX FILE PICKER WITH SALESFORCE
REST API TUTORIAL FOR SALESFORCE