Labels

Monday, April 30, 2012

code coverage 100% in Apex class

code coverage 100% in Apex class

write an apex class
public class CompanyClass
{
        public class EmpClass
        {
        public Integer iEmpno;
        public String iEmpName;
        public Double iEmpSal;
        public Integer iEmpDeptno;
        
 //parameter constractor
            public EmpClass(Integer iEmpno,String iEmpName,Double iEmpSal,Integer iEmpDeptno)
            {
             this.iEmpno=iEmpno;
             this.iEmpName=iEmpName;
             this.iEmpSal=iEmpSal;
             this.iEmpDeptno=iEmpDeptno;
            }
            
 //default constractor
            public EmpClass()
            {
             iEmpno=1;
             iEmpName='varu';
             iEmpSal=100;
             iEmpDeptno=10;
            }
 //properties of EmpClass
        public Integer Empno
        {
        get{ return iEmpno; }
        set{ iEmpno=value; }
        }
        public String EmpName
        {
        get{ return iEmpName; }
        set{ iEmpName=value; }
        }
        public Double EmpSal
        {
        get{ return iEmpSal; }
        set{ iEmpSal=value; }
        }
        public Integer  EmpDeptno
        {
        get{ return iEmpDeptno; }
        set{ iEmpDeptno=value; }
        }
        }
//Emp list object        
    public List<EmpClass> emps=new List<EmpClass>();  
//create Employees      
    public void createEmp()
    {
    emps.add(new EmpClass(1,'varun',1000,10));
    emps.add(new EmpClass(2,'santhosh',2000,20));
    emps.add(new EmpClass(3,'manoj',3000,30));
    emps.add(new EmpClass(4,'mahesh',4000,30));
    }   
//search Emp    
    public EmpClass searchEmp(String empName,List<EmpClass> empList)
    {
    for(EmpClass e:empList)
    {
    if(e.iEmpName==empName)
    return e;
    }
    return null;
    }   
//EmpClass  
        
}write an apex test class

@isTest
public class CompanyClassTest
{
@isTest static void empDefaultConst()
{
CompanyClass.EmpClass obj=new CompanyClass.EmpClass();
System.assertEquals(1,obj.Empno);
System.assertEquals('varu',obj.EmpName);
System.assertEquals(100,obj.EmpSal);
System.assertEquals(10,obj.EmpDeptno);
}
@isTest static void empParaConst()
{
CompanyClass.EmpClass obj=new CompanyClass.EmpClass(50,'arya',3000,80);
System.assertEquals(50,obj.Empno);
System.assertEquals('arya',obj.EmpName);
System.assertEquals(3000,obj.EmpSal);
System.assertEquals(80,obj.EmpDeptno);
}
@isTest static void empCreateTest()
{
CompanyClass obj=new CompanyClass();
obj.createEmp();
Integer TotEmps=obj.Emps.size();
System.assertEquals(4,TotEmps);
}
@isTest static void empSearchTest()
{
CompanyClass obj=new CompanyClass();
obj.createEmp();
CompanyClass.EmpClass empObj=obj.searchEmp('varun',obj.emps);
System.assertEquals(1,empObj.Empno);
System.assertEquals('varun',empObj.EmpName);
System.assertEquals(1000,empObj.EmpSal);
System.assertEquals(10,empObj.EmpDeptno);
CompanyClass.EmpClass empObj1=obj.searchEmp('manoj',obj.emps);
System.assertEquals(3,empObj1.Empno);
System.assertEquals('manoj',empObj1.EmpName);
System.assertEquals(3000,empObj1.EmpSal);
System.assertEquals(30,empObj1.EmpDeptno);
CompanyClass.EmpClass empObj2=obj.searchEmp('santhosh',obj.emps);
System.assertEquals(2,empObj2.Empno);
System.assertEquals('santhosh',empObj2.EmpName);
System.assertEquals(2000,empObj2.EmpSal);
System.assertEquals(20,empObj2.EmpDeptno);

}
@isTest static void empSearchTest1()
{
CompanyClass obj=new CompanyClass();
obj.createEmp();
CompanyClass.EmpClass empObj3=obj.searchEmp('mahesh',obj.emps);
System.assert(empObj3!=null);
System.assertEquals(4,empObj3.Empno);
System.assertEquals('mahesh',empObj3.EmpName);
System.assertEquals(4000,empObj3.EmpSal);
System.assertEquals(30,empObj3.EmpDeptno);
}
@isTest static void empListTest()
{
CompanyClass obj=new CompanyClass();
System.assert(obj.emps!=null);
}

@isTest static void empProTest()
{
CompanyClass.EmpClass obj=new CompanyClass.EmpClass();
obj.Empno=22;
obj.EmpName='fff';
obj.EmpSal=222;
obj.EmpDeptno=33;
System.assertEquals(22,obj.Empno);
System.assertEquals('fff',obj.EmpName);
System.assertEquals(222,obj.EmpSal);
System.assertEquals(33,obj.EmpDeptno);

}

@isTest static void empListTest2()
{
CompanyClass.EmpClass obj=new CompanyClass.EmpClass();
CompanyClass obj1=new CompanyClass();
obj.Empno=22;
obj.EmpName='fff';
obj.EmpSal=222;
obj.EmpDeptno=33;
obj1.emps.add(obj);
System.assert(obj1.emps!=null);
System.assertEquals(1,obj1.emps.size());

}
}run the test

