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
KimKim 

TestClass - Existing ChatterGroup Issue

Hello, 

I have a trigger on FeedComment that creates an Internal Request ticket (custom object) if a specific chattergroup was added on the comment. Does anyone know how I can access the ChatterGroup @IRTicket without having to use SeeAllData = true?

Class: 
public class InternalRequest_CreateFromChatterComment {

    public static void createNewInternalRequest(List<FeedComment> chatterComment){

		List<FeedComment> lFeedComment = new List<FeedComment>(); 
        List<Id> lFeedItem = new List<Id>(); 
        Map<Id,String> mfeedItemBody = new Map<Id,String>(); 
		Map<Id,Id> mpostUserId = new Map<Id,Id>();
		List<Ops_Priority_List__c> newIRtoInsert = new List<Ops_Priority_List__c>(); 
		Id internalRequestQuestionRecordTypeId = Schema.SObjectType.Ops_Priority_List__c.getRecordTypeInfosByName().get('Question').getRecordTypeId(); 

			List<User> lUser = [SELECT Id FROM User WHERE ProfileId = '00ea00000021duy']; //Checks if the user is an admin 
			Set<Id> userIds = new Set<Id>(new Map<Id, User>(lUser).keyset()); 

			Id cgroupId = [SELECT Id, Name FROM CollaborationGroup WHERE Name = 'IRTicket'].Id; //Stores chatterGroupId ca
    
        for(FeedComment c : chatterComment){
			
			if((c.ParentId ==  cgroupId && userIds.contains(c.CreatedById)) || (c.CommentBody.contains('@IRTicket') && userIds.contains(c.CreatedById))){
				lFeedComment.add(c); //List of all ChatterComments
                lFeedItem.add(c.FeedItemId); //List of all ChatterFeedItems 
				mpostUserId.put(c.Id,c.CreatedById);
			}
			System.debug('lFeedComment -->' + lFeedComment); 
			System.debug('mpostUserId -->' + mpostUserId); 
			System.debug('InsertedbyId -->' + c.CreatedById); 
		}

		if(lFeedComment.size()>0){
			System.debug('lFeedComment size --> ' + lFeedComment.size()); 
            
			List<FeedItem> feedItemList = [SELECT Body,ParentId FROM FeedItem WHERE Id =:lFeedItem]; 
           
            for(FeedItem f : feedItemList){
                mfeedItemBody.put(f.Id,f.Body); 
            }
			for(FeedComment cg : lFeedComment){
                lFeedItem.add(cg.FeedItemId); 
				
					Ops_Priority_List__c i = new Ops_Priority_List__c(
						OwnerId = mpostUserId.get(cg.Id),  
						Description__c = mfeedItemBody.get(cg.FeedItemId).stripHtmlTags() + ' ' + cg.CommentBody + ' ' + URL.getSalesforceBaseURL().toExternalForm() + '/'+
                        cg.ParentId + '?fld=' +cg.FeedItemId,   
					 	Name = 'Questions from Chatter',   
						Status__c = 'New', 
						RecordTypeId = internalRequestQuestionRecordTypeId); 

						newIRtoInsert.add(i); 
			}
			System.debug('newIRtoInsert = ' + newIRtoInsert); 
		}
		if(!newIRtoInsert.isEmpty()){
			try{
				database.insert(newIRtoInsert,false); 
			} catch (DmlException e){
				System.debug('The following exception has occured: ' + e.getMessage()); 
			}


			System.debug('newIRinserted = '+ !newIRtoInsert.isEmpty()); 
		}
	}
}

TestClass:
@isTest
public class InternalRequest_CreateFromChatterCTest {
    
    @testSetup static void setup(){
        List<TriggerController__c> tc = new List<TriggerController__c>{
            new TriggerController__c(Name='Lead',Disabled__c=false)
        }; 
            insert tc;
        
        User u = new User(
            LastName = 'Newuser', 
            Alias = 'newu', 
            Email = 'newuser@lmitest.com', 
            Username = 'newuser@lmitest.com.kdy', 
            CommunityNickname = 'newuser', 
            ProfileId = UserInfo.getProfileId(),
            TimeZoneSidKey = 'America/New_York', 
            LocaleSidKey = 'en_US', 
            EmailEncodingKey = 'UTF-8', 
            LanguageLocaleKey = 'en_US'
        ); 
        insert u; 
        
        
        Lead l = new Lead(
            FirstName = 'Firsttest', 
            LastName = 'Samplelead', 
            Email = 'firstsample@Samplelead.com', 
            CountryCode = 'US',
            Company = 'TestLeadCompany'
        ); 
        insert l; 
        
        
        FeedItem lPost = new FeedItem(
            ParentId = l.Id, 
            Body = 'Chatterpost from the lead'
        ); 
        insert lPost; 
        
    }
    
    static testMethod void leadChatterComment(){
      
        User u = [SELECT Id, EMAIL FROM User WHERE Username = 'newuser@lmitest.com.kdy' LIMIT 1]; 
        System.debug('u--->' + u); 
        Lead le = [SELECT Id FROM Lead WHERE Email = 'firstsample@Samplelead.com' LIMIT 1]; 
        System.debug('le-->' + le); 
        FeedItem f = [SELECT Id,CreatedById,ParentId FROM FeedItem WHERE ParentId =: le.Id LIMIT 1]; 
        System.debug('f-->' + f); 
        
        FeedComment lComment = new FeedComment(
            CommentBody = '@IRTicket - create this new IR', 
            FeedItemId = f.Id
        ); 
        test.startTest(); 
        insert lComment; 
        System.debug('lComment-->' + lComment); 
        test.stopTest(); 
        
        Ops_Priority_List__c iR = [SELECT Id,Name,OwnerId FROM Ops_Priority_List__c WHERE OwnerId =: f.CreatedById LIMIT 1]; 
        System.assertEquals('Questions from Chatter', iR.Name); 
        System.assertEquals(f.CreatedById,iR.OwnerId); 
        
        
    }
}

 
NagendraNagendra (Salesforce Developers) 
Hi Kim,

May I suggest you please check with below link with a similar issue and suggested workaround. Please let us know if this helps.

Kindly mark this as solved if it's resolved.

Thanks,
Nagendra