1

I am having difficulties writing a test class for the following method of mine and any help would be greatly appreciated:

//Method to filter opportunities based on SalesRep access

private Map<Id, Opportunity> filterSalesRepOpptys(List<Opportunity> lstOptyToFilter,Oppty_Search_IntegrationOperation.Oppty_SearchData seaData){
    Map<Id,Opportunity> mapOfOptys = new Map<Id,Opportunity>(lstOptyToFilter);

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

    Map<Id, Opportunity> mapFilteredOpptys = new Map<Id, Opportunity>();
    List<Opportunity> limitedOpptys = new List<Opportunity>();
    List<User> salseRepbadgeID= null;
        salseRepbadgeID = [SELECT Id FROM User WHERE IsActive=true and FederationIdentifier =:seaData.primarySalesRep limit 1];

    Id srid = salseRepbadgeID[0].Id;

    Map<Id,UserRecordAccess> mapURA = new Map<Id,UserRecordAccess>([SELECT RecordId FROM UserRecordAccess WHERE HasReadAccess = true AND UserId =:srid AND RecordId=:mapOfOptys.keyset()]);

    Integer count= 0;

    for(UserRecordAccess ura : mapURA.values()){
        setUnSortedOpptysIds.add(ura.RecordId);
    }

     //Sort the Opportunities on order
    if(mapURA!=null && mapURA.size()>0){
            for(Opportunity opp : lstOptyToFilter){
                if(setUnSortedOpptysIds.contains(opp.Id)){
                    //lstfilteredOpptys.add(opp);
                    mapFilteredOpptys.put(opp.id, opp);
                    System.debug('*** Adding to mapFilteredOpptys mapOfOptys.get(oppId).Name = ' + opp.Name);
                    count++;
                if(count==50){
                    break;
                }
                }
            }
    }
    //return all list of opportunities
    System.debug('*** Sending all Opptyes mapFilteredOpptys.values() = ' + mapFilteredOpptys);
    return mapFilteredOpptys;
}

Here is what i currently have:

@isTest
private class OpportunitySearch_Ali{
  static testMethod void OpportunitySearch_Ali_Svc()
  {
      Oppty_Search_IntegrationOperation.Oppty_SearchData opptySearchData = new Oppty_Search_IntegrationOperation.Oppty_SearchData();
      opptySearchData.opptyName= 'test';
      opptySearchData.opptyNo = '123';
      opptySearchData.soldToPartyNo = '1234';
      opptySearchData.endCustPartyNo = '1232';
      opptySearchData.tier2PartyNo = '123';
      opptySearchData.distributionChannel = 'Direct';
      Oppty_Search_IntegrationOperation.performOpptySearch( opptySearchData);

      List<Opportunity> oSR = [select Name from Opportunity where Name = 'test'];
      System.assertEquals(oSR.size(), 0);

      Oppty_Search_IntegrationOperation.Oppty_SearchData sData = new Oppty_Search_IntegrationOperation.Oppty_SearchData();
      sData.opptyName= 'test';
      sData.opptyNo = '123';
      sData.soldToPartyNo = '1234';
      sData.endCustPartyNo = '1232';
      sData.tier2PartyNo = '123';
      sData.distributionChannel = 'Direct';
      Oppty_Search_IntegrationOperation.performOpptySearch(sData);


    //Insert Account records
    List<Account> accList = new List<Account>();
    accList.add(new Account(Name = 'TestAcount0',Party_Number__c='111',ShippingCountry = 'United States'));
    accList.add(new Account(Name = 'TestAcount1',Party_Number__c= '222',ShippingCountry = 'United States'));
    accList.add(new Account(Name = 'TestAcount3',Party_Number__c= '333',ShippingCountry = 'United States'));
    accList.add(new Account(Name = 'TestAcount4',Party_Number__c= '444',ShippingCountry = 'United States'));
    insert accList;


    //Insert Opportunity records
    List<Opportunity> oppList = new List<Opportunity>();
    oppList.add(new Opportunity (Name = 'Test Opportunity ', AccountId = accList[0].id, Sales_Force__c = 'EMC',StageName = 'Pipeline',CloseDate = System.today(),Amount = 500, Opportunity_Number__c = '9999999',Quote_Cart_Number__c = '1000'));
    oppList.add(new Opportunity (Name = 'Test Opportunity 1', AccountId = accList[1].id, Sales_Force__c = 'EMC',StageName = 'Pipeline',CloseDate = System.today(),Amount = 600, Opportunity_Number__c = '8888888',Quote_Cart_Number__c = '1000'));
    oppList.add(new Opportunity (Name = 'Test Opportunity 2', AccountId = accList[2].id, Sales_Force__c = 'EMC',StageName = 'Pipeline',CloseDate = System.today(),Amount = 700, Opportunity_Number__c = '7777777'));
    oppList.add(new Opportunity (Name = 'Test Opportunity 3', AccountId = accList[3].id, Sales_Force__c = 'EMC',StageName = 'Pipeline',CloseDate = System.today(),Amount = 800, Opportunity_Number__c = '6666666',Quote_Cart_Number__c = '3000'));

    insert oppList;
    List<Opportunity> oppListQueried = new List<Opportunity>();
     oppListQueried = [ Select Id,Name,AccountId,Sales_Force__c,StageName,Amount,Opportunity_Number__c,Quote_Cart_Number__c from Opportunity order by Name];


}
1

Looking at your test class, it seems like you have created the required test data. Now you just have to call filterSalesRepOpptys method from your test class as shown below - Add below code to your test class

  Test.startTest();
    filterSalesRepOpptys(//parametrers for this method);
    Test.stopTest();

Test.startTest() and Test.stopTest() method are used when tesing governor limits.

Use System.assert method to check whether you are getting the desired output.

  • Thanks for the reply! I have added the following code to my test class but am getting an error: filterSalesRepOpptys(oppList, opptySearchData); This is the error that i'm receiving: Method does not exist or incorrect signature: filterSalesRepOpptys(List<Opportunity>, Oppty_Search_IntegrationOperation.Oppty_SearchData) Do you know what I am doing wrong here? – RayRay May 26 '15 at 7:22
  • Nevermind, I think i got it working now! Thanks again – RayRay May 26 '15 at 8:16
  • ok, great. Let me know if you face any problem. – Rohit Patil May 26 '15 at 8:25

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.