Saturday, 30 November 2013

Roll Up Summary Fields using Trigger After events

Roll Up Summary Fields using Trigger After events :

trigger My_MonthsRollUp_US on THD_Sales__c (after insert, after update, after delete, after undelete) {
    Map<Id,Account> updateAccounts = new Map<Id,Account>();
    Set<Id> updateAccountIds = new Set<Id>();    
  // If we are inserting, updating, or undeleting, use the new ID values
  if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete) {
    if(trigger.new != null)
        for(THD_Sales__c thdsales:Trigger.new)
            if(thdsales.SFDC_Account_ID__c != '0017000000ZWk80AAD')
              updateAccounts.put(thdsales.SFDC_Account_ID__c,null);
  }
  // If we are updating, some accounts might change, so include that as well as deletes
  if(Trigger.isUpdate || Trigger.isDelete) {
    if(trigger.old != null)
        for(THD_Sales__c thdsales:Trigger.old)
            if(thdsales.SFDC_Account_ID__c != '0017000000ZWk80AAD')
              updateAccounts.put(thdsales.SFDC_Account_ID__c,null);
  }
  // Do not create a record for null field
  updateAccounts.remove(null);  
  // Create in-memory copies for all accounts that will be affected
  for(Id accountId:updateAccounts.keyset()) 
    updateAccounts.put(accountId,new Account(id=accountId,
    Total_My_SO_Sales_January__c=0, Total_My_SO_Sales_February__c=0,Total_My_SO_Sales_March__c=0,
    Total_My_SO_Sales_April__c=0,Total_My_SO_Sales_May__c=0,Total_My_SO_Sales_June__c=0,
    Total_My_SO_Sales_July__c=0,Total_My_SO_Sales_August__c=0,Total_My_SO_Sales_September__c=0,
    Total_My_SO_Sales_October__c=0,Total_My_SO_Sales_November__c=0,Total_My_SO_Sales_December__c=0,
    Total_Cash_Carry_Sales_January__c=0, Total_Cash_Carry_Sales_February__c=0,Total_Cash_Carry_Sales_March__c=0,
    Total_Cash_Carry_Sales_April__c=0,Total_Cash_Carry_Sales_May__c=0,Total_Cash_Carry_Sales_June__c=0,
    Total_Cash_Carry_Sales_July__c=0,Total_Cash_Carry_Sales_August__c=0,Total_Cash_Carry_Sales_September__c=0,
    Total_Cash_Carry_Sales_October__c=0,Total_Cash_Carry_Sales_November__c=0,Total_Cash_Carry_Sales_December__c=0
    ));
    
    // Run an optimized query that looks for all accounts that meet the if/then criteria
    map< string, list< schema.sobjectfield > > fieldMap =
        new map< string, list< schema.sobjectfield > > {
            'Special Order' =>
                new list< schema.sobjectfield > {
                    null,
                    Account.Total_My_SO_Sales_January__c,
                    Account.Total_My_SO_Sales_February__c,
                    Account.Total_My_SO_Sales_March__c,
                    Account.Total_My_SO_Sales_April__c,
                    Account.Total_My_SO_Sales_May__c,
                    Account.Total_My_SO_Sales_June__c,
                    Account.Total_My_SO_Sales_July__c,
                    Account.Total_My_SO_Sales_August__c,
                    Account.Total_My_SO_Sales_September__c,
                    Account.Total_My_SO_Sales_October__c,
                    Account.Total_My_SO_Sales_November__c,
                    Account.Total_My_SO_Sales_December__c
                },
           'Cash & Carry' =>
                new list< schema.sobjectfield > {
                    null,
                    Account.Total_Cash_Carry_Sales_January__c,
                    Account.Total_Cash_Carry_Sales_February__c,
                    Account.Total_Cash_Carry_Sales_March__c,
                    Account.Total_Cash_Carry_Sales_April__c,
                    Account.Total_Cash_Carry_Sales_May__c,
                    Account.Total_Cash_Carry_Sales_June__c,
                    Account.Total_Cash_Carry_Sales_July__c,
                    Account.Total_Cash_Carry_Sales_August__c,
                    Account.Total_Cash_Carry_Sales_September__c,
                    Account.Total_Cash_Carry_Sales_October__c,
                    Account.Total_Cash_Carry_Sales_November__c,
                    Account.Total_Cash_Carry_Sales_December__c
                } 
        };     

   for(THD_Sales__c thdsales : [SELECT Id, Month_Year__c, POS_Order_Type__c, Amount__c, SFDC_Account_ID__c
   FROM  THD_Sales__c WHERE SFDC_Account_ID__c IN :updateAccounts.keySet( ) AND 
   POS_ORDER_TYPE__C IN ('Special Order','Cash & Carry') AND Month_Year__c != null]){ 
                            
        updateAccounts.get( thdsales.sfdc_account_id__c ).put(fieldmap.get( thdsales.pos_order_type__c )[thdsales.month_year__c.month()],
            ( Decimal )( updateAccounts.get( thdsales.sfdc_account_id__c ).get( fieldmap.get( thdsales.pos_order_type__c )[thdsales.month_year__c.month()]))+thdsales.Amount__c );
    }
  // Update all the accounts with new values.
  Database.update(updateAccounts.values());
}

