I'm receiving an error message System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, FeedItem: execution of AfterInsert caused by: System.QueryException: List has no rows for assignment to SObject. Class.InternalRequest_CreateNew.createNewIR: line 11, column 1 Trigger.FeedItem: line 13, column 1: []. This error occurs when I run my test class.
I'm unsure why I'm getting this error since Class.InternalRequest_CreateNew.createNewIR: line 11, column 1 line is querying an Id that exist in the sandbox which is this line of code
Id cgroupId = [SELECT Id, Name FROM CollaborationGroup WHERE Name =: 'IRTicket'].Id;
Class:
public class InternalRequest_CreateNew {
public static void createNewIR(List<FeedItem> chatterpost){
List<FeedItem> lGroupFeed = new List<FeedItem>();
Map<Id,Id> mpostUserId = new Map<Id,Id>();
Set<Id> sUserId = new Set<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();
Id cgroupId = [SELECT Id, Name FROM CollaborationGroup WHERE Name =: 'IRTicket'].Id;
System.debug('cgroupId =' + cgroupId);
List<User> lUser = [SELECT Id FROM User WHERE ProfileId = '00ea00000021duy'];
Set<Id> userIds = new Set<Id>(new Map<Id, User>(lUser).keyset());
for(FeedItem c : chatterpost){
if((c.ParentId == cgroupId && userIds.contains(c.CreatedById)) || (c.Body.contains('@IRTicket') && userIds.contains(c.CreatedById))){
lGroupFeed.add(c);
sUserId.add(c.CreatedById);
mpostUserId.put(c.Id,c.CreatedById);
}
System.debug('lGroupFeed =' + lGroupFeed);
System.debug('mpostUserId =' + mpostUserId);
System.debug('InsertedbyId =' + c.CreatedById);
}
if(lGroupFeed.size()>0){
System.debug('lGroupFeed size = ' + lGroupFeed.size());
for(FeedItem cg : lGroupFeed){
Ops_Priority_List__c i = new Ops_Priority_List__c(
OwnerId = mpostUserId.get(cg.Id),
Description__c = cg.Body.stripHtmlTags() + ' ' + URL.getSalesforceBaseURL().toExternalForm() + '/_ui/core/chatter/groups/GroupProfilePage?g=' + cgroupId + '&fld=' +cg.Id,
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
private class InternalRequest_CreateNewTest {
@testSetup static void setup(){
List<TriggerController__c> tc = new List<TriggerController__c>{
new TriggerController__c(Name = 'Lead',Disabled__c = false)
};
insert tc;
/*CollaborationGroup cgroup = new CollaborationGroup(
CollaborationType = 'Public',
Name = 'IRTicket',
OwnerId = '0051300000CLFco'
);
insert cgroup;*/
User admin = new User(
LastName = 'Adminuser',
Alias = 'admin',
Email = 'adminuser@logmein.com',
Username = 'adminuser@logmein.com.kdy',
CommunityNickname = 'adminuser',
ProfileId = '00ea00000021duy',
TimeZoneSidKey = 'America/New_York',
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US'
);
insert admin;
User sales = new User(
LastName = 'Salesuser',
Alias = 'sales',
Email = 'salesuser@logmein.com',
Username = 'salesuser@logmein.com.kdy',
CommunityNickname = 'salesuser',
ProfileId = '00e30000001euS4',
TimeZoneSidKey = 'America/New_York',
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US'
);
insert sales;
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 = '@IRTicket, Chatterpost from the lead'
);
insert lPost;
FeedItem gPost = new FeedItem(
ParentId = '0F90q0000004Ecc',
Body = 'Chatterpost from chattergroup'
);
insert gPost;
}
//Covers scenario where Admin creates chatter post on lead
static testMethod void adminLeadChatterPost(){
test.startTest();
User adm = [SELECT Id, EMAIL FROM User WHERE Email = 'adminuser@logmein.com' LIMIT 1];
Lead le = [SELECT Id, Email FROM Lead WHERE Email = 'firstsample@Samplelead.com' LIMIT 1];
FeedItem f = [SELECT Id, ParentId, CreatedByID FROM FeedItem WHERE ParentId =: le.Id LIMIT 1];
Ops_Priority_List__c i = [SELECT Id, Description__c, OwnerId, Name FROM Ops_Priority_List__c WHERE OwnerId =: f.CreatedById LIMIT 1];
test.stopTest();
System.assertEquals('Questions from Chatter',i.Name);
}
//Covers scenario where Admin creates chatter post directly on chatter group
static testMethod void adminGroupChatterPost(){
test.startTest();
User adm = [SELECT Id, EMAIL FROM User WHERE Email = 'adminuser@logmein.com' LIMIT 1];
FeedItem f = [SELECT Id, ParentId, CreatedByID FROM FeedItem WHERE ParentId =: '0F90q0000004Ecc' LIMIT 1];
Ops_Priority_List__c i = [SELECT Id, Description__c, OwnerId, Name FROM Ops_Priority_List__c WHERE OwnerId =: f.CreatedById LIMIT 1];
test.stopTest();
System.assertEquals('Questions from Chatter',i.Name);
}
}
Thank you