Friday, September 16, 2016

Lightning Component frame work

What is Salesforce Lightning?

Lightning includes the Lightning Component Framework and some exciting tools for developers. Lightning makes it easier to build responsive applications for any device.Lightning includes these technologies:

• Lightning components give you a client-server framework that accelerates development, as well as app performance, and is ideal for use with the Salesforce1 mobile app and Salesforce Lightning Experience.

• The Lightning App Builder empowers you to build apps visually, without code, quicker than ever before using off-the-shelf and custom-built Lightning components. You can make your Lightning components available in the Lightning App Builder so administrators can build custom user interfaces without code.

Using these technologies, you can seamlessly customize and easily deploy new apps to mobile devices running Salesforce1. In fact, theSalesforce1 mobile app and Salesforce Lightning Experience are built with Lightning components.

Lightning scripting vs visual force java scripting

We all know when we want to refer any element inside javascript which is embedded in the visual force page, we will be using "document.getelementbyid". Once we get reference of the element we usually perform what ever operations that we want to do inside the scripting.

Well there is no mystery inside the lightning component as well, we all know ,  mostly we are using javascripting inside the aura components , this is pretty simple. Refer any element by using "component.find("Id");" and do what ever you wish on that attribute or element.

----------------------------------------------------------------------------------------------------------------------
<aura:component >
    <ui:button label="Toggle" press="{!c.toggle}"/>
    <p aura:id="text">Now you see me</p>
</aura:component>

-------------------------------------------------------------------------------------------------------------------------

/*toggleCssController.js*/
({
    toggle : function(component, event, helper) {
     
//following statement gets the reference of the attribute by id :text and hence we can do what ever we //want on that attribute like hiding and rendering and so on...
        var toggleText = component.find("text");
//if we want to refer the attribute by name, we have to use component.get("v.nameofattribute")
//if we want to set data to the any attribute, we have to use component.set("v.nameofattribute",value )
//which we want to pass..
        $A.util.toggleClass(toggleText, "toggle");
    }
})
--------------------------------------------------------------------------------------------------------------------------

If above components did not give clear picture on referring aura components by ID, following example will definitely drive out any sort of confusion. copy paste below code and keep the component inside the lightning app.
--------------------------------------------------------------------------------------------------------------------------

<aura:component >
<ui:inputNumber label="First number" aura:id="fn" placeholder="0"/>
    <ui:inputNumber label="second number" aura:id="sn" placeholder="0"/>
    <ui:button aura:id="outputButton" label="Addition" press="{!c.calci}"/>
    <ui:outputNumber aura:id="opn" value=""/>
</aura:component>

-----------------------------------------------------------------------------------------------------------------------
/*Aura controller code */

({
calci : function(cmp, event) {
var firstnumber = cmp.find("fn").get("v.value");
var secondnumber = cmp.find("sn").get("v.value");
var outnumber = cmp.find("opn");
        var finalnumber=0;
        finalnumber=firstnumber+secondnumber;
 
outnumber.set("v.value", finalnumber);
       
}
})
---------------------------------------------------------------------------------------------------------------------------


Caluclator with lightning vs visual force page

This will be super example to understand lightning basics interms of  getters, setters and rendering partial page options, I'am sure this code will solve lot of doubts that new bees has.

Compare vf code and lightning code which I have provided below and execute in your org..
You will get much more confidence on lightning
------------------------------------------------------------------------------------------------------------------------
Component code
------------------------------------------------------------------------------------------------------------------------

<aura:component >
    <aura:attribute name="num1" type="Integer"/>
    <aura:attribute name="num2" type="Integer"/>
    <aura:attribute name="num3" type="Integer"/>
    <ui:inputNumber label="Number 1" value="{!v.num1}"  />
    <ui:inputNumber label="Number 2" value="{!v.num2}"   />
    <aura:attribute name="addbo1" type="Boolean" default="false"/>
    <aura:attribute name="subbo1" type="Boolean" default="false"/>
    <aura:attribute name="mulbo1" type="Boolean" default="false"/>
    <aura:attribute name="divbo1" type="Boolean" default="false"/>
    <aura:attribute name="refreshbo1" type="Boolean" default="false"/>
<aura:if isTrue="{!v.addbo1}">
    Addition of Two Numbers is :  {!num3}
    <ui:outputNumber  value="{!v.num3}"/>
</aura:if>
    <aura:if isTrue="{!v.subbo1}">
     Substraction of Two Numbers is :  {!num3}
         <ui:outputNumber  value="{!v.num3}"/>
