Friday, March 10, 2017

Locking Statements FOR UPDATE

Apex allows you to lock sObject records while they’re being updated in order to prevent race conditions and other thread safety problems. While an sObject record is locked, no other client or user is allowed to make updates either through code or the Salesforce user interface.

Account [] accounts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE];

NOTE:- 
1. While the records are locked by a client, the locking client can modify their field values in the database in the same transaction. Other clients have to wait until the transaction completes and the records are no longer locked before being able to update the same records. Other clients can still query the same records while they’re locked.
2. If you attempt to lock a record currently locked by another client, you will get a QueryException. Similarly, if you attempt to update a record currently locked by another client, you will get a DmlException.
3. If a client attempts to modify a locked record, the update operation might succeed if the lock gets released within a short amount of time after the update call was made. In this case, it is possible that the updates will


Implement Enter Key to Execute Method in salesforce for search

How to Call Apex method on click of Enter button.
Solution :-
We can use the "keyCode==13" with actionfuction to call the apex method on Enter Key event like below

Apex Class:
public with sharing class EnterSearchController {

    public string strKey {get;set;}
    public List<Account> lstAccount {get;set;}
    public EnterSearchController()
    {
        lstAccount = new List<Account>();
    }

    public pageReference cancel()
    {
        return new pageReference('/001/o');
    }
   
    public void search()
    {
        if( strKey != null && strKey !=''  )
        {
            String wildcard = '%'+strKey+'%';
            lstAccount = [select id,name from account where name like :wildcard  ] ;
        }
       
    }

}
Visualforce Page:

<apex:page controller="EnterSearchController" >
    <apex:form >
        <apex:pageBlock >
        <apex:actionFunction action="{!search}" name="actionFunction" />
       
            <apex:pageBlockSection >
                <apex:inputText value="{!strKey}" onkeydown="if(event.keyCode==13){this.blur();actionFunction();}" />
               
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!cancel}" value="cancel" />              
                <apex:commandButton action="{!search}" value="Search" />              
           
            </apex:pageBlockButtons>
           
            <apex:pageBlockSection columns="1" >
                <apex:pageBlockTable value="{!lstAccount }" var="acc">
                    <apex:column value="{!acc.name}"/>
                   
                </apex:pageBlockTable>
            </apex:pageBlockSection>
           
        </apex:pageBlock>
    </apex:form>
</apex:page>

Calculate Overall Code Coverage in salesforce

We can use Tooling API  to calculate the coverage individually for an apex class/ apex trigger or overall coverage.

1) How many lines are covered for a specific class or trigger:

SELECT NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverage WHERE ApexClassOrTriggerId =

2) Which lines are covered for a specific class or trigger:

SELECT Coverage FROM ApexCodeCoverage WHERE ApexClassOrTriggerId =

3) The current org-wide coverage:

SELECT PercentCovered FROM ApexOrgWideCoverage

To test these queries you may use Workbench (workbench.developerforce.com) as follows: 1) Go to Utilities | REST Explorer 2) Select GET 3) Enter the following URL:

/services/data/v37.0/tooling/query/?q=YOUR_QUERY

where YOUR_QUERY should be the query to execute

4) Click "Show Raw Response".


If you are still not confident enough, then you can do a production validation (click on validate only button in the tool which you are using to deploy) and it will give you errors/warnings if there is no coverage for classes.

Tuesday, February 14, 2017

How to avoid double clicking of apex:commandButton?

Below  code can be used to avoid double clicking of apex:commandButton.

<apex:pageBlockButtons location="bottom">
<apex:actionStatus id="saveStatus">
<apex:facet name="stop">
<apex:commandButton value="Next Step" action="{!newCase}"
status="saveStatus" reRender="thefrm" />
</apex:facet>
<apex:facet name="start">
<apex:commandButton value="Saving" disabled="true" />
</apex:facet>
</apex:actionStatus>

</apex:pageBlockButtons>

What field type can be used in the WHERE clause to improve SOQL query performance in Salesforce?

The following fields are indexed by default:

1. Primary keys (Id, Name and Owner fields).
2. Foreign keys (Lookup or Master-Detail relationship fields).
3. Custom fields which are marked as External ID or Unique.
4. Audit dates (such as SystemModStamp).