Monday 10 March 2014

Check Package Installation based on Name Space Prefix..

How can you ensure particular package is installed or not in your org. 

Here is some code snippet which will help you on the same.
 let say the package prefix is invoiceit_crmx.

String sExtensionNameSpace = 'invoiceit_crmx';
    Boolean isInstalled;
    try{
    if(Userinfo.isCurrentUserLicensed(sExtensionNameSpace)){
    isInstalled = true;
    }
    else{
    isInstalled = false;
    }
    }catch(Exception ex){
    isInstalled  = false;
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Info,'Please  install CRM extension package to access these records');
    ApexPages.addMessage(myMsg); 
   
        }
If package is not installed then It will through Type exception. You can handle that exception and show some valid  your own message.

Retrieving Value From SelctionOption List

Hi Guys 
I am going to share about one issue which I have come across.  I was just  comparing some value of selectOption list by iterating that list.

Here is the code:-
list<SelectOption> listOfOptions;
listOfOptions = new list<SelectOption>();
listOfOptions.add(new SelectOption('a','a') );
listOfOptions.add(new SelectOption('1','1') );
listOfOptions.add(new SelectOption('A','A') );
listOfOptions.add(new SelectOption('@','@') );
for(SelectOption option:listOfOptions){
   if(option== 'A'){
        System.debug('hi');
   }
}
COMPILE ERROR: Comparison arguments must be compatible types: System.SelectOption, String

To avoid this error we need to use getValue() method.
list<SelectOption> listOfOptions;
listOfOptions = new list<SelectOption>();
listOfOptions.add(new SelectOption('a','a') );
listOfOptions.add(new SelectOption('1','1') );
listOfOptions.add(new SelectOption('A','A') );
listOfOptions.add(new SelectOption('@','@') );
for(SelectOption option:listOfOptions){
   if(option.getValue()== 'A'){
        System.debug('hi');
   }
}
Now 'Hi' will be printed two times.You can run this code from workbench and developer console. Let me know if some error is there by adding comments. 

Monday 3 March 2014

Restricting User to Double Click on Button

Hey Guys !!!

This post is regarding to avoid one small issue I have come across it.
Let say there is a visual force page where input fields and one button is present. When that button is clicked records are inserted in to the object. If button is clicked twice in same time(double click) then records(duplicate records ) are being created twice. 

How will we avoid that?
How will restrict user to do double click on button?

Here I am sharing some snippet of code.Hope It will help you.

Visualforce Page


<apex:page standardController="Account">
    <apex:form >
        <apex:pageMessages id="messages"></apex:pageMessages>
        <apex:pageBlock title="Avoid Doublle Click" >
            <apex:pageBlockButtons location="both">
                
                <apex:actionStatus id="saveStatus">
                    
                    <apex:facet name="stop">
                        <apex:outputPanel >
                            <apex:commandButton action="{!save}" value="{!$Label.ButtonSavePage}" reRender="messages" status="saveStatus"/>
                            <apex:commandButton action="{!Cancel}" value="Cancel" immediate="true" />
                        </apex:outputPanel>
                    </apex:facet>
                    <apex:facet name="start">
                        <apex:outputPanel >
                            <apex:commandButton value="Saving..." disabled="true" />
                            <apex:commandButton value="Saving..." disabled="true" />
                       </apex:outputPanel>
                         
                    </apex:facet>
                                 
                </apex:actionStatus>
                  
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Account Creating">
                <apex:inputField value="{!Account.Name}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
  
</apex:page>