Sunday 13 April 2014

How to write a trigger on Account ?

Hi Guys!!!  I just a new scenario in which we have to write a trigger. it will be a good exercise for us to write a trigger. As trigger is also one of the important parameter. So lets get started.

Here, is the scenario:-


My task is 
in custom settings i have created 2 fields 1) prefix data type is text like abcd. 2)sample is number data type like 0001 ok. now in account object i have created a 1 check box and 1 text field. The task is when ever click on save button the text box needs to be generate a id this id comes from custom settings fields combination of prefix and number like(abcd00001) after this number should be increased by 1 ex:0002 and need to be stored on custom setting field sample because  it needs to be incremented by 1. when next time I am going to create a new record.

Now , lets hit it by step by step. 

1. First we have to create a custom setting. And it shpuld have two fields named sa,ple amd prefix.
2. Create a custom field on Account name Sampleprefix.
3. The idea is to have the value from custom setting and append it to the one in Account.

We will start with after insert and after update. I have already wriiten in my personal org.

Here is the trigger:-



trigger CustomSettings on Account (before insert,before update) {
    if(trigger.isInsert){
    for(Account ac: trigger.new){
        AnkushTest__prefix__c acs =AnkushTest__prefix__c.getOrgDefaults();
        string st;
        st= String.valueOf(acs.AnkushTest__prefix__c)+Integer.valueOf(acs.AnkushTest__sample__c+1);
        system.debug(st);
        ac.AnkushTest__samplePrefix__c=st;
        
        acs.AnkushTest__sample__c=integer.valueof(st.substring(4,5));
        update acs;
        
        }
    }
    if(trigger.isUpdate){
        for(Account acc: trigger.new){
        
        AnkushTest__prefix__c acs =[select AnkushTest__sample__c,AnkushTest__prefix__c, name from AnkushTest__prefix__c limit 1];

        string st;
        st= acs.AnkushTest__prefix__c+integer.valueof(acs.AnkushTest__sample__c+1);
        system.debug('))))))))))'+st);
        acc.AnkushTest__samplePrefix__c=st;
       
        acs.AnkushTest__sample__c=integer.valueof(st.substring(4,5));
        update acs;
        }
       
    }    

}



Saturday 12 April 2014

How to get Contacts associated to a particular opportunity?

It will be a good practice for you to get aware of how apex and visualforce works. 

So lets get started .... Lemme create a very simple scenario for you . Say, I want to get all the contacts that are associated to an opportunity. 

remember:- Before writing any class and visualforce first try to understand the basic requirement and dnt feel shy while noting down on a piece of paper . 

So lets first try to understand how can I retrieve Contacts . What is the relation b/w contacts and opportunity. 
On every Opportunity there is account and account has many contacts associated with it. So, the idea is to first have the Account associated with the current opportunity. then get all the ids of contacts associated with it.
  

I am just going to copy the Apex class that i have written on my personal org. :-


public class ContactList {
    Id opid=ApexPages.currentPage().getParameters().get('id');
    list<opportunity> oplist = new list<opportunity>();
    set<id> acid= new set<id>();
    list<Contact> cont = new list<Contact>();
    public list<Contact> getContactss(){
        for(Account acc:[Select id , name,(select id,lastname from contacts) from Account 
                where AnkushTest__Opportunity__c= :opid])//ApexPages.currentPage().getParameters().get('id')
            { 
               system.debug('=========='+opid);
               system.debug(acc.id+'************************');
               acid.add(acc.id);
            }
        system.debug('========'+acid);
        for(contact con:[select id , name,Accountid,phone from contact where accountid in : acid]){
            cont.add(con);
            system.debug(acid);
        }
        return cont;
    }
    
    public ContactList(ApexPages.StandardController controller) {

    }





visualforce page :-


<apex:page standardController="opportunity" extensions="ContactList">
    <apex:form >
         <apex:pageBlock title="My Content">

        <apex:pageBlockTable value="{!Contactss}" var="item">
        <apex:column >
                <apex:outputLink target="_blank" value="/{!item.id}">{!item.id}</apex:outputLink>
            </apex:column>

           <apex:column value="{!item.Accountid}"/>
           <apex:column value="{!item.phone}"/>

        </apex:pageBlockTable> 

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


Remenber:- You need to append id in URL of the opportunity for which you want to get contacts.
like this   
apex/ContractList/?id=006i000000HRFOw



Happy coding !!
Cheers



Sunday 6 April 2014

How to write Approval process in trigger?

Approval Process!!!
In Salesforce, everyone is aware of approval process. What if I say you can write dynamic Approval processs in a trigger. By doing this your functionality is achieved . 

Salesforce it self illustrates this very smoothly 



Saturday 5 April 2014

How to write a trigger on Attachment object

Hey !  Lets  work on the object which is less used in salesforce i. e am talking about Attachment object !

What is Attachment object ?

Attachment is standard object provided by the salesforce. Interesting thing is you will not find this object under customise tab in salesforce UI. But if you are aware of workbench then through that you can easily come to know the objects which are available in salesforce . Any ways lets get ahead .


Attachment objects is available under the Account Object related list.

Now, Lets run towards writting a trigger on attachment object. So, let me just walk you through the scenario first.
Scenario is :- Say , we want to update an field named last attachment .Every time the attachnment is added. It is very simple to write a trigger whcih is hardly a 10 lines code .
 Here , is the trigger :-


trigger LastAttach on Attachment (After insert) {
    list<account> aclist= new  list<Account>() ;
    Account ac= new Account();
    for (attachment a : trigger.new ){
    
        aclist.add( new Account(Id=a.ParentId ,AnkushTest__last_attachment_added__c= System.now()));
    
            }
  //}
    update aclist;
    }


Dont worry lemme explain it what i did line by line :-

So , in the very first step I created the list of account , then I tried to iterate each attachment till it is created . 
Now, the intersting point here is to note that Account is the parent of attachment object . So, I just have to pick the parent ID and Assigning it to the Account Id. Then a very simple step to have the time stamp of current time used system.now() which returns the current time. 


Happy Coding !!!!
Ankush
ankushsalesforce@gmail.com











oAuth Use Case -  Headless API Explained in the Easiest way Wondering why? and When we use the Headless API flow Use case example: Imagine a...