Tuesday, 17 September 2013

Add and Delete row in salesforce














<apex:page controller="AddDeleteRow1" >
  <!-- Javascript -->
<script type = "text/javascript">
    function winClose()
    {
        self.close();
    }
</script>
<!-- End of Javascript-->
<apex:form >
    <apex:pageBlock >
        <apex:pageblockSection >
            <apex:pageblocktable value="{!memberList11}" var="mem">
                <apex:column title="Name" value="{!mem.Name}"/>
                <apex:column title="Brand" value="{!mem.Brand__c}"/>
            </apex:pageblocktable>
        </apex:pageblockSection>
    </apex:pageBlock>
   
  <apex:variable var="rowNum" value="{!0}"/>
   
    <apex:pageBlock id="membAdd" >  
      <apex:variable var="rowNum" value="{!0}"/>          
        <apex:pageblockSection >
            <apex:pageBlockTable value="{!memberAddList}" var="memb">
                <apex:facet name="footer">
                    <apex:commandLink value="Add Row" action="{!addRow}" reRender="membAdd"/>
                </apex:facet>
                <apex:column headerValue="No." style="width:20px; text-align:center;" headerClass="centertext">
                    <apex:outputText value="{0,number, ###}" style="text-align:center;">  
                        <apex:param value="{!rowNum+1}" />  
                    </apex:outputText>
                </apex:column>           
                <apex:column headerValue="Member Name">
                    <apex:inputField value="{!memb.Name}"/>
                </apex:column>
                <apex:column headerValue="Cost">
                    <apex:inputField value="{!memb.Cost__c}"/>
                </apex:column>
                <apex:column headerValue="eMail Id">
                    <apex:inputField value="{!memb.Brand__c}"/>
                </apex:column>
                <apex:column headerValue="Delete" >
                    <apex:commandLink style="font-size:15px; font-weight:bold; text-align:center;color:red;" value="X" action="{!delRow}" reRender="membAdd,temp" rendered="{!rowNum>=0}">
                        <apex:param value="{!rowNum}" name="index" />
                    </apex:commandLink>
                    <apex:variable var="rowNum" value="{!rowNum+1}"/>
                </apex:column>               
            </apex:pageBlockTable>                   
        </apex:pageblockSection>       
        <apex:pageblockSection columns="1" >
            <apex:pageblockSectionItem >
                <apex:commandButton value="Save" action="{!saveMemb}" onComplete="winClose();"/>
                <apex:commandButton value="Cancel" onclick="winClose();" />
            </apex:pageblockSectionItem>        
        </apex:pageblockSection>
    </apex:pageBlock>
</apex:form></apex:page>

public with sharing class AddDeleteRow1 {

public List<bottle__c> memberList {get;set;}
    public List<bottle__c> memberAddList {get;set;}
    public String memberName {get;set;}
    public Integer rowNum{get;set;}
   
    public AddDeleteRow1()
    {
        getmemberList11();
        memberAddList = new List<Bottle__c>();
        memberAddList.add(new Bottle__c());
    }
   
    //List<bottle__c> member{get;set;}
       public List<bottle__c> getmemberList11(){
           /*String sql = 'SELECT Name, brand__c FROM bottle__c';
           member = Database.Query(sql);*/
           memberList = [SELECT Name, brand__c,cost__c FROM bottle__c LIMIT 10];
           return memberList;
        }
         
    public void AddRow()
    {
        memberAddList.add(new bottle__c());
    }
   
    public void delRow()
    {
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        memberAddList.remove(rowNum);  
    }   
   
    public void saveMemb()
    {
        insert memberAddList;
    }

}

Governor Limits in salesforce

  Multitanent architecture :  To share multiple users in single environment across common and exclusive features.
Governor limits:
             Governor limits simply means the limited database calls that a developer/user do.
These governor limits are important because unlimited and free access to database many number of times from each time will slow down the database effectiveness.So,to preserve the database effectiveness, governor limits are important in multi-tenant environment
  GL are are run time limits enforced by the ARE to ensure that code does not misbehave. Because apex runs in multitenant environment,types of limits apex enforces resources like memory,data base resources,num of script stmts to avoid infinite loops and num of records being processed.
@future :
1.       In apex by default every method is synchronize. If we can convert synchronize to asynchronized we can use @future. User wont have to wait  for  processing.
2.       Using @future to get higher governor limits.
Ex:
    If we can write trigger for Account and the apex logic wants to update whenever record is updated the related contacts are updated, in this scenario apex trigger cant be invoked by single account update would not be able to update thounsands of records synchronously. If we can use @future that allows to retrieve upto 50,000 records on SOQL,10,000 records in DML asynchronously.
3.      If we can call third party webservice methods(i.e call outs) form within trigger,you will need to execute that call out asynchronously by defining the web service request and response handling in @future
4.      If we use @future in method,we cant pass the sObject as a argument.

Unit test:
    unit tests are compressed test methods and classes that verify wheather a particular piece of code working properly or not.
     If your code is going to be packaged and placed force.com App exchange,the test method must provide 75% code coverage, code coverage means deviding the executing lines of code, total number of lines of classes(include triggers), it cant include test method code.
Unit test methods take no arguments, commit no data to the database, send no emails, and are flagged with the testMethod keyword in the method definition.

