Thursday, April 9, 2015

Learn Salesforce Java script page 1


-------------------------------------------------------------------------------------------------------------------------
java script is as simple as apex program, All we need to know is how to call the methods and how it behaves..

there are two ways to call apex actions into Java script

they are 1)Apex Action function
              2)Java script remoting


Now am using first way for making it simple...

Here i explained the below program with simple steps..
-------------------------------------------------------------------------------------------------------------------------



<apex:page controller="Samplevalid" >


    <script type="text/javascript">

    function validate()
    {
        if(document.getElementById('{!$Component.frm.pb.pbs.pbsi1.nam}').value == '')
        {
            alert("First name is mandatory");
        }
        if(document.getElementById('{!$Component.frm.pb.pbs.pbsi2.lname}').value == '')
        {
            alert("Last name is mandatory");
        }
        else
        {
            callSubmt();
            alert("Contact has been inserted");
        }
    }
 
    </script>
<!-- Javascript -->

<apex:form id="frm">

<apex:actionFunction action="{!submt}" name="callSubmt" reRender="pb"/>

    <apex:pageBlock id="pb">
        <apex:pageBlockSection id="pbs">
     
            <apex:pageBlockSectionItem ><apex:outputLabel value="First Name"/></apex:pageBlockSectionItem>
         
            <apex:pageBlockSectionItem id="pbsi1"><apex:inputText value="{!nam}" id="nam"/></apex:pageBlockSectionItem>  
                     
            <apex:pageBlockSectionItem ><apex:outputLabel value="Last Name"/></apex:pageBlockSectionItem>
         
            <apex:pageBlockSectionItem id="pbsi2"><apex:inputText value="{!lname}" id="lname"/></apex:pageBlockSectionItem>    
                                   
        </apex:pageBlockSection>
     
        <apex:pageBlockButtons >
     
            <apex:commandButton value="Insert" onclick="validate();"/>
         
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>

</apex:page>

--------------------------------------------------------------------------------------------------------------------------
Controller
-------------------------------------------------------------------------------------------------------------------------



public class Samplevalid
{
 
 
 
    public Contact con= new Contact();
    public String lname{get;set;}
    public String nam{get;set;}



    public void submt()
    {
       con.FirstName=nam;
       con.LastName=lname;
        insert con;
    }  
}





Inorder get elements of any apex component(apex:form , apex:inputfield ..etc) we need to use
document.getElementById('component ids') .value

Ex:  if(document.getElementById('{!$Component.frm.pbs.pbsi1.nam}').value == '')

Here $Component is a global variable that is we are calling with dollar symbol. for calling any component id we need to use this variable.


<apex:form id="frm">

To get above  component  value we need to use id of the form.

Ex; Component.frm

<apex:pageBlock id="pb">

To get above  component  value we need to use id of the parent(apex:Form) and its id.

Ex; Component.frm.pb
.
.
.
.
.
.
.
  <apex:pageBlockSectionItem id="pbsi1"><apex:inputText value="{!nam}" id="nam"/>

To get above  component  value we need to use id's  of the parents(apex:Form, apex:pageblock ,<apex:pageBlockSection> ) and its id.

Component.frm.pbs.pbsi1.nam


Acording to our program if the value of the above component value  is null below code will display the alert..


    if(document.getElementById('{!$Component.frm.pb.pbs.pbsi1.nam}').value == '')
        {
            alert("First name is mandatory");
        }
        if(document.getElementById('{!$Component.frm.pb.pbs.pbsi2.lname}').value == '')
        {
            alert("Last name is mandatory");
        }

......

Wednesday, April 8, 2015

For Certification maintainence questions click below link

Simple Caluclator coding with Radio Buttons

Sample code:

Visualforce page:

<apex:page controller="Sample">

<apex:form >
 
    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Value 1"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:inputText value="{!val1}"/>
            </apex:pageBlockSectionItem>                        
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Value 2"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:inputText value="{!val2}"/>
            </apex:pageBlockSectionItem>                      
            <apex:pageBlockSectionItem >
                <apex:selectRadio value="{!func}" layout="pageDirection">
                    <apex:selectOption itemValue="add" itemLabel="Add"/>
                    <apex:selectOption itemValue="sub" itemLabel="Subtract"/>
                    <apex:selectOption itemValue="div" itemLabel="Division"/>
                    <apex:selectOption itemValue="mod" itemLabel="Modulo Division"/>
                </apex:selectRadio>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >            
            </apex:pageBlockSectionItem>    
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Result"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:inputText value="{!result}" id="res"/><apex:actionStatus id="sts" startText="Finding..."/>
            </apex:pageBlockSectionItem>                                    
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
            <apex:commandButton value="Find" action="{!find}" reRender="res"  status="sts"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
 
</apex:form>

</apex:page>




public class Sample
{
    public Double val1 {get;set;}
    public Double val2 {get;set;}
    public Double result {get;set;}
    public String func {get;set;}
 
    public Sample()
    {
    }
 
    public void find()
    {
        if(func == 'add')
        {
            result = val1 + val2;
        }
        else if(func == 'sub')
        {
             result = val1 - val2;
        }
        else if(func == 'div')
        {
             result = val1 / val2;
        }
        else
        {
             Integer temp =  math.mod(Integer.valueOf(val1) , Integer.valueOf(val2));
             result = Double.valueOf(temp);
        }
    }

}

