Wednesday, 3 June 2015

Salesforce Developer Stuff: Understanding Trigger Context Variable

Salesforce Developer Stuff: Understanding Trigger Context Variable:                                       

https://www.forcewizard.com/blog/understanding-trigger-context-variable


    When I started my carrier as Salesforce developer.when I wrote my first trigger code, I always use to hear


trigger TriggerContextVarDemo on Book__c (before insert, after insert, before update, after update)
{
      /*This if block will run only before insert.It means mean time of
        the records going to save into salesforce database*/
       
      if(Trigger.isInsert && Trigger.isBefore){
            System.debug('****Insert before Trigger.new******'+Trigger.new);
            /*Trigger.New-new versions of the sObject records.
              e.g. It will work both insert and update for insert every record will be new.
              creating 1st record on book it will return 1st record value,creating 2nd will return second
              record value like that it will go.
            */
           
            System.debug('****Insert before Trigger.old******'+Trigger.old);
            /*Trigger.Old-old versions of the sObject records.
              this will not work both before and after insert.Then always the value will be null.
               because there will be no old version(old value) of the record, all are new record.
               old vesion in the sence while updating any record you may change some field value to new value .
               that old value with record will be in trigger.old.
            */
           
            System.debug('****Insert before Trigger new map******'+Trigger.newMap);
            /*Trigger.newMap-map of IDs to the new versions of the sObject records.
              It will work only on after insert because new map must store the Id and record.
              In the before insert how the id will be generated.
              so always it will return null.
            */
              System.debug('****Insert before Trigger old map******'+Trigger.oldMap);
              /*It will not return any value always its null*/
    }
 
    /*This if block will only run after succesful insert of records into salesforce database*/
       
    if(Trigger.isInsert && Trigger.isAfter){
            System.debug('****Insert after Trigger.new******'+Trigger.new);
            /*It will return same value as explained before insert of Trigger.new*/
           
            System.debug('****Insert after Trigger.old******'+Trigger.old);
            /* this will not work both before and after insert.Then always the value will be null.
               because the will be no old version(old value) of the record all are new record
               old vesion in the sence while updating any record you may change some field value to new value .
               that old value will be in trigger.old
            */
            System.debug('****Insert after Trigger new map******'+Trigger.newMap);
           /*It will return new map value as explained before insert of Trigger.newMap
             because id is generated for new record that is saved.
           */
           System.debug('****Insert after Trigger old map******'+Trigger.oldMap);
            /*It will not return any value always its null */
    }
   
      /*This if block will run only before update.It means mean time of
        the records going to update into salesforce database*/
       
    if(Trigger.isupdate && Trigger.isBefore){
            System.debug('****update before Trigger.new******'+Trigger.new);
            /*As I explained previously Trigger.new is the new version of sobject record
              create first book record with price 200 then edit and change price 600 and click save
              now this Trigger.new will contain the new value of record that means price  value with 600 record
           */
            System.debug('****update before Trigger.old******'+Trigger.old);
             /*As I explained previously Trigger.new is the new version of sobject record
              Edit the created book record and change price from 200 to 600 and click save
              now this Trigger.old will contain the old value of record that means price value with 200 record
           */
            System.debug('****update before Trigger new map******'+Trigger.newMap);
            /*As I explained previously Trigger.new is the new version of sobject record
              create first book record with price 200 then edit and change price 600 and click save
              now this Trigger.newMap will contain the newmap value of record that means price  value with 600 record
           */
            System.debug('****update before Trigger old map******'+Trigger.oldMap);
             /*As I explained previously Trigger.new is the new version of sobject record
              Edit the created book record and change price from 200 to 600 and click save
              now this Trigger.oldMap will contain the oldMap value of record that means price value with 200 record
           */
   }
   
    /*This if block will only run after succesful update of records into salesforce database*/
    //This block will return same value as returned in is before update
     if(Trigger.isupdate && Trigger.isAfter){
            System.debug('****update after Trigger.new******'+Trigger.new);
            System.debug('****update after Trigger.old******'+Trigger.old);
            System.debug('****update after Trigger new map******'+Trigger.newMap);
            System.debug('****update after  Trigger old map******'+Trigger.oldMap);

    }

}

Thursday, 15 January 2015

Bob Buzzard Blog: Managing a list of New Records in Visualforce

Bob Buzzard Blog: Managing a list of New Records in Visualforce: This week's post concerns bulk creation of an unknown number of sobject records - Accounts, for example. In this situation I want to dis......