Test methods cannot be used to test Web service callouts. Web service callouts are asynchronous, while unit tests are synchronous.

Hint :   test method cant write with in trigger,because of it cant called out side of test context.
 EX: public class myClass {
    static testMethod void myTest() {
       // Add test method logic using System.assert(), System.assertEquals()
       // and System.assertNotEquals() here.
     }
}
 import wizard :
1.       Import upto 50,000 rex
2.       Here we can perform insert
3.       We can operate only standard  Lead,account,contact,solution and all custom objects
4.       This process is perform at that point of time
5.       We cant export the data
6.       Here we can prevent Duplicates records
Data Loader :
       1.we can import upto 50 mil rec
       2. we can perform insert,update,delete,upsert,harddelete,export,exportall.
       3.based on external id we can perform those actions
       4. this process is batch  process.
       5.the default batch size is 200, we override these batch using Bulk API option upto 10000.
       6.we can insert data from specified record to end

Batch Apex:
scheduling :
global class scheduledsec implements Schedulable {
   
    global void execute(SchedulableContext sc) {
       List<Book__c> li=[select id from Book__c];
       delete li;
    }
}
Scheduling the above class

Work flow :  work flow is automated process rule, this rule to perform when record is created or edited to check the specific criteria.
Work flow actions  1.New task 2. Field update 3. Send email 4.outbound message
Time dependent Work flow :
             When record matches the sprcified criteria execute according to time trigger.
 Time dependent workflow does not follow the every time the record is created or edited
Approval process :  
            Approvals are complex business  process ,this rule to perform when a record approve or rejected to Hierarchal  role user to check the specied criteria.
Approval process steps :
1.      Initial submission Action
2.      Final approval Action
3.      Final rejection Action
4.      Recall Action
Aggregate function :
 List<AggregateResult> b  = [SELECT name, MAX(Price__c) FROM Book__c  GROUP BY name];
system.debug('hhhhhhhhhhhhhhh'+b[0]);



prf----per---
owd--shring---

pemission--ok

pem means (pem to objects & fields)
sharing-field level secu

so

feld lvl--pem











@RemoteAction in Visual force page

@RemoteAction in Visual force page
JavaScript remoting in Visualforce provides support for some methods in Apex controllers to be called via JavaScript.

JavaScript remoting has three parts:
·                     The remote method invocation you add to the Visualforce page, written in JavaScript.
·                     The remote method definition in your Apex controller class. This method definition is written in Apex, but there are few differences from normal action methods.
·                     The response handler callback function you add to or include in your Visualforce page, written in JavaScript.
 To use JavaScript remoting in a Visualforce page, add the request as a JavaScript invocation with the following form:
[namespace.]controller.method(
    [parameters...,]
    callbackFunction,
    [configuration]
);
·                     namespace is the namespace of the controller class. This is required if your organization has a namespace defined, or if the class comes from an installed package.
·                     controller is the name of your Apex controller.
·                     method is the name of the Apex method you’re calling.
·                     parameters is the comma-separated list of parameters that your method takes.
·                     callbackFunction is the name of the JavaScript function that will handle the response from the controller. You can also declare an anonymous function inline. callbackFunction receives the status of the method call and the result as parameters.
·                     configuration configures the handling of the remote call and response. Use this to specify whether or not to escape the Apex method’s response. The default value is {escape: true}.
Visualforce Page:

<apex:page controller="sample">
    <script type="text/javascript">
    function getAccountJS()
 
    {
        var accountNameJS = document.getElementById('accName').value;       
 
        sample.getAccount( accountNameJS,
 
        function(result, event)
        {
            if (event.status)
 
            {
                // demonstrates how to get ID for HTML and Visualforce tags
                document.getElementById("{!$Component.theBlock.thePageBlockSection.theFirstItem.accId}").innerHTML = result.Id;
                document.getElementById("{!$Component.theBlock.thePageBlockSection.theSecondItem.accNam}").innerHTML = result.Name;
            }
 
            else if (event.type === 'exception')
 
            {
                document.getElementById("errors-js").innerHTML = event.message;
            } else
 
            {
                document.getElementById("errors-js").innerHTML = event.message;
            }
        }, {escape:true});
    }
    </script>
    Account Name :<input id="accName" type="text" />
    <button onclick="getAccountJS()">Get Account</button>
    <div id="errors-js"> </div>
    <apex:pageBlock id="theBlock">
        <apex:pageBlockSection id="thePageBlockSection" columns="2">
            <apex:pageBlockSectionItem id="theFirstItem">
                <apex:outputText id="accId"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem id="theSecondItem" >
                <apex:outputText id="accNam" />
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>


Apex Controller:


global class sample
 
{
    public String accountName { get; set; }
    public static Account account { get; set; }
    public sample() { }
   
 
    @RemoteAction
    global static Account getAccount(String accountName)
 
    {
        account = [select id, name, phone, type, numberofemployees from Account where name = :accountName ];
        return account;
    }
}


Ouptut:
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicTZJLqd6rDyXk2rL7abnTQfOOPe3bxZKSvbsYGXAe1sgYUe9g7xyRSwAPa4bgrxW61l2MVRheaMTZ22ic59pwgw1xMbuSa4p4zezz4J3qJHkRLwb3Kh6yhrvFyeKaZhyphenhyphenRqGn6uOPx0pAX/s1600/Remote+Action.png