Monday 17 November 2014

Searching Component In SFDC

Hi All,

This is a  cool post for me.Here I have created one page where you can search many sfdc component(apex class,vf pages, user,report,approval process, trigger). 

Useful for Whom ?

  • All developers who always try to finish work as son as possible.
  • Don't want click on Next & Prev link to reach at particular component(if more than 100 is there).
  • Want to get all different component in one page.
  • The best thing is with in less time to get component.

We can search these component as below image.if you want to search more than add that component in options list rest will be take care.




Here is the Page:-

<apex:page controller="SearchApexComponent">
    <apex:form >
        
        <apex:pageBlock title="Find Your SFDC Component" id="pblock">
        <apex:pageMessages id="message"></apex:pageMessages>
         <apex:outputLabel value="Choose Search Item">
                <apex:selectList value="{!objectname}" size="1" multiselect="false">
                    <apex:selectOptions value="{!options }" >
                    </apex:selectOptions>
                </apex:selectList>
            </apex:outputLabel>
            <apex:outputLabel value="Enter Name" for="txt"></apex:outputLabel>
            <apex:inputText value="{!inputString}" id="txt"/>
            <apex:commandButton value="Search" action="{!Showclasses}" reRender="pbt,message,pblock"/>
            <apex:pageBlockTable value="{!Classes}" var="cls" id="pbt">
                <apex:column headerValue=" Name">
                <apex:commandLink action="{!Go}" target="_blank">
                    <apex:param name="clsid" value="{!cls['id']}"/>
                     <apex:outputField value="{!cls['name']}"/> 
                     
                </apex:commandLink>
                </apex:column>
                <apex:column headerValue=" Created By">
                     <apex:outputField value="{!cls['CreatedByid']}"/> 
                </apex:column>
                <apex:column headerValue=" Modified By">
                     <apex:outputField value="{!cls['LastModifiedByid']}"/> 
                </apex:column>
                
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
  
</apex:page>

Here is Controller :-

public with sharing class SearchApexComponent {
    public String inputString{get;set;}
    public String objectname{get;set;}
    public List<sObject> Classes{get;set;}
    string classId;
    public List<SelectOption> options{get;set;}
    public SearchApexComponent(){
        Classes = new List<sObject>();
        options = new List<SelectOption>();
        options.add(new selectOption('','--None--'));
        options.add(new selectOption('ApexClass','ApexClass'));
        options.add(new selectOption('ApexPage','VisualForce Page'));
        options.add(new selectOption('ApexTrigger','Trigger'));
        options.add(new selectOption('Approval','Approval Process'));
        options.add(new selectOption('User','User'));
        options.add(new selectOption('Document','Document'));
        options.add(new selectOption('Profile','Profile'));
        options.add(new selectOption('PermissionSet','PermissionSet'));
        options.add(new selectOption('Report','Report'));
        
    }
    
    public PageReference Showclasses(){
        system.debug('printing claaneem---'+Objectname );
        if(Objectname ==null || Objectname == '' || Objectname == '--None--'){
            
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please choose component what you want to search');
            ApexPages.addMessage(myMsg);
            return null;   
        }
        else if(inputString ==null || inputString == ''){
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please Enter Some Input String');
            ApexPages.addMessage(myMsg);
        }
        else{
            String sQuerry  = 'select id,name,CreatedByid,LastModifiedById from '+Objectname +' where name LIKE '+'\'%'+inputString+'%\''+'LIMIT 100';
            System.debug('Print sQuerry  -->'+sQuerry  );
            classes = Database.query(sQuerry );
        }
        
        system.debug('printing Classes list---'+Classes);
        return null;
    }
     
    public PageReference Go(){
        classId = Apexpages.currentpage().getparameters().get('clsid');
        
        PageReference requestPage = new pagereference('https://cs1.salesforce.com/'+classId);
        requestPage.setRedirect(true);
        return requestPage;
    }
}


Wednesday 5 November 2014

Filtering Record Using Long Text Area Field

Hi All,

We might have come across a scenario to filter some record based on a field value which is long text area type.This is post is all about how can we filter records using text area field value.

Here is code:-

String s;
list<Account> listOfAccounts = [SELECT id,name FROM Account WHERE TextAreaFiedl__c like :'%'+s+'%'];

If we write same code then compiler will fire a error like below image.


Below is work around for above error.

String s = 'asish';
list<Account> listOfAccounts;
 listOfAccounts = new list<Account>();
for(Account acc:[SELECT id,name,TextAreaFiedl__c FROM Account where name='test']){
    if(acc.TextAreaFiedl__c.Contains(s)){
       listOfAccounts.add(acc);
    }
}

Saturday 1 November 2014

How would we get recordtype id and recordtype name of an particular Object?

Hi All,

Sometimes it may require to know the recordtypeid and recordtypename of an object in apex class.This is post is all about to make you comfortable with Recordtype methods.I am assuming you have concept of recordtype. Below is few code snippet to make you comfortable with recordtype method.

RecordTypeInfo is a class contains methods for accessing recordtype information for an sObject with associated recordtype.

getRecordTypeInfosByName(); - Returns maps that associate RecordTypeInfo with  record labels.

getRecordTypeInfosById();- Returns maps that associate RecordTypeInfo with  record Id.

getname():- Returns recordtype name.

getRecordTypeId() :- Returns recordtype id.

isAvailable() :-Returns true if this record type is available to the current user, false otherwise

isDefaultRecordTypeMapping() :-Returns true if this is the default record type mapping, false otherwise.

Here Contact is the standard object and Service Contact is the recordtype name.

Schema.DescribeSObjectResult contactDescribe = Schema.SObjectType.Contact;  
        Map<String,Schema.RecordTypeInfo> rtMapByName = contactDescribe.getRecordTypeInfosByName();  
        Schema.RecordTypeInfo rtName = rtMapByName.get('Service Contact');
        system.debug('hiiiii>>>>'+rtName.getRecordTypeId());--- It prints recordtype Id
system.debug('hiiiiihelllooo>>>>'+rtName.getName());--It prints recordtype name.