Friday, April 27, 2012

INSERT,GET,SEARCH DATE FROM CUSTUM OBJECT THROUGH VISUAL FORCE PAGE

CREATE BOOK CUSTOM OBEJECT
fields are.........
BookName
Price
Title
BOOK
BOOK CUSTOM OBJECT


Write Apex Class


public class BookInsert
{

List<Book__c> l;
selectOption Opn;


public SelectOption getOpn() {      
         return new SelectOption('abc','abc');  
        }
 public void setOpn(SelectOption opn)
  {
           this.Opn=opn;
   }
public List<Book__c> getL()
{
  return l;
   } 
public Double price
{
 get { return price; }
      set { price= value; }
      }
public String title
{
 get { return title; }
      set { title= value; }
}
public String head
{
 get { return head; }
      set { head= value; }
}
public String str
{
 get { return str; }
      set { str= value; }
}

public PageReference doSearchBook() {
  l=[select Price__c,title__c from Book__c where title__c LIKE :str+'%'];
  head='search';
  return null;
   }
 public PageReference doInsert() {
   Book__c b=new Book__c(price__c=price,title__c=''+title);
      insert b;
      return null;
   }
  
 public PageReference doGetBook() {
  l=[select Price__c,title__c from Book__c];
  head='GetBook';
  return null;
   }
  
   public List<SelectOption> getBks()
   {
      
       List<SelectOption> options = new List<SelectOption>(); //new list for holding all of the picklist options
       // options.add(new selectOption('1', '- None -')); //add the first option of '- None -' in case the user doesn't want to select a value or in case no values are returned from query below
       options.add(new SelectOption('1', '-select-'));
        for (Book__c bk : [SELECT Price__c,title__c FROM Book__c])
        { //query for User records with System Admin profile
        if(bk!=null&&bk.title__c!=null)
            options.add(new SelectOption(''+bk.Id, ''+bk.title__c)); //for all records found - add them to the picklist options
        }
   
           return options;
        
        }//return the picklist options
}
Create Visual force page


<apex:page controller="BookInsert">
   <apex:form >
   <apex:message rendered="true" />
      <apex:pageBlock mode="edit" id="block">
     
         <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
              <apex:outputLabel for="searchText">Book Insert</apex:outputLabel>
               <apex:panelGroup >
                  <apex:inputText id="price" value="{!price}"/>
                   <apex:inputText id="title" value="{!title}"/>
                  <apex:commandButton value="Insert!" action="{!doInsert}" rerender="block" status="status"/>
                </apex:panelGroup>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
              
          <apex:pageBlockSection >
           <apex:pageBlockSectionItem >
              <apex:outputLabel for="searchText1">Click Here To Get All Books</apex:outputLabel>
                <apex:panelGroup >
                   <apex:commandButton value="Get!" action="{!doGetBook}" rerender="block" status="status"/>
                </apex:panelGroup>
             </apex:pageBlockSectionItem>
          </apex:pageBlockSection>
                      
            <apex:pageBlockSection >
             <apex:pageBlockSectionItem >
               <apex:outputLabel for="searchText2">Click Here To search All Books</apex:outputLabel>
                 <apex:panelGroup >
                   <apex:inputText id="search" value="{!str}"/>
                   <apex:commandButton value="Search" action="{!doSearchBook}" rerender="block" status="status"/>
                 </apex:panelGroup>
               </apex:pageBlockSectionItem>
             </apex:pageBlockSection>
            
            <apex:actionStatus id="status" startText="requesting..."/>
        <apex:pageBlockSection title="{!head}" id="results" columns="1">
           <apex:pageBlockTable value="{!l}" var="i" rendered="{!NOT(ISNULL(l))}">
              <apex:column value="{!i.price__c}"/>
              <apex:column value="{!i.title__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
           
      </apex:pageBlock>
   </apex:form>
</apex:page>
U GET LIKE

DROPDOWN SELECTION & DISPLY SELECECTED ITEM DATA

Create Book Oject like
BookName
Price
Title












Create Apex Class

