Wednesday 22 July 2015

Test class Factory design pattern

Test class Factory design pattern

public class UtileTesClass {



    /*-----setup data for user----*/
    public Static User setupUser()
    {
        Profile prof = [select id from profile where name = 'System Administrator'];
        User user = new User(alias = 'standt', email = 'my123_123@gmail.com', emailencodingkey = 'UTF-8',
                         lastname = 'Test', languagelocalekey = 'en_US', localesidkey = 'en_US',
                         profileid = prof.Id, timezonesidkey = 'America/Los_Angeles',
                         username = 'testuser_123@gmail.com');
        insert user;
        return user;
    }

   
    /*----------------Setup data for Account----*/
    public static Account setupAccount(user MyMnger)
    {      
        Account  acc= new Account();  
        acc.Name='Test Account';
        acc.ShippingStreet ='Royal Street';
        acc.ShippingCity='London';
        acc.ShippingState='UK';
        acc.ShippingPostalCode ='123456';
        acc.ShippingCountry='UK';
        return acc;
    }

     
    /*----------------Setup data for Case ----*/
    public static case setupCase(Account acc)
    {      
        Case cs = new Case();
        cs.status='Open';
        cs.Subject='Demo Case';
        cs.Origin='Email';
        cs.AccountId = acc.id;
        cs.Reason = 'Defects';  
        return cs;
    }
   
       
}

------------------------------------------------- Test class --------------------------------------

@isTest(SeeAllData=false)

public class TestclassFactory{

    static testmethod void testCCREscalations (){
    User CCRUser =  UtileTesClass.setupUser();

    Test.startTest();

        system.runAs(CCRUser) {
        List<user> selectedusers = new List<User>();
        String CommonError ='';
        Integer ErrorFlag = 0;

         

            // setup Account
            Account account = UtileTesClass.setupAccount(CCRUser);
            insert account;
           
            List<account> listAccount = [select id,name from Account];
            System.assertEquals(1, listAccount.size());

            Case cas = UtileTesClass.setupCase(account);
            insert cas;          

        }
    Test.stopTest();
    }                

 
    }

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 )
*/