Excel INTERCEPT function in Apex Salesforce

Hello awesome Developers!!! This post will save your time if you are going to implement Excel INTERCEPT function in apex salesforce. In my previous post of excel function (Excel LINEST function in Apex Salesforce), we coded for LINEST function of excel.

Before we jump to code, lets have a quick view of INTERCEPT function.

Syntax of Intercept function is as follows in Excel

INTERCEPT(known_y’s, known_x’s)

WHERE Known_y’s  and Known_x’s  both are Required.

We must ensure that Known_y’s and Known_x’s size is same.

The above code snippet is a static method, you can put it any helper/utility class in salesforce.  See below a demo call to this method from developer console.

The output of the above code snippet from developer console will be below

INTERCEPT = 0.0483870967741935483870967741935484

Use it and save your time, in your saved time do some other creative coding 🙂 !!!

Excel LINEST function in Apex Salesforce

As a developer we sometime need to implement excel function in code for meeting client’s requirement. Here I will share code for Excel LINEST function in apex. You can check LINEST function/method of excel here.

See below code method, you can use it in your apex class.

It will save your time if you need to write it from scratch in apex. I am open for your valuable feedback.

Happy Coding 🙂

Getting length of integer part and fractional part in apex of a number type field salesforce

Sometimes we need to play with field (custom field or standard) properties in apex. If we need to find the integer part length in a “Number” type field, then there is no direct method in Describe Field Result (DescibeFieldResult) class. But we can find it using some other methods of DescribeFieldResult class.

Lets suppose we have a custom field of Number type with below properties.

“Test Number Field” (Test_Number_Field__c) Number(14, 2).

Number Options (Integer Part Length)
Number type field options.

So here from the above custom field we want to fetch Length property from number options i.e. 14. But when we use getPrecision() method we will get 16, and when we use getScale() method we will get 2.

Output of the above apex code snippet will be :

Apex Code Output.

So here we get the length of our Integer part in apex.

Conclusion

Integer Part Length = dfr.getPrecision() – dfr.getScale();

Fractional Part Length = dfr.getScale();

Total Length = dfr.getPrecision();

Finally if you need to do anything with the integer part length then grab the code from here and make it fast.

Happy Coding 🙂

Overlapping pseudo code

As a developer, we sometimes need to write code for checking overlapping. We check overlapping for two objects having one start and end point (Overlapping between two appointments, overlapping between two courses, programs etc).
For example lets take a Course as object, So Course has a Start Date and End Date.

Course (StartDate, EndDate).

Now we have given two courses, “courseOne” and “courseTwo”, now we need to check whether they are overlapping to each other or not?

We have For courseOne

  • courseOne.startDate
  • courseOne.endDate

For courseTwo

  • courseTwo.startDate
  • courseTwo.endDate
Analysis of Possible Overlapping

Let’s start some analysis on possible overlapping between courseOne and courseTwo, there are four possible use cases when courseOne and courseTwo can overlap each other.

Overlapping Analysis Image.
Overlapping Analysis

1.) Complete Overlap : When courseOne completely covers the courseTwo i.e. courseOne starts before courseTwo and ends after courseTwo. So in coding we can write this condition as.

2.) Under Overlap : When courseTwo completely covers the courseOne i.e. courseTwo starts before courseOne and ends after courseOne. We can simply say its vice-versa of complete overlap. In coding we can write this as.

3.) End Overlap : When ending of courseOne overlap with starting of courseTwo i.e. Before end of courseOne, courseTwo starts. We can write this check in code as.

4.) Start Overlap : When starting of courseOne is overlapped with ending of courseTwo i.e. courseOne starts before courseTwo ends. In coding we can write this conditions as.

Pseudo Code

Finally you can write a complete method to check overlapping between two object as follows.

This code can be changed as per your requirement, just grab this pseudo code and be a faster/smarter coder.
Happy Coding 🙂

setTabIcon in Service Console is not showing full icon/image.

Recently I came across service console in Salesforce, Where I need to show the tab Icon for a custom visual-force page. There is a standard method setTabIcon() , which we can use to set the tab icon. This method works fine if we have image/icon of exactly 16*16 px. But if our image/icon is larger, than its not shown completely, only some part will be shown.

So the first workaround we will think is to upload a image/icon of 16*16px. For this first we need to resize the all images/icons and then we need to upload them either on salesforce document or somewhere on net where they are publicly accessible. We need to also take care if we are in a minor release of managed package.

Analysis of Problem :

When we use setTabIcon method, it creates the below element in the HTML for showing the tab icon.

What element standard page creates is

Possible Solutions :

1.) If we can play with tab elements using javascript then we can change the span into img tag and that will resolve the issue, but for that we may face DOM exception when accessing parent element from an iframe.

2.) We can do something with setTabStyle(), that I will be using as a workaround here.

Workaround :

So instead of using setTabIcon method we can use setTabStyle method, And using that we can set the image/icon as background accordingly. See the below code snippet of apex class and visual-force page.

Here is the visual-force page code. In document ready method of jQuery (else window.onload if using only javascript) write the below method to set tab icon using setTabStyle.

