Monday, 24 February 2014

Preventing Copy,Paste and Right Click In Your Page

Hi Guys
Everybody must have visited bank site there we can not do right click or copy Account number and paste in another field Retype Account Number. That means we should not give any chance to anybody to see page source. We do this for more security purpose. 
That made me to think and try to design same in visual-force page. Here I have only used JavaScript for this functionality. 
There are some snippet of code.Hopefully It will help you. 

Functionality 

  • Preventing to see the Page Source.
  • Preventing paste Email field value in Re Email field.
Here is the page:-
<apex:page StandardController="Contact">
  <script>
      function DisableRightClick(event){    
        if (event.button==2)
        {
            alert("Right Click is not Allowed");       
        }
      }
     function DisableCtrlKey(e){
        
        var code = (document.all) ? event.keyCode:e.which;
         if (parseInt(code)==17){
            alert("Please re-type your email address");           
        }   
    }
  </script>
  <apex:form >
      <apex:pageBlock title="Create Contact" onmousedown="DisableRightClick(event)">
          <apex:pageBlockButtons >
              <apex:commandButton value="Save" action="{!save}"/>
          </apex:pageBlockButtons>
          <apex:pageBlockSection columns="1">
              <apex:inputField value="{!Contact.Firstname}"/>
              <apex:inputField value="{!Contact.LastName}"/>
              <apex:inputField value="{!Contact.Email}"/>
              <apex:inputtext value="{!Contact.Email}" label="Re-Type Email Id"  onKeyDown="DisableCtrlKey(event)"/>              
              <apex:inputField value="{!Contact.Phone}"/>              
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>

Thursday, 20 February 2014

Test Class in Salesforce

Hi Guys 
This post is only for somebody (who are new to test class in salesforce) who asked me to write a test class for one of my Post 

Unit tests are class methods that verify whether a particular piece of code is working properly. Unit test methods take no arguments, commit no data to the database, send no emails, and are flagged with the testMethod keyword or the isTest annotation in the method definition. Also, test methods must be defined in test classes, that is, classes annotated with isTest.

Here I am sharing some links regarding test class basic functionality. Please go through this.

http://salesforceworld.blogspot.in/2012/03/unit-testing-in-salesforce.html
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm.

Roopal Chouhan ,Here is the example of test class which you had asked me to write for this Controller

Here is the test class. 
/**
 * Testing and Code Coverage.
 */
@isTest
private class TestClass {

    static testMethod void myUnitTest() {
        creatingListOfRecordsController  clsObj = new creatingListOfRecordsController();
        /*Account acct =  new Account();
        acct.Name = 'testAccount';*/
        
        Apexpages.currentpage().getParameters().put('index','1');
      
       clsObj.addRow();
       for(creatingListOfRecordsController.Accountwrapper innnerCls : clsObj.accountwrapperList) {
             innnerCls.account.Name = 'acct';
        }
        
        clsObj.saving();  
        clsObj.removingRow();  
    }
}

Tuesday, 4 February 2014

A Visualforce Page Excatly Same as Contact Edit Page

Hi 
This is a very simple post. I had seen that question  developerforce.com . That's why I thought I will share in my blog. The question was there will be a link called "Copy Mailing Address to Other Address" when clicked that link all Mailing address will be copied to other address same as standard edit page of contact.
This can be done two ways. 
  • By using java script
  • By using Controller extension
In the command link we have to call java script method there we will get all the field value of mailing addresss using documnet.getElementByid() method in javascript and will assign to the other address field. 

Second approach is we will call extension method where will assign all the mailing address field to other address field and will reRender (refresh) the block. It will behave exactly like as standard edit page layout of Contact functionality.
Now the challenge is how will we display that command link inside that pagablock section header.

This is the page and controller 


<apex:page standardController="Contact" extensions="contactEditPageExtension">
    <apex:sectionHeader title="Contact Edit" subtitle="New Contact"/>
    <apex:outputText value="Contacts not associated with accounts are private and cannot be viewed by other users or included in reports."> 
    </apex:outputText><br/><br/>
    <apex:form >
        <apex:pageBlock title="Contact Edit" id="test">
            <apex:pageBlockButtons location="both">
                <apex:commandButton value="Save" action="{!Save}"/>
                <apex:commandButton value="Save & New" />
                <apex:commandButton value="Cancel" action="{!Cancel}"/>
                
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Contact Information">
                <apex:inputField value="{!contact.FirstName}"/> 
                <apex:inputField value="{!contact.LastName}"/>       
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Test">
                   <apex:facet name="header">
                        <apex:pageBlockSectionItem >
                        <apex:outputText value="Address Information" style="float:left;"></apex:outputText>
                        <apex:commandLink value="Copy Malling Address to Other Address" style="float:right;" action="{!test}" reRender="test"/>
                        </apex:pageBlockSectionItem>
                    </apex:facet>
             
                    <apex:inputField value="{!contact.MailingCountry}"/> 
                    <apex:inputField value="{!contact.otherCountry}"/>  
                    <apex:inputField value="{!contact.MailingStreet}"/> 
                    <apex:inputField value="{!contact.otherStreet}"/>   
                    <apex:inputField value="{!contact.MailingState}"/> 
                    <apex:inputField value="{!contact.otherState}"/>    
                  
            </apex:pageBlockSection>
             
        </apex:pageBlock>
    </apex:form>
  </apex:page>


My controller 


public with sharing class contactEditPageExtension {
    public Contact contact{get;set;}
    public contactEditPageExtension(ApexPages.StandardController controller) {
        //this.contact = (Contact )controller.getRecord();
        contact = new Contact();
    }
    
    public void test(){
         contact.otherCountry  =  contact.MailingCountry;
        contact.otherState  =   contact.MailingState;
        contact.otherStreet  =   contact.MailingStreet;
        //return null;
    }


}