+ Start a Discussion
SkeeterSkeeter 

Too Many SOQL Queries

Not sure what I'm missing to get the error.  Any help is greatly appreciated.

Test Class
@isTest(SeeAllData=true)
private class ChatterFeedItemTest 
{
 static testMethod void TestChatterFeedInsert()
    {

    User cl = [SELECT Id From User WHERE ID = :UserInfo.getUserId()];
               
    Escalated_Ticket__c inc = new Escalated_Ticket__c();
    inc.Client__c = cl.id;
    inc.Summary__c = 'Test Subject';
    inc.Description__c = 'Test Description';
    inc.Client_Updated__c = False;
    insert inc;
        
    //Start Test
    Test.startTest();    
        
    // Create Chatter Post on test incident
    FeedItem testFeed1 = new FeedItem();
    testFeed1.ParentId = inc.Id;    
    testFeed1.Body = 'Test Feed 1 Post'; 
    testFeed1.Type = 'TextPost';
    insert testFeed1;
    
    // Create Chatter Comment on test incident
    FeedComment testFeed1Comment = new FeedComment();
    testFeed1Comment.CommentBody = 'Test Feed 1 Comment';
    testFeed1Comment.FeedItemId = testFeed1.Id;
    testFeed1Comment.CommentType='TextComment';
    insert testFeed1Comment;
            
    // Create Chatter Post on test incident
    FeedItem testFeed2 = new FeedItem();
    testFeed2.Body ='Test Feed 2 Post';
    testFeed2.ParentId = inc.Id;
    testFeed2.Type = 'TextPost';
    insert testFeed2;
            
    // Create Chatter Comment on test incident
    FeedComment testFeed2Comment = new FeedComment();
    testFeed2Comment.CommentBody ='#answer Test Feed 2 Comment';
    testFeed2Comment.FeedItemId = testFeed2.Id;
    testFeed2Comment.CommentType='TextComment';
    insert testFeed2Comment;      
        
    //Update Test Incident
	inc.Client_Updated__c = True;
    update inc;  
  
    //Stop Test
    Test.stopTest();  
   
    }
}

Trigger
 
trigger ChatterFeedItemTriggerAI on FeedItem (after insert) {

	//Collect all ParentIds into a set
    Set<Id>incIds = new Set<Id>();
    
    //List of all Escalated Tickets to be updated with the flag
    List<Escalated_Ticket__c> toUpdate = new List<Escalated_Ticket__c>();
    
    //Collect all ParentIds    
    for(FeedItem i : trigger.New){
        if(i.ParentId.getSobjectType() == Escalated_Ticket__c.SobjectType && i.Type == 'TextPost'){
        incIds.add(i.ParentId);
        }
    }
    
    //Collect all escalated ticket records with the above list of parentIds and return only those records
    Map<Id,Escalated_Ticket__c> mapofInc = new Map<Id,Escalated_Ticket__c>([select Id,Client__c,Client_Updated__c from Escalated_Ticket__c where id in:incIds]);
    Boolean updateValues = false;
    for(FeedItem i : trigger.new){
        if(mapofInc.ContainsKey(i.ParentId)){
            if(mapofInc.get(i.ParentId).Client__c == i.CreatedById){
                mapofInc.get(i.ParentId).Client_Updated__c = true;
                updateValues = true;
            }
        }
    }
    if(updateValues){
        update mapofInc.values();
    }
}


 
Krishna SambarajuKrishna Sambaraju
Remove the "SeeAllData = true" and try it.
Abhishek BansalAbhishek Bansal
Hi,

I have updated your test class.
Please use the below test class :
@isTest
private class ChatterFeedItemTest 
{
 static testMethod void TestChatterFeedInsert()
    {

    //User cl = [SELECT Id From User WHERE ID = :UserInfo.getUserId()];
               
    Escalated_Ticket__c inc = new Escalated_Ticket__c();
    inc.Client__c = UserInfo.getUserId();
    inc.Summary__c = 'Test Subject';
    inc.Description__c = 'Test Description';
    inc.Client_Updated__c = False;
    insert inc;
        
    // Create Chatter Post on test incident
    FeedItem testFeed1 = new FeedItem();
    testFeed1.ParentId = inc.Id;    
    testFeed1.Body = 'Test Feed 1 Post'; 
    testFeed1.Type = 'TextPost';
    
    //Start Test
    Test.startTest();
    
    insert testFeed1;
    
    //Stop Test
    Test.stopTest();  
    
    Escalated_Ticket__c testInc = [Select Client_Updated__c from Escalated_Ticket__c where id = :inc.Id];
    system.assertEquals(true,testInc.Client_Updated__c);
    }
}

Let me know if you have any issues in it.

Thanks,
Abhishek
ManojjenaManojjena

Hi lilranger,
You are getting error because you are inserting  four times in one test  method .Basically if you wil insert or update like this your trigger wil execute per dml once .

If you want to test with bulk records you need to create record with for loop and needs to do tthe DML with list of records .
Also in your test class SeeAllData=true is not required here only if you want to query the Database record then you need this .
I don't think update statement is requird in test class .
Try with below code it will help.
@isTest
private class ChatterFeedItemTest {
	static testMethod void TestChatterFeedInsert(){
		Profile prof = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
		User usr = new User(Alias = 'standt', Email='adminuser@testorg.com', 
			EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
			LocaleSidKey='en_US', ProfileId = prof.Id, 
			TimeZoneSidKey='America/Los_Angeles', UserName='adminuser@testorg.com');
		System.runAs(usr) {
		Escalated_Ticket__c inc = new Escalated_Ticket__c();
			inc.Client__c = usr.Id;
			inc.Summary__c = 'Test Subject';
			inc.Description__c = 'Test Description';
			inc.Client_Updated__c = False;
			insert inc;
			inc=[SELECT id,Client_Updated__c FROM Escalated_Ticket__c WHERE id =:inc.Id];
			System.assertEquals(False,inc.Client_Updated__c);
		List<FeedItem> fedItemList=new List<FeedItem>();   
		for(Integer count=0;count<200 ;count++){
			FeedItem testFeed = new FeedItem();
			testFeed1.ParentId = inc.Id;    
			testFeed1.Body = 'Test Feed +count+'Post'; 
			testFeed1.Type = 'TextPost';
			fedItemList.add(testFeed);
		}
		Test.startTest(); 
			Insert fedItemList;
		Test.stopTest();    
		}
	}
}
Let me know if it helps !!
Thanks
Manoj
 
Miles Smith 5Miles Smith 5
Thanks for sharing.I found a lot of interesting information here. A really good post, very thankful and hopeful that you will write many more posts like this one.
for me, this thread solution worked https://salesforce.stackexchange.com/questions/164931/is-there-a-way-to-call-javascript-after-action-on-visualforce-page​
with regards
https://kodi.software/download/  https://messenger.red/ https://hotstar.onl/