Wednesday, 14 January 2015

Test class for visualforce (VF) page with URL parameters

//Test class for visualforce (VF) page with URL parameters
/*
Here i am creating test class for standard controller as well when we want to pass parameters in URL 
*/
@isTest
public class VFTestClass
{

Monday, 12 January 2015

visualforce with radio button

/*
Here i want to demonstrate vf page with radio button on standard object USER and selected value will be save in custom object (TestUser__c )
*/

Monday, 27 October 2014

SOQL query of Knowledge Articles

SOQL query of Knowledge Articles


SELECT ArticleNumber,ArticleType,CreatedById,UrlName,VersionNumber FROM Offer__kav where PublishStatus = 'Online' AND Language ='en_US'


Output:- 


ArticleNumberArticleTypeCreatedByIdUrlNameVersionNumber
1000001008Offer__kav00590000001DZcHAAWMy-first-Article1
2000001004Offer__kav00590000001DZcHAAWHow-can-I-cancel-my-flight1
3000001006Offer__kav00590000001DZcHAAWIs-my-passport-valid1
4000001000Offer__kav00590000001DZcHAAWfirst-offer11
5000001001Offer__kav00590000001DZcHAAWsecond-offer21





SOQL query of Knowledge Articles

Monday, 11 August 2014

Generate report for all test classes In salesforce (SFDC)

 Generate report for all test classes in salesforce (SFDC)

/*
    to meet this requirement i created a scheduler class (TestResultScheduler ), a main class (TestResultGenerator ) , and one future method class (testLog) to retrieve all test class status and email the result.

*/

Thursday, 26 June 2014

Create a report using apex class and send as an Email Attachment

/*
Create a customize report to send specific user via email, using apex class and scheduler in email attachment.

Suppose we have object Job__c and specific user need daily email how much Job everyday created my his team. (here I am defining very simple scenario but it is very useful for complex and complected report)

I am using here single object Job__c and 3 apex classes to meet this requirement.
also i try to cover following scenario

a batch Apex job that contains 20,000 records and is executed without the optional scope parameter is considered five transactions of 200 records each
*/

Tuesday, 6 May 2014

Bob Buzzard Blog: Reading Barcodes in Salesforce1

Bob Buzzard Blog: Reading Barcodes in Salesforce1: A little over a month ago I wrote a post on Reading QR Codes in Salesforce1 . At the time I added an entry to my todo list to investigate ba...

Monday, 5 May 2014

Handle ENTER event key press in visual force page

 Handle ENTER event key press in visual force page OR
 Preventing default form submission and calling an action instead

<apex:page standardController="account" >
       <script type="text/javascript">      
     
   function submitListener(e){
    var keynum = 0;
    if (window.event){
        keynum = window.event.keyCode;
    }
    else if (e.which){
        keynum = e.which;
    }
    // Here we check whether the Enter button was pressed
    if (keynum == 13){
        onFormSubmit();
    }
}

 </script>
<apex:form id="orderSearchForm" onkeyup="submitListener(event)">

<apex:actionFunction name="onFormSubmit"    action="{!save}"  />
        <apex:pageBlock title="View AND Edit">
<apex:pageBlockButtons >   
                        <!--   --> <apex:commandButton action="{!cancel}" value="Cancel"/>   
              <apex:commandButton action="{!save}" value=":-)"  />

              <apex:commandButton action="{!cancel}" value="Cancel"/>
                
</apex:pageBlockButtons>
            
<apex:pageblockSection columns="2">
       <apex:pageblockSection columns="1">
                  Name:  {!account.name}
                </apex:pageblockSection>
                            <apex:pageblockSection columns="1">
                  <apex:inputField value="{!account.name}" />
                  <apex:inputField value="{!account.type}" />
                  <apex:inputField value="{!account.industry}"/>
                  <apex:inputField value="{!account.fax}" />
                </apex:pageblockSection>
            </apex:pageblockSection>            
        </apex:pageBlock>
    </apex:form>
</apex:page>






Wednesday, 30 April 2014

Rendering a page block section based on conditions


set dynamically, Multiple Conditions for Rendered Attribute
set dynamically ,Rendering a page block section based on conditions

/*
   Consider one  custom object:-  A__c

and A__c has fields :-  First_Name__c, Last_Name__c, Age__c and Contact_Details__c

Suppose when user opted First then user can see 4 fields but When user opted Second then user can see only two fields.

*/


/*Code for Visualforce (Vf)  page*/

<apex:page standardController="A__c" extensions="renderedconditionalcntrl"  >

<apex:form >

<apex:pageBlock title="Rederer with condition">

