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;
        }
       
    }    

}



So , lemme explain you what i did . I have defined operations that are  going to happen when  it is insert and when it is update.
So, in Insert  I have just taken the the value from custom setting and added it to the SamplePrfix.

Now, the interesting thing is  Whenever  I create a new record custom setting has to be updated so that every time a new record is created it starts from the last number. Say, if I created a record . whose value is abcd01. then next time whenver I am going to create a new record its value should be increased by 1.


Happy coding!!!!

Cheers!!















No comments:

Post a Comment

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...