+ Start a Discussion
CesincoCesinco 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY while running test class

Hi folks,

 

Trying to deploy a really simple trigger and getting into problems - hope someone "out there" can help.

 

BTW, the Collaboration Group named "Technology Roadmap" does indeed exist in both sandbox and production envirtonments so the chatterGroups List variable should contain the single CollaborationGroup object. Instead, the test class errors with the message:

 

Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PostToChatterOnNewTechRoadmap: execution of AfterInsert caused by: System.ListException: List index out of bounds: 0 Trigger.PostToChatterOnNewTechRoadmap: line 9, column 1: []", Failure Stack Tra...

 

Trigger code:

 

trigger PostToChatterOnNewTechRoadmap on Technology_Roadmap__c (after insert) {

	CollaborationGroup chatterGroup;
	List<CollaborationGroup> chatterGroups = [SELECT Id FROM CollaborationGroup WHERE Name=:'Technology Roadmap' LIMIT 1];
	//if (chatterGroups.isEmpty()) {
	//	chatterGroup = new CollaborationGroup();
	//	chatterGroup.id = '0F9W0000000Cch3KAC';
	//} else {
		chatterGroup = chatterGroups[0];
	//}

	for (Technology_Roadmap__c trm : Trigger.new) {
		FeedItem fitem = new FeedItem();
		fitem.type = 'LinkPost'; //'TextPost,';
		fitem.ParentId = chatterGroup.id; //'0F9W0000000Ccge' = Sandbox; //'0F920000000PItQ' = Production;
		fitem.LinkUrl = '/' + trm.id;
		fitem.Title = 'New Technology Roadmap: ' + trm.Name;
		fitem.Body = 'Design Discipline: ' + ((trm.Design_Discipline__c != null) ? trm.Design_Discipline__c : 'Not specified')
					+ '\nCircuit Application: ' + ((trm.Circuit_Application__c != null) ? trm.Circuit_Application__c : 'Not specified')
					+ '\nSegment: ' + ((trm.Segment__c != null) ? trm.Segment__c + ((trm.Subsegment__c != null) ? ' => ' + trm.Subsegment__c : '') : 'Not specified');
		insert fitem;
	}

}

 

Test class:

 

@isTest
public class Test_trig_PostToChatterOnNewTechRoadmap{

	static testMethod void verifyPostToChatterOnNewTechRoadmap() {

		//CollaborationGroup chatterGroup = [SELECT Id FROM CollaborationGroup WHERE Name='Technology Roadmap' LIMIT 1];

		CollaborationGroup chatterGroup;
		List<CollaborationGroup> chatterGroups = [SELECT Id FROM CollaborationGroup WHERE Name=:'Technology Roadmap' LIMIT 1];
		if (chatterGroups.isEmpty()) {
			chatterGroup = new CollaborationGroup();
			chatterGroup.id = '0F9W0000000Cch3KAC';
		} else {
			chatterGroup = chatterGroups[0];
		}

		List<Technology_Roadmap__c> ltrm = new List<Technology_Roadmap__c>{};
		for (integer i=0; i<10; i++) {
			Technology_Roadmap__c trm = new Technology_Roadmap__c(Name = 'trigCoverageTest_' + i);
			trm.Design_Discipline__c = 'AC';
			System.debug('Name: ' + trm.Name);
			System.debug('Design Discipline: ' + trm.Design_Discipline__c);
			ltrm.add(trm);
		}

		test.startTest();
		insert ltrm;
		test.stopTest();

		List<FeedItem> lfi = [SELECT Title, Body FROM FeedItem WHERE ParentId = :chatterGroup.id ORDER BY Title];

		integer j = 0;
		string assTitle;
		for (FeedItem fi : lfi) {
			assTitle = 'New Technology Roadmap: trigCoverageTest_' + j++;
			System.debug(fi.Title);
			System.assertEquals(assTitle, fi.Title);
		}
	}
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Thomas DvornikThomas Dvornik
All data is hidden from the tests. You should create a group in the test. See http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_data_access.htm

If you need the data, you can add isTest(SeeAllData=true) to your method.

All Answers

Vinit_KumarVinit_Kumar

Cesinco,

 

Please change your code from 

 

List<CollaborationGroup> chatterGroups = [SELECT Id FROM CollaborationGroup WHERE Name=:'Technology Roadmap' LIMIT 1];

 

to

 

List<CollaborationGroup> chatterGroups = [SELECT Id FROM CollaborationGroup WHERE Name='Technology Roadmap' LIMIT 1];

 

You don't need colon in query,

CesincoCesinco

Hi Vinit,

 

Thanks so much for your quick reply. Unfortunately, taking out the colon didn't fix the situaiton. And just to make sure, I also tried another code change that used a scalar rather than a List reference for the CollaborationGroup since I was only interested in retrieving one group anyway. So the new code had the line:

 

CollaborationGroup chatterGroup = [SELECT Id FROM CollaborationGroup WHERE Name='Technology Roadmap' LIMIT 1];

 

and the response from the test was different, but still indicated that no such CollaborationGroup could be found:

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PostToChatterOnNewTechRoadmap: execution of AfterInsert

caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.PostToChatterOnNewTechRoadmap: line 20, column 1: []

 

Vinit_KumarVinit_Kumar

This means your query is not returning any results.Can you try running the query in Developer Console and see if returned results.If the siisue still persists,then post your latest code,I can take a look.

 

 

CesincoCesinco

Hi Vinit,

 

Thanks again for the quick reply. Here is a link to the screen capture of my query:

 

https://docs.google.com/file/d/0B7b_epkNFV-QVHYtR2treml3c1U/edit?usp=sharing

Vinit_KumarVinit_Kumar

That means the query is fine and returning results.Could you post your latest code here and the exact error message which you are getting.

CesincoCesinco

Hi Vinit,

 

Yes - that is why I find this so puzzling - if the query returns a result in manual testing, why would it return 0 rows in automated test procedure? Also, If I actually run the code by performing the steps that a normal user would (i.e. creating a Technology Roadmap object), the code works properly and a new feed item is created within the chatterGroup Technology Roadmap. The only reason I am still struggling with this is because of SalesForce.com's insistence that I must have 75% code coverage in automated testing, otherwise I would have deployed this days ago.

 

Anyway, here is all the latest code with comments stripped out to be more concise.

 

Trigger:

 

trigger PostToChatterOnNewTechRoadmap on Technology_Roadmap__c (after insert) {

    CollaborationGroup chatterGroup = [SELECT Id FROM CollaborationGroup WHERE Name='Technology Roadmap' LIMIT 1];
    
	for (Technology_Roadmap__c trm : Trigger.new) {
		FeedItem fitem = new FeedItem();
		fitem.type = 'LinkPost'; //'TextPost,';
		fitem.ParentId = chatterGroup.id; //'0F9W0000000Ccge' = Sandbox; //'0F920000000PItQ' = Production;
		fitem.LinkUrl = '/' + trm.id;
		fitem.Title = 'New Technology Roadmap: ' + trm.Name;
		fitem.Body = 'Design Discipline: ' + ((trm.Design_Discipline__c != null) ? trm.Design_Discipline__c : 'Not specified')
					+ '\nCircuit Application: ' + ((trm.Circuit_Application__c != null) ? trm.Circuit_Application__c : 'Not specified')
					+ '\nSegment: ' + ((trm.Segment__c != null) ? trm.Segment__c + ((trm.Subsegment__c != null) ? ' => ' + trm.Subsegment__c : '') : 'Not specified');
		insert fitem;
	}
}

 

Test class:

 

@isTest
public class Test_trig_PostToChatterOnNewTechRoadmap{
    
