Sep 23, 2024
70 Views
Comments Off on Salesforce Trigger Example to count number of completed tasks in a single contact
0 0

Salesforce Trigger Example to count number of completed tasks in a single contact

Written by

Below is an example of how to use Salesforce Trigger to count number of completed tasks in a single contact

trigger UpdateAccountNameFromContactLastName2 on Contact (before insert, after insert, before update, after update, before delete, after delete, after undelete) {
    set<Id> setAccountIds = new set<Id>() ;
    if(Trigger.isInsert){
        for(Contact oContact : Trigger.New){
            if(oContact.lastName != null) { //although lastName is required field; still checking for null
                setAccountIds.add(oContact.accountId) ;  
            }
        }
        system.debug('after insert-setAccountIds:: ' + setAccountIds) ;
    }    
    if(Trigger.isUpdate){
        for(Contact oContact : Trigger.New){
            if(oContact.lastName != null && oContact.lastName != Trigger.oldMap.get(oContact.Id).lastName) 
            setAccountIds.add(oContact.accountId) ;
        }
        system.debug('after update-setAccountIds:: ' + setAccountIds) ;
    }    
    if(Trigger.isDelete){
        for(Contact oContact : Trigger.Old){
            if(oContact.lastName != null) 
            setAccountIds.add(oContact.accountId) ;
        }
        system.debug('after delete-setAccountIds:: ' + setAccountIds) ;
    }    
    if(Trigger.isUndelete){
        for(Contact oContact : Trigger.new){
            if(oContact.lastName != null) 
            setAccountIds.add(oContact.accountId) ;
        }
        system.debug('after undelete-setAccountIds:: ' + setAccountIds) ;
    }
    if(!setAccountIds.isEmpty()){
        set<String> setContactName = new set<String>() ;
        list<Account> listAccountToUpdate = new list<Account>() ;
        list<Account> listAccount = [Select Id, Name, (Select Id, LastName from Contacts order by createddate)     From Account Where Id IN: setAccountIds] ;
        if(listAccount != null && !listAccount.isEmpty()){
            for(Account oAccount : listAccount){
                for(Contact oContact : oAccount.Contacts){
                    if(!setContactName.contains(oContact.lastName)){
                        setContactName.add(oContact.lastName) ;
                    }
                }
                list<String> listContactName = new list<String>() ;
                listContactName.addAll(setContactName) ;
                oAccount.Name = oAccount.Name.substringBefore(listContactName[0]) ;
                for(String lastName : setContactName){
                    oAccount.Name = oAccount.Name +  ' ' + lastName  ;
                }
                listAccountToUpdate.add(oAccount) ;
            }
        }
        if(!listAccountToUpdate.isEmpty()) update listAccountToUpdate ;
    }
}

 

Article Categories:
Blog
Ramya Singh
https://thestarbiznews.com

Ramya Singh isn't your average tech blogger. Sure, she's got the brains to understand the latest algorithms and the jargon to explain them in plain English. But she's also got a twinkle in her eye and a way of weaving technology into the fabric of everyday life that makes it nothing short of fascinating. Whether she's reviewing the latest smartphone, exploring the potential of virtual reality, or delving into the ethical implications of artificial intelligence, Ramya does it with a contagious enthusiasm that makes you want to learn more, do more, and be a part of the exciting world of tech. So, if you're looking for a tech blog that's informative, inspiring, and just plain fun, follow Ramya Singh. She'll take you on a journey through the ever-evolving landscape of technology, and you might just find yourself a little bit more tech-savvy (and a lot more excited) along the way.