Tuesday 21 April 2015

Eclipse force.com IDE shortcuts

  •     Short roots
     
    • Open the file -> Right Click -> Compare With -> Local History -> Select the Revision Time -> it will open a comparison window.
    •     Use F12 to quickly activate the editor.
    •     Use CTRL+H to search the files.
    •     Use CTRL+L to navigate to the desired line number. This is extremely useful when your code contains thousands of number of lines.
    •     Use CTRL+D to delete the entire line.
    •     Use CTRL+SHIFT+R, to open the file from package explorer quickly. Would be better if you use ‘*’ in your search strings.
    •     Use CTRL+F6 to switch to the next editor.
    •     Use CTRL+SPACE, for the code context assistance. This is the most common and I think most of eclipse users are aware of this shortcut.
    •     Use CTRL+W to close the current window/editor.
    •     Use CTRL+SHIFT+L to get the context menu help for all the available keyword shortcuts.
    •     Use ALT+F7 to open the sub tab in the editor like Metadata and Source.
    •     Use ALT+/ to show the correct variable name (type some letters and use this key).
    •     Use CTRL+K to find the next occurrence of the selected text. You can select a text or any word and press CTRL+K to find the next occurrence of the same text in that file.

Salesforce record Id 15 digit 18 digit conversion

Visual force page
<apex:page controller="idConvertor">
    <apex:form >
        <apex:pageBlock >
        <apex:pageMessages id="showmsg"></apex:pageMessages>
            <apex:pageBlockButtons >
                <apex:commandButton value="Convert" action="{!convert}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection >
            <apex:inputText value="{!inputId}" label="Input Id:"/>
            <apex:outPutText value="{!outputId}" label="Output Id:"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Apex class
public with sharing class idConvertor {
    public String inputId{get;set;}
    public String outputId{get;set;}

    public PageReference convert(){
        outputId = convertId(inputId);
        return null;
    }

    String convertId(String inputId){
        string suffix = '';
        integer flags;
        try{
            for (integer i = 0; i < 3; i++) {
                flags = 0;
                for (integer j = 0; j < 5; j++) {
                    string c = inputId.substring(i * 5 + j,i * 5 + j + 1);
                    if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
                        flags = flags + (1 << j);
                    }
                }
                if (flags <= 25) {
                    suffix += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);
                }else{
                    suffix += '012345'.substring(flags - 26, flags-25);
                }
            }
        }
        catch(Exception exc){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Valid 15 digit Id'));
        }
        String outputId = inputId+suffix;
        return outputId;
    }
}

Actionstatus visualforce disable screen

Here is a visual force page and controller for the same :)
Check it :)
You would like :)

Visual force page 
In case of a custom button , it would be nice if can idle a page until complete action for a click .

<apex:page controller="actionStatusImage" tabStyle="Account">
    <apex:outputpanel >
        <apex:actionstatus id="actStatusId">
            <apex:facet name="start">
                <div class="waitingSearchDiv" id="el_loading" style="background-color: #DCD6D6;
                       height: 100%;opacity:0.65;width:100%;">
                    <div class="waitingHolder" style="top: 74.2px; width: 91px;">
                        <img class="waitingImage" src="/img/loading.gif" title="Please Wait..." />
                        <span class="waitingDescription">Saving...</span>
                    </div>
                </div>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputpanel>
    <apex:form id="formId">
    <apex:pageBlock id="pgBlckId" title="New Account">
        
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save" reRender="pgBlckId" status="actStatusId"/>
        </apex:pageBlockButtons>
        
        <apex:pageBlockSection id="pgBlckSecId" title="Account Information" collapsible="false">
            <apex:inputField value="{!account.name}"/>

        </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Apex class
public class actionStatusImage {
    public Account account{get;set;}
    public actionStatusImage(){
        account = new Account();
    }
    public Pagereference save(){
        insert account;
       
        return null;
    }
}

Import a CSV file and insert Accounts from CSV

Awsome !!! Especially if we can insert set of records into our instance from an excternal csv file using visualforce page something like dataloader n all :)
Just try :)
Here is code for visualfoce page and apex class for the same :)
Try :)
Visualforce page :
<apex:page controller="importDataFromCSVController">
    <apex:form >
        <apex:pagemessages />
        <apex:pageBlock >
            <apex:pageBlockSection columns="4">
                  <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>
                  <apex:commandButton value="Import Account" action="{!importCSVFile}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock >
           <apex:pageblocktable value="{!accList}" var="acc">
              <apex:column value="{!acc.name}" />
              <apex:column value="{!acc.AccountNumber}" />
              <apex:column value="{!acc.Type}" />
              <apex:column value="{!acc.Accountsource}" />
              <apex:column value="{!acc.Industry }" />
        </apex:pageblocktable>
     </apex:pageBlock>
   </apex:form>
