function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kittu9kittu9 

Custom Settings in apex

trigger EmailSend on Task(after insert, after update) {

try {
 Profile_allowed_to_runSendEmailToGroup__c getallprofiles=Profile_allowed_to_runSendEmailToGroup__c.getinstance('Profile Set 1');
        
        set<string>profileset=new set<string>();
        if(getallprofiles.get('Allowed_Profile__c')!=null)
        {
            string getprofilename=string.valueof(getallprofiles.get('Allowed_Profile__c'));
            list<string>profilelst=getprofilename.split(';');
            profileset.addAll(profilelst);
        }
        string profilename=[SELECT Id, Name FROM Profile WHERE Id=:userinfo.getProfileId() LIMIT 1].name;
        list < string > groupname = new list < string > ();
        List < String > mailAddrs = new List < String > ();
        String fullFileURL = URL.getSalesforceBaseUrl().toExternalForm();
        if(profileset.contains(profilename)==true)
        {
            List < id > Accoundid = new list < id > ();
            for (task t1: trigger.new) {
                if (t1.whatid != null && t1.what.type == 'Account') {
                    Accoundid.add(t1.whatid);
                }
                if (t1.Status == 'Completed') {
                    if (t1.Activity_Type__c == 'DPR')
                        groupname.add('SET DPR');
                    else if (t1.Activity_Type__c == 'GME')
                        groupname.add('SET GME');
                    else if (t1.Activity_Type__c == 'GM Changes')
                        groupname.add('SET GM CHANGES');
                    else if (t1.Activity_Type__c == 'Ownership Changes')
                        groupname.add('SET OWNERSHIP CHANGES');
                    else
                        groupname.add('SET ALL OTHERS');
                }
            }
            map < id, account > AccountMap = new map < id, account > ([select id, name from account where id In: Accoundid]);
            list < id > grpmemberid = new list < id > ();
            for (groupmember gm1: [select userOrGroupId from groupMember where group.name in : groupname]) {
                grpmemberid.add(gm1.userOrGroupId);
            }
            for (user u1: [select id, email from user where id IN: grpmemberid]) {
                mailAddrs.add(u1.email);
            }
            if (mailAddrs.size() > 0) {
                List < Messaging.SingleEmailMessage > allemail = new List < Messaging.SingleEmailMessage > ();
                for (task t1: trigger.new) {
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    mail.setToAddresses(mailAddrs);
                    string accountName;
                    if (t1.whatid != null && t1.what.type == 'Account') {
                        if (AccountMap.containskey(t1.whatid))
                            accountName = AccountMap.get(t1.whatid).name;
                        else
                            accountName = '';
                    }
                    mail.setSubject(t1.Activity_Type__c + ' : ' + accountName + ' Task Information');
                    mail.setPlainTextBody('A new Contact Report has been completed ' + '\n' + fullFileURL + '/' + t1.Id);
                    allemail.add(mail);
                }
                if (allemail.size() > 0) {
                    Messaging.sendEmail(allemail, false);
                }
            }
        }
  } catch (exception e) {
        system.debug('###' + e.getmessage());
    }
}

tThe above code is working fine. In custom setting have 3 records Profile Set 1,Profile Set 2,Profile Set 3 it is working fine for "Profile Set 1"

 

not working for remaining other. whr i have to change in code. pls help me on this.

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

You have only queried Profile set 1 from custom setting. Look at the below line highlighted.

 

try {
 Profile_allowed_to_runSendEmailToGroup__c getallprofiles  =                Profile_allowed_to_runSendEmailToGroup__c.getinstance('Profile Set 1');

..................

}

 

Hope so this helps you...!

 

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.

 

kittu9kittu9

yes that i know how do the same for the other two is my question.

 

Profile Set 2

 

Profile Set 3

 

have to change any other code in trigger or not.