Apex - Salesforce.com - I need help writing an APEX class to test my working trigger - Chatter - Monitor specific keywords

StackOverflow https://stackoverflow.com/questions/13767870

Вопрос

Here is my working trigger

trigger CheckChatterPostsOnNSP on FeedItem (before insert) {

Set<Id> nspIds = new Set<Id>();

//Get the NSP that will be updated
List<Non_Standard_Pricing__c> nsp2Update = new List<Non_Standard_Pricing__c>();

//Get the key prefix for the NSP object via a describe call.
String nspKeyPrefix = Non_Standard_Pricing__c.sObjectType.getDescribe().getKeyPrefix();

//Get the Id of the user
Id profileId = UserInfo.getProfileId();

for (FeedItem f: trigger.new) {
    String parentId = f.parentId;

    if(profileId == '00e30000000eWXR') {// Users profile must be Sales and Service
        //We compare the start of the 'parentID' field to the NSP key prefix to
        //restrict the trigger to act on posts made to the NSP object.
        if (
            parentId.startsWith(nspKeyPrefix) &&
            (
                f.Body.contains('***APPROVED BY CHANNEL***') || 
                f.Body.contains('***APPROVED BY CSM***') || 
                f.Body.contains('[APPROVED BY CHANNEL]') || 
                f.Body.contains('[APPROVED BY CSM]')
            )
        ){
            nspIds.add(f.parentId);
        }
    }
}
List < Non_Standard_Pricing__c > nsps = [select id, Pre_Approved_Service_Discount__c, ownerId
from Non_Standard_Pricing__c where id in :nspIds];

for (Non_Standard_Pricing__c n: nsps) {
    //We compare the creator of the Chatter post to the NSP Owner to ensure
    //that only authorized users can close the NSP using the special Chatter 'hot-key'

        n.Pre_Approved_Service_Discount__c = true;
        nsp2Update.add(n);
    }
    update nsp2Update;
}

Here is my attempt to write an APEX Test Class

@isTest
public class CheckChatterPostsOnNSPTEST {

    static testMethod void CheckChatterPostsOnNSPTEST() {
        //Create and insert opp
        Opportunity opp = new Opportunity(Name='test opp', StageName='stage', Probability = 95, CloseDate=system.today());
        insert opp;

        //Create and insert NSP
        Non_Standard_Pricing__c NSP = new Non_Standard_Pricing__c(Opportunity__c = opp.Id, Status__c = 'Open');
        insert NSP;

        //Find user with Profile = Sales and Service
        Profile SalesNService = [Select id from Profile where Name = 'Sales and Service' limit 1];
        User u = new User(
            Alias = 'standt', 
            Email='standarduser@testorg.com',
            EmailEncodingKey='UTF-8',
            LastName='Testing',
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US',
            ProfileId = SalesNService.Id,
            TimeZoneSidKey='America/Los_Angeles',
            UserName='standarduser@testorg.com'
        );

        System.runAs(u)
        {
            //Create FeedItem entry with text '[APPROVED BY CHANNEL]'
            FeedItem post = new FeedItem();
            post.body = '[APPROVED BY CHANNEL]';

            //Now update the opportunites to invoke the trigger
            Test.startTest();
            insert post;
            Test.stopTest();
        }

        //Assertion Testing
        for(Opportunity o : [select Id, Name, Primary_NSP__r.Pre_Approved_Service_Discount__c from Opportunity where Id = :opp.Id]){
            system.assert(o.Primary_NSP__r.Pre_Approved_Service_Discount__c = true);
        }
    }
}

I'm getting the following errors Message: System.QueryException: List has no rows for assignment to SObject Stack Trace: Class.CheckChatterPostsOnNSPTEST.CheckChatterPostsOnNSPTEST: line 14, column 1

Any help is greatly appreciated.

Это было полезно?

Решение

That'd be pointing to this line:

Profile SalesNService = [Select id from Profile where Name = 'Sales and Service' limit 1];

Simply check if this query returns something? Typo in the profile name (maybe you have them with underscores or "Sales & Service")? Maybe there's no such profile in org at all (for example if you've created such one on production but the sandbox you're in was not refreshed afterwards)?

I'm afraid we can't help you more than that ;) It can't be even related to API versions, "seeAllData" etc because docs say Profiles are still visible.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top