Simple Caluclator coding for Beginers

Visual Force Page:

<apex:page controller="Calculator">
<apex:form >
 
    <apex:pageBlock title="Simple caluclator">
        <apex:pageBlockSection columns="1">
            <apex:pageBlockSectionItem >
Please Give First Number<apex:inputText value="{!number1}"/>
 </apex:pageBlockSectionItem>
 <apex:pageBlockSectionItem >
Please Give Second Number<apex:inputText value="{!number2}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Please Provide The Operator <apex:inputText value="{!operator}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="ShowResult" action="{!calculation}" ></apex:commandbutton>
<apex:commandButton value="Refresh" action="{!Refresh}" rendered="{!flag}" ></apex:commandbutton>
 </apex:pageBlockButtons>

 <apex:pageBlockSection rendered="{!flag}">
 <apex:pageblockSectionItem >

 Result <apex:outputText value="{!result}"  >


 </apex:outputText>

 </apex:pageblockSectionItem>
  </apex:pageBlockSection>



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


------------------------------------------------------------------------------------------------------------------------
Controller
-------------------------------------------------------------------------------------------------------------------------


public class Calculator
{


 public boolean flag { get; set; }
public integer number1{get;set;}
public integer number2{get;set;}
public string operator{get;set;}
public double result{get;set;}


public Calculator()
{
flag=false;
}
public void calculation()
{
flag=true;
if(operator.contains('+'))
result=number1+number2;
if(operator.contains('-'))
result=number1-number2;
if(operator.contains('*'))
result=number1*number2;
if(operator.contains('%'))
result=number1/number2;
}
public void Refresh()
{
flag=false;
number1=null;
number2=null;
operator=null;


}

}

Monday, April 6, 2015

Basic Salesforce Interview questions part 15

153. What is the maximum allowed time limit while making a callout to external service in apex?
Ans - maximum of 120 second time limit is enforced while making callout to external service

154. How can you expose an apex class as a REST web service in salesforce?
Ans - An apex class can be exposed as REST web service by using keyword '@RestResource'

155. What are custom controllers?
Ans - Custom controller is an apex class that implements all of the logic for a vf page without leveraging standard controller

156. Can a custom controller class accept arguments?
Ans - No. A custom controller cannot accept any arguments and must use a no argument constructor for outer class.

157. What different type of method can be defined for VF controllers?
Ans - getter methods, setter methods and action methods

158. What are getter methods, setter methods?
Ans - Getter methods retrieve data from controller while setter methods pass data from page to controller.

159.What is Organisation Wide Default in salesforce?
Ans - Organization wide default is used to set default access level for various standard objects and all custom objects. Default access can be set as Public read only, Public read write, Public read write and transfer for different objects. For example if the record access level is set to private then the records will be accessible only to owners, users above in role hierarchy and if it shared using manual sharing.

Basic Salesforce Interview questions part 14

146. What are the different exceptions in apex
Ans - Apex exceptions can be built in or we can also create our own custom exceptions. Exception class is the super class of all the built in exceptions. So as to create a custom exception the class name should end with string 'exception' and it should extend any of the built in exception class or other custom exception class.

147. What different access modifiers can be used for apex classes?
Ans - Apex classes can have either or these three access modifiers 1. Private 2. public 3. global

148. What different access modifiers can be used for class members?
Ans - Class members can have any of these four access modifiers 1. global 2. public 3. protected 4. private

149. What are governers limit in salesforce?
Ans - Governers limit are run time limits that are enforced by salesforce. Governers limits are reset per transaction.

150. What is an apex transaction?
Ans - An apex transaction represents set of operations that are executed as a single synchronous unit.

151. What does heap in apex mean?
Ans - Every apex transaction has its heap. Heap is nothing but the garbage collected at the end of every apex transaction.

152. How many callouts to external service can be made in a single apex transaction?
Ans - A total of 10 callouts are allowed in a single apex transaction

Basic Salesforce Interview questions part 13

153. What is the maximum allowed time limit while making a callout to external service in apex?
Ans - maximum of 120 second time limit is enforced while making callout to external service

154. How can you expose an apex class as a REST web service in salesforce?
Ans - An apex class can be exposed as REST web service by using keyword '@RestResource'

155. What are custom controllers?
Ans - Custom controller is an apex class that implements all of the logic for a vf page without leveraging standard controller

156. Can a custom controller class accept arguments?
Ans - No. A custom controller cannot accept any arguments and must use a no argument constructor for outer class.

157. What different type of method can be defined for VF controllers?
Ans - getter methods, setter methods and action methods

158. What are getter methods, setter methods?
Ans - Getter methods retrieve data from controller while setter methods pass data from page to controller.

159.What is Organisation Wide Default in salesforce?
Ans - Organization wide default is used to set default access level for various standard objects and all custom objects. Default access can be set as Public read only, Public read write, Public read write and transfer for different objects. For example if the record access level is set to private then the records will be accessible only to owners, users above in role hierarchy and if it shared using manual sharing.