    static testMethod void verifyPostToChatterOnNewTechRoadmap() {
        
        CollaborationGroup chatterGroup;
        List<CollaborationGroup> chatterGroups = [SELECT Id FROM CollaborationGroup WHERE Name='Technology Roadmap' LIMIT 1];
        if (chatterGroups.isEmpty()) {
            chatterGroup = new CollaborationGroup();
            chatterGroup.id = '0F9W0000000Cch3KAC';
        } else {
            chatterGroup = chatterGroups[0];
        }
    
        List<Technology_Roadmap__c> ltrm = new List<Technology_Roadmap__c>{};
        for (integer i=0; i<10; i++) {
            Technology_Roadmap__c trm = new Technology_Roadmap__c(Name = 'trigCoverageTest_' + i);
            trm.Design_Discipline__c = 'AC';
            System.debug('Name: ' + trm.Name);
            System.debug('Design Discipline: ' + trm.Design_Discipline__c);
            ltrm.add(trm);
        }
        
        test.startTest();
        insert ltrm;
        test.stopTest();
        
        List<FeedItem> lfi = [SELECT Title, Body FROM FeedItem WHERE ParentId = :chatterGroup.id ORDER BY Title];
        
        integer j = 0;
        string assTitle;
        for (FeedItem fi : lfi) {
            assTitle = 'New Technology Roadmap: trigCoverageTest_' + j++;
            System.debug(fi.Title);
            System.assertEquals(assTitle, fi.Title);
        }
    }
}

 

 Error message:

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PostToChatterOnNewTechRoadmap: execution of AfterInsert

caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.PostToChatterOnNewTechRoadmap: line 3, column 1: []

 

Stack trace:

 

Class.Test_trig_PostToChatterOnNewTechRoadmap.verifyPostToChatterOnNewTechRoadmap: line 25, column 1

line 25 is the line:

 

insert ltrm;
CesincoCesinco

A link to the screen capture of the feed items being created automatically when testing via user interface (not through automated testing):

 

https://docs.google.com/file/d/0B7b_epkNFV-QMF9kTGk4RUZsUlE/edit?usp=sharing

Thomas DvornikThomas Dvornik
All data is hidden from the tests. You should create a group in the test. See http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_data_access.htm

If you need the data, you can add isTest(SeeAllData=true) to your method.
This was selected as the best answer
CesincoCesinco

Hi Thomas,

 

Thanks for taking up the challenge. I have already created the same chatter group in each environment - production and sandbox. Obviously, they will have different IDs in each so this is why I need to retrieve their IDs by using their name in the select query. The code works when running in the sandbox using the normal user-interface that any ordinary user would use. It only fails to work in the Test Class. Unfortunately, without passing the tests in the Test Class, I cannot deploy it to production even if I am fully convinced that it works.

 

I have even thought that maybe there is a security issue at work whereby the code in the Test Class runs under one set of credentials independent of those used by my credentials while accessing the regular UI and have wrapped my code in a runAs{} construct, but this went nowhere as well.

 

I will try your suggestion just in case...

 

Thanks,

 

Cesar

CesincoCesinco

Thomas,

 

I spoke too soon. The problem was indeed the absence of the (SeeAllData=true) parameter at the top of the Test Class's isTest directive. After running the test, I got a "Pass" for my result. I am marking your response as the accepted solution - THANKS!!!

 

Cesar

 

 

Vinit_KumarVinit_Kumar

Cesar,

 

My bad,I forgot to inform you to add (SeeAllData=true) in your test class to access existing data of your org in test class.

CesincoCesinco

No problem, Vinit - I appreciate that you spent the time to try to answer.