public class BookDropdown
{
List<Book__c> l;
public String Opn{
 get { return Opn; }
 set { Opn= value; }
}
public List<Book__c> getL()
{

l=[SELECT Price__c,title__c FROM Book__c where Id=:this.Opn];
  return l;
   } 
public Double price
{
 get { return price; }
      set { price= value; }
      }
public String title
{
 get { return title; }
      set { title= value; }
}
public String head
{
 get { return head; }
      set { head= value; }
}
public String str
{
 get { return str; }
      set { str= value; }
}
   public List<SelectOption> getBks()
   {
      
       List<SelectOption> options = new List<SelectOption>(); //new list for holding all of the picklist options
       // options.add(new selectOption('1', '- None -')); //add the first option of '- None -' in case the user doesn't want to select a value or in case no values are returned from query below
       options.add(new SelectOption('1', '-select-'));
        for (Book__c bk : [SELECT Price__c,title__c FROM Book__c])
        { //query for User records with System Admin profile
        if(bk!=null&&bk.title__c!=null)
            options.add(new SelectOption(''+bk.Id, ''+bk.title__c )); //for all records found - add them to the picklist options
        }
   
           return options;
        
        }//return the picklist options
}

Web hosting Create Visual force page
<apex:page controller="BookDropdown">
   <apex:form >
    <apex:actionFunction name="rerenderStates" rerender="list" >
          <apex:param name="firstParam" assignTo="{!opn}" value="" />
      </apex:actionFunction>
   <apex:message rendered="true" />
      <apex:pageBlock mode="edit" id="block">
        
        
            <apex:pageBlockSection >
              <apex:pageBlockSectionItem >
                 <apex:outputLabel value="DropDownBook: " for="themesList"/>
                   <apex:selectList id="themesList" size="1" value="{!opn}" onChange="rerenderStates(this.value)">
                       <apex:selectOptions value="{!bks}"/>
                   </apex:selectList>
              </apex:pageBlockSectionItem>
           </apex:pageBlockSection>
         
           <apex:actionStatus id="status" startText="requesting..."/>
        <apex:pageBlockSection title="{!head}" id="results" columns="1">
           <apex:pageBlockTable id="list" value="{!l}" var="i" rendered="{!NOT(ISNULL(l))}">
              <apex:column value="{!i.price__c}"/>
              <apex:column value="{!i.title__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlockSection>

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

type url in address bar
https://c.ap1.visual.force.com/apex/BookDropdown
u get

Thursday, April 26, 2012

Consuming Apex webservice in C# .net,Consume Apex webservice in C# .net,c#,Salesforce.com,Salesforce.com webservices,apex webservices,web,webservices,csharp webserviceConsuming, Apex webservice,in C# .net


WRITE AN APEX CLASS AS FOLLOWS(ADDITION OF TWO NUMBERS) 
Consumming Apex webservice in C# .net


global with sharing class WebServiceMath
{
webService static Integer add(Integer a,Integer b)
{
return a+b;
}
}
Save the code.... Click on generate from WSDL Button,You will get
<!-- Web Services API : WebServiceMath -->
<definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/"xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://soap.sforce.com/schemas/class/WebServiceMath"targetNamespace="http://soap.sforce.com/schemas/class/WebServiceMath">.....................................................................................................................................
Copy the URL Of WSDL

Then go to VisualStudio

Web hosting
Right Click on Sulution Add WebReferencs
copy:https://ap1.salesforce.com/services/wsdl/class/WebServiceMath
and click on go u will get proxy.---->com.salesforce.ap11
Then come back to apex

App Setup-->dev-API-->
Partner WSDL
A loosely typed WSDL for customers, partners, and ISVs who are building client applications for multiple organizations. It can be used to access data within any organization.
Generate Partner WSDL--click on link

 u get wsdl https://ap1.salesforce.com/services/wsdl/apex
copy this link
Go to VisualStudio
Add webreference past the link and go
give name as ---->sForceService
u get another proxy 

Create Asp.NetPage
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebService._Default" %>
<html>
<head>
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        A:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        B:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="ADD" onclick="Button1_Click" />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>
 
Default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Runtime;
using System.Web.Services;
using System.Web.Services.Protocols;
using WebService.com.salesforce.ap11;
using WebService.sForceService;
namespace WebService
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SforceService o = new SforceService(); 

 LoginResult lr     =o.login("UserName@gmail.com","password+Security Token");
//u get Security Token in      personalsetup
      WebService.sForceService.SessionHeader sh = new WebService.sForceService.SessionHeader();
           
            sh.sessionId = lr.sessionId;
            o.Url = lr.serverUrl;
            o.SessionHeaderValue = sh;
           com.salesforce.ap11.WebServiceMathService obj = new com.salesforce.ap11.WebServiceMathService();
           obj.SessionHeaderValue = new WebService.com.salesforce.ap11.SessionHeader();
          obj.SessionHeaderValue.sessionId=sh.sessionId;
           //obj.Timeout = "6000";
            Label1.Text = obj.add(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text)).ToString();
        }
    }
}