Monday, May 20, 2013

Simple Salesforce.com Apex programming sample to call apex webservice using CSharp (C#)


Here is a very simple code to call a apex web service in salesforce using C#. It's also return a "Contact" object with values.The returning value is based on the query used in the apex web service code. From this example you can understand how to query salesforce objects, how to call apex web service, how to connect to salesforce via code and also you can understand how to retrieve salesforce object value.You can also query the custom objects.
You need to add two web reference in your application.
     1. Enterprise WSDL web reference
     2. WSDL for the webservice class you have created.

To get the Enterprise WSDL navigate to

  • Your Name -> Setup -> App Setup -> Develop -> API
  • There you can get the  Enterprise WSDL

To get the WSDL for the webservice class you have created, navigate to

  • Your Name -> Setup -> App Setup -> Develop -> Apex Class
  • There select your class. 
  • Then click generate WSDL

In the following code ApexService_WSDL is Enterprise WSDL web reference and Enterprise_WSDL is WSDL web reference for the webservice class you have created.

Apex WebService Code in SalesForce:
global class MyCustomController  
 {  
    webservice static Contact[] ReturnObject()  
    {  
      Contact[] cont=[SELECT Firstname, Lastname FROM Contact];  
      return cont;  
    }  
 }  

C# Code in Client Side Application:
SforceService binding = new SforceService();  
 private LoginResult lr;  
 binding.Timeout = 10000;  
 lr = binding.login("YOUR LOGIN EMAIL ID", "PASSWORD + SECURITY TOKEN");  
 binding.Url = lr.serverUrl;  
 binding.SessionHeaderValue = new Enterprise_WSDL.SessionHeader();  
 binding.SessionHeaderValue.sessionId = lr.sessionId;
 
 using (ApexService_WSDL.MyCustomControllerService client = new MyCustomControllerService())  
 {  
    client.SessionHeaderValue = new ApexService_WSDL.SessionHeader();   
    client.SessionHeaderValue.sessionId = lr.sessionId;                   
    ApexService_WSDL.Contact[] m = client.ReturnObject();  
 }  

Don't forget to replace you salesforce email id , password + security token ;). To get or reset your security token, navigate to the following path.

  •      Your Name -> Setup -> My Personal Information -> Reset Security Token, and click the Reset My Security Token button
  • And you will get a mail with security token.

Tuesday, May 14, 2013

Show and Hide text in ASP.NET page using JavaScript


Here JavaScript is used to Show and Hide text in ASP.NET. To do that use the below code. Replace the Sample Text inside the paragraph tag with the content u need to show hide.
JavaScript Code:
 <script type="text/javascript">  
   function ShowHide(ref) {  
     var txtArea = document.getElementById(ref);  
     var xtxtArea = document.getElementById("x" + ref);  
     if (txtArea.style.display == 'none')   
     {  
       xtxtArea.innerHTML = '<b><u>Hide Text</u></b>';  
       txtArea.style.display = '';  
     }  
     else  
     {  
       xtxtArea.innerHTML = '<b><u>Show Text</u></b>';  
       txtArea.style.display = 'none';  
     }  
   }  
 </script>  

Design Code:
 <a href="#_link" id="A1" onclick="ShowHide('txtArea');"><b><u>Show Text</u></b></a>  
 <div id="Div1" style="display: none">  
   <p>  
     Sample Text....Sample Text....Sample Text....Sample Text....  
     Sample Text....Sample Text....Sample Text....Sample Text....  
   </p>  
 </div>  

Ajax ModalPopupExtender control sample in ASP.NET


ModalPopupExtender is a very useful control in Ajax Control Toolkit. It's used to display content as a modal dialog box. Here when the user clicks the "Show" button a panel will be displayed as a modal dialog. That panel has a "Hide" button. When the user clicks the hide button the modal dialog will be closed. use your creativity to display the modal dialog box more interactively using CSS.
Namespace:
 using AjaxControlToolkit;  

Design Code:
 <asp:Button ID="btn_Show" runat="server" Text="Show" OnClick="btn_Show_Click" />  
 <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>  
 <cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" PopupControlID="Panel1" TargetControlID="HiddenField1" BackgroundCssClass="modalBackground"></cc1:ModalPopupExtender>  
 <asp:HiddenField ID="HiddenField1" runat="server" />  
 <asp:Panel ID="Panel1" runat="server">  
   <div style="width: 200px; height: 150px; background-color: yellow;">  
       <asp:Button ID="btn_Hide" runat="server" Text="Hide" OnClick="btn_Hide_Click" />  
   </div>  
 </asp:Panel>  

C# Code:
 protected void btn_Show_Click(object sender, EventArgs e)  
 {  
    this.ModalPopupExtender1.Show();  
 }  
 protected void btn_Hide_Click(object sender, EventArgs e)  
 {  
    this.ModalPopupExtender1.Hide();  
 }  

CSS code:
 .modalBackground  
 {  
   background-color: Gray;  
   filter: alpha(opacity=70);  
   opacity: 0.7;  
 }  

Note: you have to add Ajax Controll Toolkit in project.