Sunday, 16 June 2013

Map usage in Apex

Map usage in APEX:

public with sharing class MapUsage {
        
        public static void mapUse(){
        
            Map<Integer, String> mp = new Map<Integer, String>();
            // kEy is Integer
            // value is String
            
            mp.put(1221,'Mahi');
            //mp.put(1226,'Shyam');
            mp.put(1211,'Rajesh');
            mp.put(1255,'Kiran');
            
            System.debug('---------------Elements in the map are ------------>'+mp);
            System.debug('---------------Size of the map is-------4---------->'+mp.size());
            System.debug('------------Keys in the map are ----------->'+mp.keyset());
            System.debug('------------Values in the map are ----------->'+mp.values());
            
            System.debug('------------------'+mp.get(1211));
            
            mp.remove(1226);
            
            mp.put(1211,'Charan');
            mp.put(1248,'Pavan');
            mp.put(1222,'Kiran');
            
            System.debug('---------------Elements in the map are ------------>'+mp);
            System.debug('---------------Size of the map is-------5---------->'+mp.size());
            
            for(Integer i : mp.keyset()){
                System.debug('-----Key--------'+i);
                System.debug('-----value---------'+mp.get(i));
            }
            
            for(String s: mp.values()){
                System.debug('----------------'+s);
            }
            
        }        
}

Set usage in Apex

Set usage in APEX:

public with sharing class SetUsage {
    
    public void setUse(){
        
        Set<Integer> st = new Set<Integer>();
        
        st.add(1234);
        st.add(1248);
        st.add(1225);
        st.add(1252);
        
        System.debug('----------------Elementa in the set are --------------'+st);
        System.debug('----------------Size of Elementa in the set are ------4--------'+st.size());
        
        st.remove(1252);
        System.debug('----------------Size of Elementa after removing an element in the set are ------3--------'+st.size());
        
        
        st.add(1248);
        st.add(1252);
        st.add(1221);
        System.debug('----------------Size of Elementa in the set after adding duplciates are ------5--------'+st.size());      
        
        Set<Integer> newst = st.clone();
        System.debug('----------------Size of Elementa in the new set are -------5-------'+newst.size());
        
        st.clear();
        System.debug('-----------------NEw set is EMpty or not--------false--------'+newst.isEmpty());
        
        for(Integer i: newst){
            System.debug('----------------->'+i);
        }        
    }
    
}

List usage in Apex

List Usage in APEX:

public with sharing class ListUsage {
        
        public void listUse(){
            
            // Initialising a list
            List<String> lst = new List<String>();
            
            // Adding elemenst into list
            lst.add('ABC');
            lst.add('DEF');
            lst.add('GHI');
            lst.add('JKL');
            
            System.debug('---------Elements in the list are-------------->'+lst);
            System.debug('----------Size of the Elements are-----4-------->'+lst.size());
            System.debug('-----------Element in 2nd index--------GHI-------->'+lst[2]);

            lst.remove(1);
            System.debug('---------Elements in the list after removing are-------------->'+lst);
            System.debug('----------Size of the Elements after removing are----3--------->'+lst.size());
            
            lst.add('MNO');
            lst.add('GHI');
            lst.add('DEF');
            lst.add('1248');
            
            System.debug('---------Elements in the list after adding duplicate values are-------------->'+lst);
            System.debug('----------Size of the Elements after adding duplicate values are--------7----->'+lst.size());
            
            List<String> newlst = lst.clone();
            System.debug('---------Elements in the new list are-------------->'+newlst);
            System.debug('----------Size of the Elements in new list are-------7------>'+newlst.size());
            
            lst.clear();// removes all the elements in the list
            System.debug('-----------------List is Empty or not-------true----------->'+lst.isEmpty());
            System.debug('-----------------New List is Empty or not-------false----------->'+newlst.isEmpty());
            
            //Indexing for loop 
            for(integer i=0; i<newlst.size(); i++){
                System.debug('-----Index------->'+newlst[i]);
            }
            
            //For Each Loop
            for(String s: newlst){
                System.debug('-----For Each---------->'+s);
            }
            
            
        }
        
        
        
}

Saturday, 1 June 2013

Using Hierarchy Custom Settings in Salesforce

1. Create a hierarchy custom setting like below

- As per the above screen shot create a custom field of data type check box
Note: App Setup > Develop > Custom Settings (To open custom settings)

2. Click on Manage button

- As per the above screen shot set the OWD value for isBypassed as 'False'
- Add two users by clicking on New button, set isBypassed as 'True' for one user and 'False' for another user

3. Create a page called 'Change Owner'
<apex:page tabStyle="Case" standardController="Case">
<apex:form >
<apex:sectionHeader title="Change Case Owner"/>
<p>This screen allows you to transfer cases from one user or queue to another. When you transfer ownership, the new owner will own:</p>
<ul><li>all open activities (tasks and events) for this case that are assigned to the current owner</li></ul>
<p>Note that completed activities will not be transferred. Open activities will not be transferred when assigning this case to a queue.</p>
<apex:pageBlock mode="Edit">
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Cancel"/>
</apex:pageBlockButtons>
<br/><apex:pageBlockSection title="Select New Owner" collapsible="false" columns="3">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Owner"></apex:outputLabel>
<apex:inputField value="{!Case.ownerId}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

