Monday, 27 October 2014

Analytic Snapshots in Salesforce

An Analytic Snapshot helps us to create report on historical data. 

Analytic Snapshots comprises of three things:
  • A source Report of type Tabular or Summary
  • A Custom Object with fields matching the columns in the source Report
  • An Analytic Snapshot definition, which maps fields and schedules the snapshot runs 
For reference, kindly check the below link

https://help.salesforce.com/apex/HTViewHelpDoc?id=data_about_analytic_snap.htm&language=en_US

Steps to set Analytic Snapshot:

1. Go to Setup --> Administration Setup --> Data Management --> Analytic Snapshot.


2. Click "Analytic Snapshot" button.

3. Select the Running User, Source Report and Target Object.

4. Click "Save & Edit Field Mappings" button. 


5. Map the fields and click "Save" button.


6. Click "Edit" button in Schedule Analytic Snapshot related list.


7. Set the frequency and preferred start time and click "Save" button.


Email notification:


After clicking the link in email:


An analytic snapshot will fail during a scheduled run if:
  • The source report includes more than 100 fields
  • The source report was changed from summary to tabular
  • The selected grouping level for a summary source report is no longer valid
  • The running user does not have access to the source report
  • The running user does not have the “Run Reports” permission
  • The target object has more than 100 custom fields
  • The target object contains validation rules
  • The target object is included in a workflow
  • The target object is a detail object in a master-detail relationship
  • The target object runs an Apex trigger when new records are created on it
  • The running user does not have the “Create” permission on the target object. Note that if the target object's status is In Development, the running user must have the “Customize Applications” permission.

How to get deleted records from recylebin and restore them to database

We need to use ALL ROWS keyword in soql query.
ALL ROWS keywords need to be used to query all records in an organization, including deleted records( records in your organization's Recycle Bin) and archived activities. 

You cannot use the ALL ROWS keywords with the FOR UPDATE keywords. 


list<Account> listOfAccounts = [SELECT id,Name FROM Account WHERE isDeleted = true ALL ROWS];

undelete listOfAccounts ;


isDeleted:- It is a standard field of account used to differentiate whether account is in Recycle Bin or not. 

undelete:- Its a DML Operation.


FOR UPDATE:Keywords used in SOQL to lock an sobject records and preventing from being updated. 
P.S.:- ORDER BY and FOR UPDATE keywords in any SOQL can not be used together.

How to permanently delete record from data base.

As we all know if we delete records from our org. It will be stored in recycle bin for 15 days. We can go to recycle bin and delete manually. 
Suppose the requirement is when we delete records those records should be deleted from recycle bin.
For this requirement we have to write a trigger on object. 

trigger todelteRecordPermanent on Account (after delete) {
    list<Id> Ids = new list<Id>();
    for(Account  account: Trigger.old){
        Ids.add(account.id);  
    }
    Database.emptyRecycleBin(ids);
}

How to use required attribute in picklist as like salesforce standard?

There is an attribute called Required which can be used in almost all visual-force component.If we use that attribute then red color mark will be appended with the field and if we don't enter any value ,try to save,then it shows one error(You must enter some value) just below to that field. 

For example:-
<apex:inputField value="{!account.Industry}" id="field1" required="true"/> 

save imageHere Industry is a standard pick-list, if we write required = true, red mark is coming,however if we are making pick-list in controller and displaying using <apex:selectlist> and <apex:selectoptions> then red won't be displayed. 
<apex:selectList label="Picklist" multiselect="false" size="1" required = "true">
               <apex:selectOptions value="{!options }">
                  </apex:selectOptions>

 </apex:selectList>

options = new list<SelectOption>();
        options.add(new SelectOption('-None-','-None-'));
        options.add(new SelectOption('A','A'));
        options.add(new SelectOption('b','b'));
        options.add(new SelectOption('c','c'));
        options.add(new SelectOption('d','d'));


It will display like below image

save image 
How can we display like standard pick-list?
Below is some code for this.
<apex:pageBlockSection id="someArea">
          <apex:pageBlockSectionItem >
               <apex:outputLabel value="picklistLabel"/>
                 <apex:outputPanel layout="block" styleClass="requiredInput">
                     <apex:outputpanel layout="block" styleClass="requiredBlock"/>
                     <apex:selectList label="Picklist" multiselect="false" size="1" >
                            <apex:selectOptions value="{!options }"/>
                   </apex:selectList>
                </apex:outputPanel>
                </apex:pageBlockSectionItem>

</apex:pageBlockSection>
save image

How to pass lookup field id to controller?

Lets take an example. Suppose we want pass Accountid(look up field from contact to account) from visual force page to controller. If we choose one account from look up then automatically account industry will be populated.

Here is the page:-

<apex:page controller="passingLookupIdController">
  <script>
    function doSomething(test) {
      
        alert('hi-->'+test);
        PassingParameter1(test);
        // lkfield.value now has the selected ID value
    }
    </script>
 <apex:form >
  <apex:actionFunction name="PassingParameter1" action="{!PassingParameter}" reRender="field1">
  <apex:param value="" name="recordId"/>
  </apex:actionFunction>

   <apex:pageBlock >
          <apex:pageBlockSection id="someArea"> 

               <apex:inputField value="{!con.AccountId}" id="field">
                  <apex:actionSupport event="onchange" reRender="field"oncomplete="doSomething('{!con.AccountId}');"/>
              </apex:inputField>
               <apex:inputField value="{!account.Industry}" id="field1" />
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>


Here is controller:-

public with sharing class passingLookupIdController {
    public Contact con{get;set;}
     public Account account{get;set;}
    public passingLookupIdController (){
        con =  new Contact();       
    }
    public PageReference PassingParameter(){
        Id recordId = ApexPages.currentPage().getParameters().get('recordId');
        System.debug('Print--->'+recordId );
        account = [SELECT ID,Industry FROM Account WHERE id=:recordId ]; 
        return null; 
    }
}

How to get help text in apex class ??

We generally use help text while creating custom field in salesforce . Sometimes we need it in apex controller while development. This post is regarding to get help text of particular field and display in vf page. 

Here is some code snippet to get help text in apex class.

Schema.DescribeFieldResult dfr = Schema.sObjectType.Account.fields.TestField__c;
String helptext = dfr.getSObjectField().getDescribe().getInlineHelpText();

In the visualforce page we can display help text.

{!$ObjectType.Account.fields.TestField__c.inlineHelpText}

How to add validation through trigger in salesforce

This post is regarding to add validation and to display error message through trigger. 
Suppose we want to give validation is no account should be created having name as 'asish'
We can do this by validation rule easily,in some case we want through trigger. Keep in mind trigger should be ours last choice.We should use standard functionality as much as possible.

Here is trigger for above validation:-

trigger ValidateAccount on Account (before insert,before update) {
    for(Account account:Trigger.new){
        if(account.Name == 'asish'){
            account.Name.addError('You can not create account having name as Asish');
        }
    }
}


Here is the output showing error message while creating account.

save image