</apex:page>
Apex class :
public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public List<account> acclist{get;set;}
  public importDataFromCSVController(){
    csvFileLines = new String[]{};
    acclist = New List<Account>();
  }
 
  public void importCSVFile(){
       try{
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n');
           
           for(Integer i=1;i<csvFileLines.size();i++){
               Account accObj = new Account() ;
               string[] csvRecordData = csvFileLines[i].split(',');
               accObj.name = csvRecordData[0] ;           
               accObj.accountnumber = csvRecordData[1];
               accObj.Type = csvRecordData[2];
               accObj.AccountSource = csvRecordData[3]; 
               accObj.Industry = csvRecordData[4];                                                                           
               acclist.add(accObj); 
           }
        insert acclist;
        }
        catch (Exception e)
        {
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importing data Please make sure input csv file is correct');
            ApexPages.addMessage(errorMessage);
        }
  }
}

Thursday 16 April 2015

Trigger on Notes and Attachments



If we need to write a trigger on ay object in salesforce , salesforce itself gives as a cool UI to create the same? Ever you tried to do the same for Notes and Attachments in salesforce ? No... Nowhere can see such a UI. Amazing ! We need to create trigger under the Notes and attachments. Then there is a solution... Read through...

Method 1 : Create trigger from Developer console.

Click New > Apex Trigger
Select SObject as "Attachment" form dropdown available.

Method 2 : Using Force.com IDE or ANT.

Sample code :
trigger SetTitleToAttachment on Attachment (after insert) {
String Title;
Id pId;
for(Attachment att: Trigger.new){
Title=att.Name;
pId=att.ParentId;
}
List<Case> c=[select Id , Title__c from Case where Id=:pId];
List<Case> caseRecordToUpdate = new List<Case>();
if(c != Null){
for(integer i=0 ; i<c.size(); i++){
c[i].Title__c=Title;
caseRecordToUpdate.add(c[i]);
}
}
if( caseRecordToUpdate.size() > 0 ){
update caseRecordToUpdate;
}
}

Need to login to other salesforce Instance from your own Instance ?


Need to login to other salesforce Instance from your own Instance ?
  
    Here is a simple solution. Read and implement

Decide service provider and Identity provider
 
Service Provider  :  Salesforce Instance (A's developer org).
Identity Provider  :  Salesforce Instance (B's developer org).

Identity provider
Step 1 : Create and register a domain in Identity provider organization,  (Domain Management --> B's Domain)
Step 2 : Enabled Identity provider. (Security Controls --> Identity Provider)
             Created a dummy certificate (self Signed) and set it as use on communication with service provider.
             Saved identity provider settings.
             Downloaded the certificate and saved in a drive.( Need to upload in service provider)
Service Provider
Step 3 : Enable single sign on in service provider (Security Controls > Single Sign-On Settings)
            Upload certificate down loaded from Identity provider.
            Put "Assertion contains the Federation ID from the User object" as "SAML Identity Type" since need to connect IP login name with Federation ID in SP user.
            Put "Identity is in the NameIdentifier element of the Subject statement" SAML Identity Location since need to connect IP login name with Federation ID in SP user.
           Save and note "Salesforce Login URL".

Identity provider

Step 4 : Define Connected App for service provider (Create > Apps > Connected Apps Section )
            Give basic information like App name contact email etc.
            Enable "Entity ID".
            ACS URL – Use the Salesforce Login URL from Service Provider
            Save and note "IdP-Initiated Login URL".
            Add which profiles should be able to access this app.
Service Provider
Step 5 :  Edit Single sign on settings and paste  "IdP-Initiated Login URL" from Step 4 to "Identity Provider Login URL".
User Set up
Step 6 : Copy one of Username from Identity Provider instance to “Federation Id” field of related user in Service Provider.

Identity provider User Interface
Step 7 : Created a custom link "Login To A's Instance" on home page in B's instance .
Working

Login to Identity provider (B). Move to "Home" tab and Click "Login To A's Instance" in narrow column.
Result
             Redirects to A's instance without separate login.