- Design a visualforce page from the above code

4. Create custom button for the Case Object like below:


if ({!$Setup.Disable_Change_Owner__c.isBypassed__c}) {

window.location = "/apex/changeowner?id={!Case.Id}";

}

else {

alert("You don't have the permission");

}


- Use the above script for OnClick JavaScript
- Add the above custom button 'Change Owner' to Case Page Layout.
- "{!$Setup.Disable_Change_Owner__c.isBypassed__c}" is the way to refer the hierarchy custom setting in JavaScript.
- According to the above button one user can access the page and navigate to the 'Case Owner' page

- Another user is not able to access the page, it will throw JavaScript alert


Note: Above scenario helps to create custom change owner functionality for the case record

***
We can create multiple fields for the hierarchy custom settings and we can assign different values according to the business requirement.
***



Monday, 15 April 2013

Change Case Owner VF Page


<apex:page tabStyle="Case" standardController="Case" extensions="ChangeOwner">
<apex:form >
<apex:sectionHeader title="Change Case Owner"/>
<p>This screen allows you to transfer cases from one user or queue to another. When you transfer ownership, the new owner will own:</p>
<ul><li>all open activities (tasks and events) for this case that are assigned to the current owner</li></ul>
<p>Note that completed activities will not be transferred. Open activities will not be transferred when assigning this case to a queue.</p>
<apex:pageBlock mode="Edit">
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Cancel"/>
</apex:pageBlockButtons>
<br/><apex:pageBlockSection title="Select New Owner" collapsible="false" columns="3">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Owner"></apex:outputLabel>
<apex:inputField value="{!Case.ownerId}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>


public class ChangeOwner {
   
    public ChangeOwner(ApexPages.StandardController controller) {

    }

}

Saturday, 2 March 2013

Products

* contains standard price book and we can create multiple custom price books.
* without standard price book, it is not possible to create custom price book.
* Price Book: Same product can have multiple prices based on the different locations. Example take laptops which are cheap in US but INIDA they have more cost like that.
* Which is the child object for the opportunity.


Lead

* After converting the lead it is not possible to see the lead through user interface.
* While running the reports we can see the converted leads.
*

Saturday, 16 February 2013

Update a record on which we are writing the trigger

Use before insert or before update if you need to update or insert the record on which you are writing the trigger.

Trigger populate_userlookup on Book__c (before insert, before update) {
  for(Book__c b : Trigger.new) {
   b.buyer__c = b.ownerId;
 }
}

Note: No need to use DML statements like insert or update in case of before triggers.

Upserting data from Cast Iron to Salesforce

 

1. Create an End Point to salesforce.com

Click on Projects Tab>Right click on Endpoints>Create>Salesforce.com as shown above
  

 2.Configure the Endpoint like below
Give Salesforce Username> Give Password along with Security Token>Select Login Normally>Click on Test Connetion  

It will display like below:

3. Select upsert activity from saleforce

Configure the activity like below


Pick appropriate end point as you already created

 
 Select Configure>Give Object type and External Id

Select Retry options

Map Inputs like below

Before that you need to create orchestration variables

 I have created four variables of String Type.

Each and every variable should  be initialized.

Output values if necessary map with orchestration variables


Click on Verify Activity

Like above it will display the results in instances

4. Now check salesforce wether record got inserted or updated.

Based on the External Id, If that id already present in saleforce then it will insert record, if the external id field value not present in saleforce then it will insert the value.


Sunday, 10 February 2013

CAMPAIGNS


Integration between Cast Iron and Microsoft SQL Server 2005

Connecting Cast Iron with Salesforce:
 
Inserting data from Cast Iron to Salesforce:

Inserting data from Cast Iron to Microsoft SQL Server 2005:

Inserting data from Microsoft SQL Server 2005 to Salesforce:

Inserting data from Salesforce to Microsoft SQL Server 2005:



Sunday, 3 February 2013

Difference between Interface and Abstract Class


Interface
Abstract Class
Declare with keyword Interface
Declare with keyword Abstract
It will not contain constructor
It may or may not contain constructor
By default all methods are public abstract methods.
It contains abstract methods and non abstract methods.
It contains only final variables
It contains final variables and instance variables
We can implement multiple interfaces
We can inherit only one abstract class

Tuesday, 15 January 2013

Difference between SOAP and Restful Webservice

* REST and SOAP are the two web service frameworks available on the Force.com platform. 
* The SOAP API uses authentication with username and password, and communicates using XML messages. There are different API's to work with the Enterprise, Partner, Bulk, Metadata etc. 
*The REST API uses HTTP to authenticate, it uses a token authentication and can use OATH to authenticate, it communicates using JSON messages. 
*REST API is generally lighter weight than the SOAP API, works well with mobile applications especially.

Labels