Monday, April 29, 2019

What is difference between insert and database.insert in salesforce apex

DML Statements vs. Database Class Methods
Apex offers two ways to perform DML operations: using DML statements or Database class methods. This provides flexibility in how you perform data operations. DML statements are more straightforward to use and result in exceptions that you can handle in your code. This is an example of a DML statement to insert a new record.
// Create the list of sObjects to insert
List<Account> acctList = new List<Account>();
acctList.add(new Account(Name='Acme1'));
acctList.add(new Account(Name='Acme2'));

// DML statement
insert acctList;
This is an equivalent example to the previous one but it uses a method of the Database class instead of the DML verb.
// Create the list of sObjects to insert
List<Account> acctList = new List<Account>();
acctList.add(new Account(Name='Acme1'));
acctList.add(new Account(Name='Acme2'));

// DML statement
Database.SaveResult[] sr = Database.insert(acctList, false);

// Iterate through each returned result
for (Database.SaveResult sr : srList) {
    if (sr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully inserted account. Account ID: ' + sr.getId());
    }
    else {
        // Operation failed, so get all errors               
        
for(Database.Error err : sr.getErrors()) {
            System.debug('The following error has occurred.');                   
            System.debug(err.getStatusCode() + ': ' + err.getMessage());
            System.debug('Account fields that affected this error: ' + err.getFields());
        }
    }
}
One difference between the two options is that by using the Database class method, you can specify whether or not to allow for partial record processing if errors are encountered. You can do so by passing an additional second Boolean parameter. If you specify false for this parameter and if a record fails, the remainder of DML operations can still succeed. Also, instead of exceptions, a result object array (or one result object if only one sObject was passed in) is returned containing the status of each operation and any errors encountered. By default, this optional parameter is true, which means that if at least one sObject can’t be processed, all remaining sObjects won’t and an exception will be thrown for the record that causes a failure.
The following helps you decide when you want to use DML statements or Database class methods.
·       Use DML statements if you want any error that occurs during bulk DML processing to be thrown as an Apex exception that immediately interrupts control flow (by using try. . .catch blocks). This behavior is similar to the way exceptions are handled in most database procedural languages.
·       Use Database class methods if you want to allow partial success of a bulk DML operation—if a record fails, the remainder of the DML operation can still succeed. Your application can then inspect the rejected records and possibly retry the operation. When using this form, you can write code that never throws DML exception errors. Instead, your code can use the appropriate results array to judge success or failure. Note that Database methods also include a syntax that supports thrown exceptions, similar to DML statements.
Note
https://www.salesforce.com/us/developer/docs/apexcode/Content/images/img/help/helpNote_icon.gifMost operations overlap between the two, except for a few.
·      The convertLead operation is only available as a Database class method, not as a DML statement.
The Database class also provides methods not available as DML statements, such as methods transaction control 

What are trigger context variables in salesforce

salesforce lightning,
Variable
Usage
isExecuting
Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.
isInsert
Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.
isUpdate
Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.
isDelete
Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.
isBefore
Returns true if this trigger was fired before any record was saved.
isAfter
Returns true if this trigger was fired after all records were saved.
isUndelete
Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)
new
Returns a list of the new versions of the sObject records.
Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.
newMap
A map of IDs to the new versions of the sObject records.
Note that this map is only available in before update, after insert, and after update triggers.
old
Returns a list of the old versions of the sObject records.
Note that this sObject list is only available in update and delete triggers.
oldMap
A map of IDs to the old versions of the sObject records.
Note that this map is only available in update and delete triggers.
size
The total number of records in a trigger invocation, both old and new.


Price Book Entry Fields

Field
Description
Active
Whether the price book entry (product and list price) is active and can be added to an opportunity or quote.
Created By
The name of the user who created the price book entry, including the date and time of creation.
Currency
This field is available only for Salesforce orgs with multiple currencies enabled and represents the currency to use for the price book entry (product and list price).
Last Modified By
The name of the user who last saved the price book entry record.
List Price
The price of the product within the price book, including currency.
Price Book
The price book that contains the price for this entry.
Product
The product’s name.
Product Code
The internal code or product number that’s used to identify the product.
Standard Price
The standard price for the product. This field is derived and doesn’t exist on the underlying PricebookEntry standard object.
Use Standard Price
Whether the p


What is difference between soap and rest api


Difference between SOAP and REST APIs
SOAP API
REST API
Simple Object Access Protocol
Representational State Transfer
It is based on standard XML format
It is based on URI
It works with WSDL
It works with GET, POST, PUT, DELETE
Works over with HTTP, HTTPS, SMTP, XMPP
Works over with HTTP and HTTPS


How to create extension controller for visual force page and get url params


get current record id salesforce
Visualforce Page:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<apex:page standardController="Account" extensions="CurrentRecordIdDemoController">
  <apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection title="Current account record Id is : {!currentRecordId}" collapsible="false">
            <apex:outputField value="{!acc.name}"/>
            <apex:outputField value="{!acc.AccountNumber}"/>
            <apex:outputField value="{!acc.Type}"/>
            <apex:outputField value="{!acc.Industry}"/>
        </apex:pageBlockSection>
         
        <apex:pageBlockSection title="Testing parameter" collapsible="false">
            Name is <b>{!parameterValue}</b>
        </apex:pageBlockSection>
         
    </apex:pageBlock>
  </apex:form>
</apex:page>
Apex Code:
1
2
3
4
5
6
7
8
9
10
11
public class CurrentRecordIdDemoController{
public String currentRecordId {get;set;}
public String parameterValue {get;set;}
public Account acc{get;set;}

    public CurrentRecordIdDemoController(ApexPages.StandardController controller) {
        currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
        acc = [select id ,name, AccountNumber, Type, Industry from
 Account where id =: currentRecordId ];
        parameterValue = ApexPages.CurrentPage().getparameters().
get('nameParam');
    }
}