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
Logi VLogi V 

Help in this test class for trigger and triggerhandler

trigger UserTrigger on User (before insert, After Insert) {
    if(trigger.isinsert && trigger.isAfter) {
        List<String> uIds = new List<String>();
        for(User u: trigger.new)
            if(String.isNotBlank(u.ContactId))
                uIds.add(u.id);
           if(uIds.size() > 0) {
      //  UserTriggerHandler.addToMainGroup(trigger.newMap.keySet());
        UserTriggerHandler.sendEmailToAddChatterGroup(String.join(uIds,','), uIds[0]);
    } 
    }
}



public without sharing class UserTriggerHandler {
    @future
    public static void addToMainGroup(Set<Id> userIds) {
     List<User> users=[select id, Username from User where id in :userIds];

     CollaborationGroup chatterGroup=[select Id from CollaborationGroup Where Name='Sidebar'];
     List<CollaborationGroupMember> chatterGroupMembers=new List<CollaborationGroupMember>();

     for(User user: users){
         CollaborationGroupMember newMember = new CollaborationGroupMember(CollaborationGroupId=chatterGroup.id, MemberId = user.Id, NotificationFrequency = 'P');
         chatterGroupMembers.add(newMember);
     }

     insert chatterGroupMembers;

    }
    
    public static void sendEmailToAddChatterGroup(String body, String s) {
      Messaging.SingleEmailMessage mail = 
      new Messaging.SingleEmailMessage();
      List<String> sendTo = new List<String>();
      sendTo.add(Label.Chatter_Group_Inbound_Email);
      mail.setToAddresses(sendTo);
      mail.setSubject(s);
      mail.setHtmlBody(body);
      Messaging.sendEmail(new List<Messaging.SingleEmailMessage> {mail});
    
    } 
}
ShirishaShirisha (Salesforce Developers) 
Hi Logi,

Greetings!

Please refer the sample test class on how to cover the email trigger code.

https://developer.salesforce.com/forums/?id=9060G000000XgfHQAS

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
New American StoreNew American Store
Hi.

i also stuck sometime in these commands but thanks to my colleagues who always ready to help me in any condition ... i will definetly discuss and resend my answer over here ... The Suicide Squad Bloodsport Jacket (https://www.newamericanstore.com/product/the-suicide-squad-bloodsport-jacket/)