        <apex:outputLabel value="My Options"> </apex:outputLabel>
         <apex:inputField label="My Option" value="{!Aobj.My_Option__c}">
             <apex:actionSupport event="onchange" action="{!changeevent}"  status="displaystatus" immediate="false" reRender="c1"/>            
        </apex:inputField>

<apex:pageBlockSection id="c1" columns="1" collapsible="false">

        <apex:pageBlockSectionitem rendered="{! IF( isFirstDiv || isSecondDiv,true,false)}" >
     
                <apex:outputLabel value="First Name"> </apex:outputLabel>
             <apex:inputField label="First Name" value="{!Aobj.First_Name__c}" />
        </apex:pageBlockSectionitem>
     
        <apex:pageBlockSectionitem rendered="{! IF( isFirstDiv || isSecondDiv,true,false)}" >
                <apex:outputLabel value="Last Name"></apex:outputLabel>
                          <apex:inputField label="Last Name" value="{!Aobj.Last_Name__c}" />
        </apex:pageBlockSectionitem>
     
     
          <apex:pageBlockSectionitem rendered="{!isFirstDiv}" >
                <apex:outputLabel value="Age"></apex:outputLabel>
                        <apex:inputField label="Age" value="{!Aobj.Age__c}"/>
        </apex:pageBlockSectionitem>
     
        <apex:pageBlockSectionitem rendered="{!isFirstDiv}">
                <apex:outputLabel value="Contact Number"></apex:outputLabel>
                          <apex:inputField label="Contact Number" value="{!Aobj.Contact_Details__c}"/>
        </apex:pageBlockSectionitem>

     

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

/*Code for Apex class (extensions)*/


public with sharing class renderedconditionalcntrl {

    public boolean isFirstDiv{get;set;} //first flag
    public boolean isSecondDiv{get;set;} //second flag
    public A__c Aobj{get;set;}
    public renderedconditionalcntrl(ApexPages.StandardController controller) {
    
     Aobj = new A__c();
    }
    
    public void changeevent(){
    System.debug('~~~~~~~~~~~~~~~~~~~~~' + Aobj.My_Option__c );
    
    //Set your flag 
    
       isFirstDiv = false;     
       isSecondDiv  = false; 
       
        if(Aobj.My_Option__c  == 'First')
        isFirstDiv = true;
        
         else if(Aobj.My_Option__c  == 'Second'){            
            isSecondDiv  = true;             
        }
        else {
            isFirstDiv = false;
              isSecondDiv  = false;
        }
        
    }

}


Refer following Images for the above implementation :- 


















Tuesday, 18 February 2014

Salesforce: Salesforce - "System mode & User mode" and "With s...

Salesforce: Salesforce - "System mode & User mode" and "With s...: This topic has always been part of discussion where the answer is not satisfactory and convinced to others.  Using this article, I wi...

Tuesday, 4 February 2014

SFDC 401 notes: Get Started for SFDC 401

SFDC 401 notes: Get Started for SFDC 401: 1) For a Contact object, All users should be able to see all the fields except User Y who should net be able to see Mobile No. Field. This ...

Monday, 3 February 2014

Batch Apex in salesforce

/* you need to create a class and to schedule that class write one more class
*/

Class Name:- batchAccountUpdate



global class batchAccountUpdate implements Database.Batchable <sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,Name FROM Account';
        return Database.getQueryLocator(query);
    }
  
    global void execute(Database.BatchableContext BC, List<Account> scope)
    {
         for(Account a : scope)
         {
             a.Name = a.Name + 'L';
             update a;
         }
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}

// to schedule class: - scheduleExpireNotify

global class scheduleExpireNotify implements Schedulable
{
    global void execute(SchedulableContext sc)
    {
        batchAccountUpdate en = new batchAccountUpdate();
        Database.executeBatch(en);
    }
}


Sunday, 2 February 2014

Inline Editing using Visualforce page

<apex:page standardController="Merchandise__c" recordSetVar="acts">
<apex:form >
<apex:pageBlock title="Merchandise">

        <apex:pageblockTable value="{!acts}" var="a">
            <apex:column value="{!a.name}"/>
        </apex:pageblockTable>


// code for inline Editing

 <apex:inlineEditSupport event="ondblclick" showOnEdit="saveButton, cancelButton" hideOnEdit="editButton"/>
 </apex:pageBlock>
</apex:form>
</apex:page>