</aura:if>
    <aura:if isTrue="{!v.mulbo1}">
    multiplication of Two Numbers is :  {!num3}
         <ui:outputNumber  value="{!v.num3}"/>
</aura:if>
    <aura:if isTrue="{!v.divbo1}">
    Division of Two Numbers is :  {!num3}
         <ui:outputNumber  value="{!v.num3}"/>
</aura:if>
     <ui:button press="{!c.Add}" label="Addition" />
     <aura:if isTrue="{!v.refreshbo1}">
     <ui:button press="{!c.Refresh}" label="Refresh" />
    </aura:if>
    <ui:button press="{!c.Sub}" label="Substraction" />
 
    <ui:button press="{!c.Mul}" label="Multiplication" />
 
    <ui:button press="{!c.Div}" label="Division" />

   
 
</aura:component>

-----------------------------------------------------------------------------------------------------------------------
Controller code
------------------------------------------------------------------------------------------------------------------------


({
Add : function(component, event, helper) {
        var num1=component.get("v.num1");
        var num2=component.get("v.num2");
        var num3=num1+num2;
        component.set("v.num3", num3);
         component.set("v.addbo1", true);
        component.set("v.refreshbo1", true);
            component.set("v.mulbo1", false);
          component.set("v.subbo1", false);
          component.set("v.divbo1", false);
     

},
    Mul : function(component, event, helper) {
        var num1=component.get("v.num1");
        var num2=component.get("v.num2");
        var num3=num1*num2;
        component.set("v.num3", num3);
         component.set("v.mulbo1", true);
          component.set("v.refreshbo1", true);
            component.set("v.divbo1", false);
          component.set("v.subbo1", false);
          component.set("v.addbo1", false);
     

},
    Sub : function(component, event, helper) {
        var num1=component.get("v.num1");
        var num2=component.get("v.num2");
        var num3=num1-num2;
        component.set("v.num3", num3);
         component.set("v.subbo1", true);
          component.set("v.refreshbo1", true);
            component.set("v.mulbo1", false);
          component.set("v.divbo1", false);
          component.set("v.addbo1", false);
     

},
    Div : function(component, event, helper) {
        var num1=component.get("v.num1");
        var num2=component.get("v.num2");
        var num3=num1/num2;
        component.set("v.num3", num3);
         component.set("v.divbo1", true);
          component.set("v.refreshbo1", true);
            component.set("v.mulbo1", false);
          component.set("v.subbo1", false);
          component.set("v.addbo1", false);
     

},
   
     Refresh : function(component, event, helper) {
     
         component.set("v.num1", 0);
          component.set("v.num1", 0);
         component.set("v.divbo1", false);
          component.set("v.mulbo1", false);
          component.set("v.subbo1", false);
          component.set("v.addbo1", false);
           component.set("v.refreshbo1", false);
     

}
})

-------------------------------------------------------------------------------------------------------------------------
visual force page for the caluclator code with same results as above ligtnening
---------------------------------------------------------------------------------------------------------------------------
<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;


}

}

Compare Vf page and Lightning page

Some of us feel like lightning is altogether different from visual force pages but the fact is when we try to co relate the vf page with the lightning component , it would be very easy to understand the lightning concepts.

Following vf page displays different colors with text by making use of css styles and next followed lightning component also displays same  by making use css styles again.   Both are almost similar with minor differences.
 


Visual force page with "Hello world" multiple colors
--------------------------------------------------------------------------------------------------------------------------
<apex:page >

   <style>

.container {
    padding-top: 20px;
     background-color: yellow;
}

.blue1
{
   background-color: blue; }
   .red1
{
   background-color: red; }
 
   .green1
{
   background-color: green; }
 


   </style>

<div class="container">
<h1>Hello Visul force styles!</h1>

 <div >
         <h1>This is thimma code</h1>
         <div class="red1">I'm red.</div>
         <div class="blue1">I'm blue.</div>
         <div class="green1">I'm green.</div>
     </div>


</div>

 </apex:page>
--------------------------------------------------------------------------------------------------------------------------

Lightning  page with "Hello world" multiple colors
CMP name: Helloworld

--------------------------------------------------------------------------------------------------------------------------

<aura:component implements="force:appHostable">
<h1>Hello Lightning Component!</h1>
    <style>
     
    .THIS {
    background-color: yellow;
    padding-top: 10px;
}

h1.THIS {
    font-size:60px;
}

.THIS h1{
    font-weight: bold;
    padding: 10px;
}