I hope that will help other developer guys, if they stuck in such kind of problem. Use this workaround if it can help you to save your day.

SOQL Datetime literal in javascript for current Datetime.

Sometimes we need to use AJAX toolkit to do some SOQL/DML on Data from a custom button, which executes javascript. Here in this post we will just get current Datetime and then we will create datetime literal as per SOQL standard to fetch some records as per requirement.

For more information about SOQL date/datetime formats Click Here.

Where to Use :

This can be used when you need to fetch records where some datetime field is less/greater than current time. You can also update it to get records of last 1 hours, 2 hours etc.

Datetime Literal Javascript Methods :

Use With Ajax toolkit Salesforce :

Hope this will help when you are creating SOQL query string using javascript.

Use it wherever you can whether its javascript button, lightening component or anything, to save your time and be a speedy coder.

Currency format using Apex:outputText

Currency format using Apex:outputText is very easy, The advantage of using Apex:outputText for showing a currency is it always respects the currency of the current user.

Below is one example, where we will see how we can format a number into a currency. CurrentFormatController is an apex class.

Visualforce code for our example is kept very simple, It will look like the below

The final output of the above code will be

$799.99

Showing dollar currency symbol as per the current user, If user has different currecny it will show accordingly.

Datetime on Visualforce Page

Whenever we are working with datetime on visualforce page, we may face some issue either with its formatting or its value not coming correctly. So basically the formatting of date depends on the locale of the user, and second thing which is its value depends on the timezone.

In the salesforce database each datetime value is stored in GMT, and when this is shown on the detail page it is adjusted with the current user’s timezone or offset. But sometime we developer create custom visualforce pages to fulfil different different business requirements. Here I come up with the all possible use cases and solution of showing Datetime on visualforce page with their pros and cons.

Datetime on Visualforce Page
  • Datetime Format (Showing DD/MM/YYYY, showing time only, showing year only, showing week day etc).
  • Datetime Value (Either in GMT or in current user timezone).
Method/Approach Taken Is Format Customizable ? Is Current Timezone ?
apex:outputText – direct datetime value. NO NO
apex:outputText – formatted using apex:param. YES NO
apex:outputText – with one space in value(Trick). NO YES
apex:outputText – (Offset is added on page). YES(But in controller) YES
apex:outputText – Showing a String containing formatted Datetime from controller. YES YES
apex:outputText – (Offset is added from controller). YES YES
apex:outputField – (No Customization can be done with format). NO YES

Datetime on visualforce page
Datetime on Visualforce page

Source Code

Visualforce page Code

Apex controller

Conclusion

For Different format see Datetime format Patterns.

So I have shared my finding on the datetime on visualforce page, I hope that this will help developers. Please comment if you need further explanation or have any feedback.

Apex:actionRegion not working with Apex:actionFunction

One of my friend was using apex:inputFile on a Visual force page, and he also needs to reRender some information on the same page. If we need to use both together, we have to use apex:actionRegion in order to isolate apex:inputFile (because inputFile doesn’t support reRender and throws error).

He was also doing the same thing but the state of information was not maintained, See the below psuedo code to get an idea of code

You will think that the above code will work fine, But It won’t!

Issue In the Above Code

Let’s say you are using a list of contact in editable mode where user can add or remove rows and update the information of contact.
User has added one row and filled the information, Now he clicked on some button to add one more row. To add row, function “myFun” (placed in actionRegion 1) is invoked. Here the information for first row of contact will go away on reRender of myInfo panel.

Solution/ Workaround

As of now, after doing some research and hit and trial approach  I have found one workaround or you can say solution. Instead of putting apex:actionFuntion and your part which needs to reRendered put them in a single actionRegion. So the updated code (working) will be as follows.

Reason/Explanation

For now there is no 100% correct explanation behind this behavior. May be actionFunction sends only information of apex:actionRegion in which it is placed to the server. For example if actionFunction is in actionRegion A, then it will send information from actionRegion A to the server. May Be my assumption is wrong. So for now no solid reason, but we have solution. So for now just solve this issue and move faster to meet your deadlines.

Getting “Default New User License” of customer portal in apex code.

Sometime we need to get Default New User License of Customer Portal in the apex code, After doing some google search, I found that there is no direct method or way to do it in the apex.

To get the Default New User License of customer portal in apex code, we need either customer portal name or site name. Below are the steps for Getting “Default New User License” of customer portal in apex code. Here you go :-

Getting Default New User License of Customer Portal in Code

STEP 1 : You need MetadataService.cls Apex class in your salesforce org.(I would suggest keep only required methods of your need only, as this class has huge lines of code).
STEP 2 : Now you need to set Remote Site Setting. Setting up remote site setting is shown in below figures.

Setup Remote Site Setting
Setup Remote Site Setting

Setup Remote Site Setting for VF Page.
Setup Remote Site Setting for VF page.

STEP 3 : After setting up MetaDataService apex class and remote site setting we are ready to read “Default New User License” of customer portal in apex code. Given below is the code snippet for this.