Category Archives: Salesforce

Apex Trigger Context Variables

All triggers define implicit variables that allow developers to access runtime context.
These variables are contained in the System.Trigger class:

Variable Usage
isExecuting Returns true if the current context for the Apex
code is a trigger, not a Visualforce page,
a Web service, or an executeanonymous()
API
call.
isInsert Returns true if this trigger
was fired due to an insert operation, from the Salesforce user
interface, Apex,
or the API.
isUpdate Returns true if this trigger
was fired due to an update operation, from the Salesforce user
interface, Apex,
or the API.
isDelete Returns true if this trigger
was fired due to a delete operation, from the Salesforce user
interface, Apex,
or the API.
isBefore Returns true if this trigger
was fired before any record was saved.
isAfter Returns true if this trigger
was fired after all records were saved.
isUndelete Returns true if this trigger
was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user
interface, Apex,
or the API.)
new Returns a list of the new versions of the sObject records. This
sObject list is only available in insert, update,
and undelete triggers, and the
records can only be modified in before triggers.
newMap A map of IDs to the new versions of the sObject records. This map
is only available in before
update
, after
insert
, after
update
, and after
undelete
triggers.
old Returns a list of the old versions of the sObject records. This
sObject list is only available in update and delete
triggers.
oldMap A map of IDs to the old versions of the sObject records. This map
is only available in update and
delete triggers.
size The total number of records in a trigger invocation, both old and
new.

Get Key Prefix of Salesforce Object

Let’s say we have a custom object “Customer__c”, and we want the Key Prefix of this object.

Using Apex:

String keyPrefix result = Customer__c.SObjectType.getDescribe().getKeyPrefix();
System.Debug('Key Prefix: ' + keyPrefix);

Using Workbench:
Login to Workbench || Info || Standard & Custom Objects || Select one object from the picklist || Expand Attributes

Big Objects in Saleforce

What is Big Object?

  • Big Object means 100s of billions of record on AppCloud.
  • It is a new capability with highly scalable object to store and manage large amount of data on the Salesforce platform.
  • This feature helps to engage directly with customers by preserving all your historical customer event data.
  • Big Objects are built by Salesforce to provide consistent performance whether there is 1 million records, 100 million, or even 10 billion records.

Use Cases:

  • Audit and Tracking: Track and maintain a long-term view of your user’s usage of Salesforce or your customer’s usage of your products for analysis or compliance purposes.
  • 360 view of Customers: Now data models can be extended to contain billing infos/ecommerce transactions/any other info related to customers.
  • Historical Data Archive: Maintain access to historical data for analysis or compliance purposes while optimizing the performance of your core CRM or Force.com applications.

Considerations:

  • It is provided as a pilot to selected premium customers.
  • The Big Objects cannot be created from the UI. We have to take help of the Metadata APIs to create one.
  • Big Objects don’t support triggers however they support object and field permissions.
  • BigObject don’t support standard UI elements (Home Pages, Detail Pages, List Views), But It can be used in Visualforce Page and Lightning components.
  • Data can be populated to the Big Objects via SFDC APIs/Bulk APIs/using Apex(insertImmediate() method).
  • The only SOQL relationship queries available are based on a lookup field from a BigObject to a standard or custom object.
  • Mostly Async SOQL would be used for the querying the Big Objects. Single level child-to-parent relationship queries and aggregate queries are supported.
  • Big Objects don’t support transactions.
  • We can create up to 100 Big Objects per org. The limits for BigObject fields are similar to the limits on custom objects, and depend on your org’s license type.
  • Big Objects don’t appear in the Setup UI until they are deployed.
  • Big Objects don’t appear in Salesforce1.

Generic Loading Component For Visualforce Pages

Visualforce Page Component:

<apex:component>
    <apex:actionStatus onstart="startLoading();" onstop="stopLoading();" id="loadStatus" />
    <style>
        .overlay {
            display: none;
            height: 100%;
            left: 0;
            position: fixed;
            top: 0;
            opacity: 0.3;
            -moz-opacity: 0.3;
            width: 100%;
            -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=30)";
            filter: alpha(opacity=30);
            background: #000;
            -khtml-opacity: 0.3;
            z-index: 1000;
        }

        .loader {
            background: url('/img/loading32.gif') scroll no-repeat 0 0;
            width: 32px;
            height: 32px;
            position: absolute;
            left: 50%;
        }
    </style>

    <div id="load_scrl" class="loadingBox loader" style="display:none"> </div>
    <div class="loadingBox overlay"> </div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">

        function startLoading() {
            $('#load_scrl').css('top', $(document).scrollTop() + 200);
            $('.loadingBox').show();
        }

        function stopLoading() {
            $('.loadingBox').hide();
        }
    </script>
</apex:component>

Now add this component in your Visualforce Page:

<c:LoadingIcon/>

Use the “Status” attribute with the value “loadStatus” on the command button as follows:

<apex:commandButton action="{!save}" value="Save" status="loadStatus" rerender="myForm"/>

Use in Javascript:

startLoading(); //Start Loading
stopLoading() ; //Stop Loading

Get The Week Number of Month From Date in Salesforce

public Integer weekOfMonth(Date todaysDate){
        Integer weekCount = 0;
        Integer startWeekResidue = 0;
        Integer endWeekResidue = 0;
        
        //Calculating startWeekResidue
        Date dt = todaysDate.toStartOfMonth().addDays(-1);
        Date dtFirstWeekend = dt.toStartOfWeek().addDays(6);
        startWeekResidue = dt.daysBetween(dtFirstWeekend);
        
        //Calculating endWeekResidue
        Date dtLastWeekend = todaysDate.toStartOfWeek().addDays(-1);
        endWeekResidue = dtLastWeekend.daysBetween(todaysDate);
        
        //Counting the weeks
        weekCount = (todaysDate.day() - (startWeekResidue + endWeekResidue))/7;
        weekCount += (startWeekResidue > 0 ? 1:0)+(endWeekResidue > 0 ? 1:0);
        System.Debug('Week Number: ' + weekCount);
        return weekCount;
}

Example: Pass Parameter: 13Sept2017 – Output: 3