.THIS.container {
    padding-top: 20px;
}

.THIS .red {
    background-color: red;
    padding: 10px;
}

.THIS .blue {
    background-color: blue;
    padding: 10px;
}

.THIS .green {
    background-color: green;
    padding: 10px;
}
 
 
 
    </style>
    <div class="container">
         <h1>This is thimma code</h1>
         <div class="red">I'm red.</div>
         <div class="blue">I'm blue.</div>
         <div class="green">I'm green.</div>
     </div>
 

</aura:component>
-------------------------------------------------------------------------------------------------------------------------




Tuesday, August 2, 2016

javascript code which solves focus of text issues on IOS device.

Several times I see when we are using IOS device especially Iphone , focus issues are rising up when some one try to edit the text area  inside the IOS device's. just add below peace of code inside your page and see the results. 

<script>
window.onkeydown=function(){window.focus();}
</script>

Wednesday, October 7, 2015

Salesforce: Pagination, search engine ,column sorting ,alphabetical sorting

------------------------------------------------------------------------------------------------------------
Vf  page
------------------------------------------------------------------------------------------------------------


<apex:page controller="FeedbackUserSelectionController1">
<apex:sectionHeader title="Feedback Form" subtitle="User Selection Page"  />
   <style type="text/css">
.loading-icon {
   background-image: url(/img/loading.gif);
   width: 16px;
   height: 16px;
}
a.alpha-link {
   font-weight: normal;
   font-size: 91%;
   padding: 0 4px;
   color: #015BA7 !important;
}
a.alpha-link+a.alpha-link {
   border-left: 1px solid #CFCECE;
}
a.alpha-link:hover {
   background-color: #e0f5fc !important;
}
a.alpha-select {
   font-weight: bold;
   text-decoration: none;
   background-color: #C6E1FF;
   color: #000000 !important;
}
.search-block {
   text-align: center;
}
.search-block input {
   margin: 0px 15px 0px 5px;
}
.search-block-button {
   min-width: 110px;
}
.process-block {
   text-align: center;
   margin-top: 10px;
}
.process-block input {
   margin: 0px 15px;
}
.process-block-button {
   min-width: 110px;
}
.page-buttons input {
   min-width: 110px;
}
</style>
   <apex:form id="TheForm">
      <!-- ***************************** -->
      <!-- Search Criteria               -->
      <apex:pageBlock mode="maindetail" >
         <div class="search-block">
            <div style="display: inline-table">
               <span>Name:</span>
               <apex:inputText value="{!SearchName}" />
            </div>
            <div style="display: inline-table">
               <span>Department:</span>
               <apex:inputText value="{!Searchdepartment}" />
            </div>
            <apex:commandButton styleClass="search-block-button" value="Search" action="{!Searchuser}" rerender="TablePanel"
               status="TableUpdateStatus" />
         </div>
         <div class="process-block">
            <apex:actionStatus id="ProcessButtonStatus">
               <apex:facet name="stop">
                  <apex:outputPanel >
                     <apex:commandButton styleClass="process-block-button" value="Process Selected" action="{!DoSomethingMany}"
                        status="ProcessButtonStatus" rerender="nothing" />
                     <apex:commandButton styleClass="process-block-button" value="Clear All" action="{!ClearAll}"
                        rerender="TheForm,TablePanel" />
                  </apex:outputPanel>
               </apex:facet>
               <apex:facet name="start">
                  <apex:outputPanel >
                     <apex:commandButton styleClass="process-block-button" value="Processing..." disabled="true" />
                     <apex:commandButton styleClass="process-block-button" value="Processing..." disabled="true" />
                  </apex:outputPanel>
               </apex:facet>
            </apex:actionStatus>
         </div>
      </apex:pageBlock>
      <!-- ************************* -->
      <!-- search results table      -->
      <apex:pageBlock id="TablePanel" title="Select Users to Give feedback ">
         <div>
            <span class="page-buttons" style="float: left; margin-bottom: 5px;"> <apex:commandButton disabled="{!!StdSetControllerUser.hasprevious}" value="Previous" action="{!StdSetControllerUser.previous}"
                  rerender="TablePanel" /> <apex:commandButton disabled="{!!StdSetControllerUser.hasnext}" value="Next"
                  action="{!StdSetControllerUser.next}" rerender="TablePanel" />
            </span>
            <!-- alphabet selection -->
            <span style="float: right; margin: 5px 5px 5px 5px;"> <apex:repeat value="{!AlphaList}" var="a">
                  <apex:commandLink value="{!a}" action="{!BuildQuery}" rerender="TablePanel"
                     styleClass="alpha-link{!if(AlphaFilter=a,' alpha-select','')}" status="TableUpdateStatus">
                     <apex:param name="AlphaFilter" value="{!a}" assignTo="{!AlphaFilter}" />
                  </apex:commandLink>
               </apex:repeat>
            </span>
         </div>
         <div style="clear: both;"></div>
         <apex:actionStatus id="TableUpdateStatus">
            <apex:inputHidden value="{!AlphaFilter}" id="hiddenField" />
            <!-- loading message -->
            <apex:facet name="start">
               <apex:outputPanel layout="block" styleClass="message infoM4">
                  <apex:panelGrid columns="2" styleClass="messageTable" columnClasses="messageCell" style="padding:0px;margin:0px;">
                     <apex:panelGroup >
                        <img class="loading-icon" src="/s.gif" />
                     </apex:panelGroup>
                     <apex:panelGroup >
                        <div class="messageText">Please wait...</div>
                     </apex:panelGroup>
                  </apex:panelGrid>
               </apex:outputPanel>
            </apex:facet>
            <!-- user table -->
            <apex:facet name="stop">
               <apex:pageBlockTable value="{!CurrentUserList}" var="a">
               
                  <apex:column >
                     <apex:facet name="header">
                        <apex:outputPanel id="SelectedCount">
                           <div style="text-align: center;">
                              <apex:outputText value="Selected" />
                              <br />
                              <apex:outputText value="{!UserSelectedCount}" />
                           </div>
                        </apex:outputPanel>
                     </apex:facet>
                     <div style="text-align: center;">
                        <apex:inputCheckBox value="{!a.aCheckBox}" id="check-box">
                           <apex:actionSupport event="onchange" rerender="SelectedCount" action="{!UpdateUserSelectedSet}" />
                           </apex:inputcheckbox>
                     </div>
                  </apex:column>
                  <apex:column >
                     <apex:facet name="header">
                        <apex:commandLink action="{!SortToggle}" rerender="TablePanel" status="TableUpdateStatus">
                           <apex:param name="SortField" value="Name" assignTo="{!SortField}" />
                           <apex:outputText value="{!$ObjectType.User.Fields.Name.Label}{!IF(SortField=='Name',IF(SortDirection='asc','?','?'),'')}" />
                        </apex:commandLink>
                     </apex:facet>
                     <apex:outputLink value="/{!a.aUser.Id}" target="_blank">{!a.aUser.Name}</apex:outputlink>
                  </apex:column>
                 
                  <apex:column >
                     <apex:facet name="header">
                        <apex:commandLink action="{!SortToggle}" rerender="TablePanel" status="TableUpdateStatus">
                           <apex:param name="SortField" value="Department" assignTo="{!SortField}" />
                           <apex:outputText value="{!$ObjectType.User.Fields.Department.Label}{!IF(SortField=='Department',IF(SortDirection='asc','?','?'),'')}" />
                        </apex:commandLink>
                     </apex:facet>
                     <apex:outputField value="{!a.aUser.Department}" />
                  </apex:column>
                  <apex:column >
                     <apex:facet name="header">
                        <apex:commandLink action="{!SortToggle}" rerender="TablePanel" status="TableUpdateStatus">
                           <apex:param name="SortField" value="CompanyName" assignTo="{!SortField}" />
                           <apex:outputText value="{!$ObjectType.User.Fields.CompanyName.Label}{!IF(SortField=='CompanyName',IF(SortDirection='asc','?','?'),'')}" />
                        </apex:commandLink>
                     </apex:facet>
                     <apex:outputField value="{!a.aUser.CompanyName}" />
                  </apex:column>
                  <apex:column >
                     <apex:facet name="header">
                        <apex:commandLink action="{!SortToggle}" rerender="TablePanel" status="TableUpdateStatus">
                           <apex:param name="SortField" value="Email" assignTo="{!SortField}" />
                           <apex:outputText value="{!$ObjectType.User.Fields.Email.Label}{!IF(SortField=='Email',IF(SortDirection='asc','?','?'),'')}" />
                        </apex:commandLink>
                     </apex:facet>
                     <apex:outputField value="{!a.aUser.Email}" />
                  </apex:column>
               
             
               
               </apex:pageBlockTable>
            </apex:facet>
         </apex:actionStatus>
         <div style="margin-top: 5px;">
            <apex:outputText value="Number of Records per Page: " />
            <apex:selectList value="{!RecPerPage}" size="1">
               <apex:selectOptions value="{!RecPerPageOption}" />
               <apex:actionSupport event="onchange" action="{!BuildQuery}" reRender="TablePanel" status="TableUpdateStatus" />
            </apex:selectList>
         </div>
      </apex:pageBlock>
   </apex:form>
