-2

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

  • ParentId = '0F90q0000004Ecc', and ProfileId = '00e30000001euS4', will fetch nothing in your test class, you have to create parentID record and query profile data for data – Rao Oct 2 '17 at 23:04
  • I've created the CollaborationGroup record and used that Id to reference on the ParentId. For some reason I'm getting a new error that System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, An active or archived group with this name already exists or is being deleted. Choose a different name or try again in a few minutes.: [Name]. Is this because the ChatterGroup name I used on my test class is already an existing chattergroup? – Kim Oct 3 '17 at 15:33
  • you should use names in your test cases that are not actually names or even possibly names of things like accounts, contacts, or usernames on test users that may possibly exist in the actual org. Personally - i add " - Test" to name values on my objects in test cases to always avoid that potential. – Ronnie Oct 4 '17 at 2:22
2

Update this the item to pull in the profile id:

 User admin = new User(
        LastName = 'Adminuser', 
        Alias = 'admin', 
        Email = 'adminuser@logmein.com', 
        Username = 'adminuser@logmein.com.kdy', 
        CommunityNickname = 'adminuser', 
        ProfileId = userInfo.getProfileId(),
        TimeZoneSidKey = 'America/New_York', 
        LocaleSidKey = 'en_US', 
        EmailEncodingKey = 'UTF-8', 
        LanguageLocaleKey = 'en_US'
    );

Then when you assign your parent (owner) to your record, you can use the parent id of the record you inserted by assigning like this:

ParentId = myObject.Id;

You never want to rely on data that is in any org, as it may/may not align with your test case, and further you're also not using

seeAllData =true

So even if it did exist, you'd never see it

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.