Populate Auto Number for Area using Trigger – Coderinme

 

You all have been introduced to trigger in our previous post ” Learn Salesforce Trigger in a Simple way” , the first post about Trigger then we have two more parts of it which you can see on our site.

Today, I’m going to tell you the use of Map in a trigger and the scenario is like:

There is a custom object “AREA” with three fields Name, AreaCode__c, AreaCount__c and we have created two custom fields Area_Count__c(Number Field) and Area__c (Lookup of AREA) in standard field “ACCOUNT”. If we create an account with the existing area then the number of Area_Count__c increase by 1 and display as “for Dilshad Garden it would be like DG-001” and after the second insertion it will be DG-002 and so on. Also we can delete the account and the count remain accurate and if we again insert an account it will again increment from the last count value.

The code for this be like:

trigger PopulateArea on Account(before insert,after delete){
        if(trigger.isInsert){
                Set<id> strNew = new Set<id>();


                for(Account ac:trigger.new)
                        strNew.add(ac.Area__c);

                Map<id,Area__c> mapArea = new Map<id,Area__c>([Select Name,AreaCode__c,AreaCount__c,(select id, Area_Count__c from Accounts__r Order By createdDate DESC LIMIT 1) from Area__c where id IN: strNew]);
                Map<id,Integer> mapCount = new Map<id,Integer>();


                for(Account act:trigger.new){
                        
                        if(mapArea.containsKey(act.Area__c)){
                                Integer count = 0, arCount=0;
                                if(mapArea.get(act.Area__c).Accounts__r.size()>0)
                                    arCount = Integer.valueOf(mapArea.get(act.Area__c).Accounts__r[0].Area_Count__c.split('-')[1]);
                                if(mapCount.containsKey(act.Area__c)){
                                    count =   arCount + 1 + mapCount.get(act.Area__c);
                                    Integer mpcnt=mapCount.get(act.Area__c)+1;
                                    mapCount.put(act.Area__c,mpcnt);
                                }
                                else{
                                    count = arCount + 1;
                                    mapCount.put(act.Area__c,1);
                                }

                                if(count<10)
                                act.Area_Count__c = mapArea.get(act.Area__c).AreaCode__c + '-00' + count;
                                else if(count>=10 && count<100)
                                act.Area_Count__c = mapArea.get(act.Area__c).AreaCode__c + '-0' + count;
                                else if(count>100)
                                act.Area_Count__c = mapArea.get(act.Area__c).AreaCode__c + '-' + count;

                        }
                }
                for(Area__c ar: mapArea.values()){
                    if(mapCount.containsKey(ar.id))
                        ar.AreaCount__c += mapCount.get(ar.id);
                }
        update mapArea.values();
    }
    
     if(trigger.isDelete){
        Set<id> strOld = new Set<id>();
        Map<id,Area__c> mapDel = new Map<id,Area__c>();
        Map<id,Integer> mapDelCount = new Map<id,Integer>();
        for(Account acct : trigger.old)
            strOld.add(acct.Area__c);
        List<Area__c> arr = [select Name,AreaCode__c,AreaCount__c from Area__c where id IN: strOld];
        for(Area__c area : arr)
            mapDel.put(area.id, area);
        for(Account acct : trigger.old){
                if(mapDel.containsKey(acct.Area__c)){
                        if(mapDelCount.containsKey(acct.Area__c)){
                                Integer countDel=mapDelCount.get(acct.Area__c)+1;
                                mapDelCount.put(acct.Area__c,countDel); 
                        }
                        else
                          mapDelCount.put(acct.Area__c,1);      
                }
        }   

        for(Area__c ar: mapDel.values()){
            if(mapDelCount.containsKey(ar.id))
                ar.AreaCount__c -= mapDelCount.get(ar.id);
        }
        update mapDel.values();
    }
        
}

 

The portion,

if(trigger.isInsert){
   Set<id> strNew = new Set<id>();


   for(Account ac:trigger.new)
     strNew.add(ac.Area__c);

 Map<id,Area__c> mapArea = new Map<id,Area__c>([Select Name,AreaCode__c,AreaCount__c,(select id, Area_Count__c from Accounts__r Order By createdDate DESC LIMIT 1) from Area__c where id IN: strNew]);
 Map<id,Integer> mapCount = new Map<id,Integer>();


 for(Account act:trigger.new){
                        
   if(mapArea.containsKey(act.Area__c)){
        Integer count = 0, arCount=0;
        
if(mapArea.get(act.Area__c).Accounts__r.size()>0)
                                    arCount = Integer.valueOf(mapArea.get(act.Area__c).Accounts__r[0].Area_Count__c.split('-')[1]);
                                if(mapCount.containsKey(act.Area__c)){
                                    count =   arCount + 1 + mapCount.get(act.Area__c);
                                    Integer mpcnt=mapCount.get(act.Area__c)+1;
                                    mapCount.put(act.Area__c,mpcnt);
                                }
                                else{
                                    count = arCount + 1;
                                    mapCount.put(act.Area__c,1);
                                }

                                if(count<10)
                                act.Area_Count__c = mapArea.get(act.Area__c).AreaCode__c + '-00' + count;
                                else if(count>=10 && count<100)
                                act.Area_Count__c = mapArea.get(act.Area__c).AreaCode__c + '-0' + count;
                                else if(count>100)
                                act.Area_Count__c = mapArea.get(act.Area__c).AreaCode__c + '-' + count;

                        }
                }
                for(Area__c ar: mapArea.values()){
                    if(mapCount.containsKey(ar.id))
                        ar.AreaCount__c += mapCount.get(ar.id);
                }
        update mapArea.values();
    }

 

In this, we are operating upon insertion.First we added the id of the Account in the set that we have just inserted then queried the fields of the Area and the related Account in the inner query to work upon these fields.we retrieve the values and opt for the last created account so that we have the last counter value to increase it.

Then, we check if the map contains that area then increase the countMap by 1 to store the count of the Area and increment it .and put that counter value in the map.

In the next logic, we have just checked if it cross the limited value to particular range then the counter will change.Then, at last we put the Area_Count value in the field from the map and update it.

Same we did in the case of delete, we retrieve the values and store it and delete the Account.

This trigger can have multiple solutions, if you have any alternate and better solution according to you then please share with us.

Note: All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin@coderinme.com

A Salesforce Developer at AlmaMate Info Tech PVT LTD. An Aligarian and also your query solver in database field.This site mark the difference as Black or White to make you choose the best for you. Don't feel pressurized, feel confident..!!

Leave a reply:

Your email address will not be published.