</apex:page>

------------------------------------------------------------------------------------------------------------
Controller...
------------------------------------------------------------------------------------------------------------



public with sharing class FeedbackUserSelectionController1 {
    // User and selection set/variables
    private list<UserSubclass> Userlist {get; set;}
    private set<Id> UserSelectedSet;
    public Integer UserSelectedCount {get; set;}
    public String SelectedOneUser {get; set;}
    // selection and filter
    public list<String> AlphaList {get; set;}
    public String AlphaFilter {get; set;}
    public String SearchName {get; set;}
    public String Searchdepartment {get; set;}
    private String SaveSearchName;
    private String SaveSearchdepartment;
    private String Queryuser;
    // display sort and number
    public String RecPerPage {get; set;}
    public list<SelectOption> RecPerPageOption {get; set;}
    public String SortFieldSave;
    /***
    * FeedbackUserSelectionController - Constructor initialization
    ***/
   
    public FeedbackUserSelectionController1(){
        Userlist = new list<UserSubclass>();
        UserSelectedSet = new set<Id>();
        //records for page initialization
        RecPerPageOption = new list<SelectOption>();
        RecPerPageOption.add(new SelectOption('10','10'));
        RecPerPageOption.add(new SelectOption('25','25'));
        RecPerPageOption.add(new SelectOption('50','50'));
        RecPerPageOption.add(new SelectOption('100','100'));
        RecPerPageOption.add(new SelectOption('200','200'));
        RecPerPage = '10'; //default records per page
        // initialization alpha list
        AlphaList = new list<String> {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'Other', 'All'};
            SortFieldSave = SortField;
        // alpha filter, use page parameter or set default to all
        if (apexpages.currentpage().getparameters().get('alpha') == null) {
            AlphaFilter = 'All';
        } else {
            AlphaFilter = apexpages.currentpage().getparameters().get('alpha');
        }
        // list generation
        BuildQuery();
    }
    /***
    * StdSetControllerUser - paging through the User list
    ***/
   
   
    public ApexPages.StandardSetController StdSetControllerUser {
        get {
            if(StdSetControllerUser == null) {
                StdSetControllerUser = new ApexPages.StandardSetController(Database.getQueryLocator(Queryuser));
                // sets the number of records in each page set
                StdSetControllerUser.setPageSize(Integer.valueOf(RecPerPage));
            }
            return StdSetControllerUser;
        }
        set;
    }
   
   
    /***
    * getCurrentUserlist - return an User list for the table
    ***/


   
    public list<UserSubclass> getCurrentUserlist() {
        UpdateUserSelectedSet();
        Userlist = new list<UserSubclass>();
        for (User a : (list<User>)StdSetControllerUser.getRecords()) {
            Userlist.add(new UserSubclass(a, UserSelectedSet.contains(a.Id)));
        }
        return Userlist;
    }
   
   
    /***
    * UpdateUserSelectedSet - add/remove Users from the selected User id list
    ***/
    public void UpdateUserSelectedSet(){
        for(UserSubclass a : Userlist ){
            if(a.aCheckBox == true) {
                UserSelectedSet.add(a.aUser.Id);
            } else {
                if(UserSelectedSet.contains(a.aUser.Id)) {
                    UserSelectedSet.remove(a.aUser.Id);
                }
            }
        }
        UserSelectedCount = UserSelectedSet.size();
    }
    /***
    * ClearUserSelectedSet - remove selected Users and initialize counter
    ***/
    public PageReference ClearAll(){
        Userlist.clear();
        UserSelectedSet.clear();
        UserSelectedCount = 0;
        SearchName = '';
        Searchdepartment = '';
        SaveSearchName = '';
        SaveSearchdepartment = '';
        AlphaFilter = 'All';
        BuildQuery();
        return null;
    }
    /***
    * Searchuser - set search criteria fields and refresh User table
    ***/
    public PageReference Searchuser() {
        SaveSearchName = SearchName;
        SaveSearchdepartment = Searchdepartment;
        BuildQuery();
        return null;
    }
    /***
    * BuildQuery - build query command for list selection change
    ***/
    public void BuildQuery() {
        StdSetControllerUser = null;
        String QueryWhere = '';
        if (AlphaFilter == null || AlphaFilter.trim().length() == 0) {
            AlphaFilter = 'All';
        }
        Queryuser = 'SELECT Department,FirstName,LastName,Name , CompanyName, Email, LOB__c ' +
            ' FROM User';
        if (AlphaFilter == 'Other') {
            QueryWhere = BuildWhere(QueryWhere, '(' + String.escapeSingleQuotes(SortField) + ' < \'A\' OR ' +
                                    String.escapeSingleQuotes(SortField) + ' > \'Z\') AND (NOT ' +
                                    String.escapeSingleQuotes(SortField) + ' LIKE \'Z%\') ');
        } else if (AlphaFilter != 'All') {
            QueryWhere = BuildWhere(QueryWhere, '(' + String.escapeSingleQuotes(SortField) + ' LIKE \'' + String.escapeSingleQuotes(AlphaFilter) + '%\')' );
        }
        if (SaveSearchName != null) {
            QueryWhere = BuildWhere(QueryWhere, ' (Name LIKE \'%' + String.escapeSingleQuotes(SaveSearchName) + '%\')');
        }
        if (SaveSearchdepartment != null) {
              QueryWhere = BuildWhere(QueryWhere, ' (Department LIKE \'%' + String.escapeSingleQuotes(SaveSearchdepartment) + '%\')');
        }
        Queryuser += QueryWhere;
        Queryuser += ' ORDER BY ' + String.escapeSingleQuotes(SortField) + ' ' + String.escapeSingleQuotes(SortDirection) + ' LIMIT 10000';
        system.debug('Queryuser:' + Queryuser);
    }
    /***
    * BuildWhere - build soql string for where criteria
    ***/
    public String BuildWhere(String QW, String Cond) {
        if (QW == '') {
            return ' WHERE ' + Cond;
        } else {
            return QW + ' AND ' + Cond;
        }
    }
    /***
    * SortDirection - return sort direction. Default ascending(asc)
    ***/
    public String SortDirection {
        get { if (SortDirection == null) {  SortDirection = 'asc'; } return SortDirection;  }
        set;
    }
    /***
    * SortField - return sort by field. Default to Name
    ***/
    public String SortField {
        get { if (SortField == null) {SortField = 'Name'; } return SortField;  }
        set;
    }
    /***
    * SortToggle - toggles the sorting of query from asc<-->desc
    ***/
    public void SortToggle() {
        SortDirection = SortDirection.equals('asc') ? 'desc NULLS LAST' : 'asc';
        // reset alpha filter and sort sequence when sorted field is changed
        if (SortFieldSave != SortField) {
            SortDirection = 'asc';
            AlphaFilter = 'All';
            SortFieldSave = SortField;
        }
        // run the query again
        BuildQuery();
    }
    /***
    * DoSomethingOne - do something with one selected User
    ***/
    public PageReference DoSomethingOne() {
        system.debug('SelectedOneUser: ' + SelectedOneUser);
        return null;
    }
    /***
    * DoSomethingMany - do something with many selected Users
    ***/
    public PageReference DoSomethingMany() {
        for (Id UserId : UserSelectedSet) {
            system.debug('Checked: ' + UserId);
        }
        return null;
    }
 
    public class UserSubclass {
        public Boolean aCheckBox {get;set;}
        public User  aUser {get;set;}
        // sub-class initialization
        public UserSubclass(User a, Boolean chk){
            aUser = a;
            aCheckBox = chk;
        }
    }
}

Monday, September 28, 2015

How to expose restapi class to the Legacy systems ?

By using @RestResource class and annotations like @httppost, @httpget ,@httpput, @httpdelete..etc

we need to place these annotations before the Methods which we want to expose..

ex:  @RestResource(urlMapping='/Merchandise/*')
global with sharing class MerchandiseManager {
  
    @HttpGet
    global static Merchandise__c getMerchandiseById() {
        RestRequest req = RestContext.request;        
        String merchId = req.requestURI.substring(
                                  req.requestURI.lastIndexOf('/')+1);
        Merchandise__c result = 
                       [SELECT Name,Description__c,Price__c,Total_Inventory__c
                        FROM Merchandise__c 
                        WHERE Id = :merchId];
        return result;
    }
  
    @HttpPost
    global static String createMerchandise(String name,
        String description, Decimal price, Double inventory) {
        Merchandise__c m = new Merchandise__c(
            Name=name,
            Description__c=description,
            Price__c=price,
            Total_Inventory__c=inventory);
        insert m;
        return m.Id;
    }
}