0

Below is my test class method:

static testMethod void test_createCaseWithPPAs_UseCase1(){
    List<Post_Record__c> post_record_Obj  =  [SELECT Id  from Post_Record__c];
    System.assertEquals(true,post_record_Obj.size()>0);
    PPA_UploadController obj01 = new PPA_UploadController();
   Account acc = new Account();
      acc.Name = 'Test Name';
      insert acc;
      case cs3 = new case();
      cs3.Status = 'New';
      cs3.Origin = 'Web';
      cs3.AccountId = acc.id;
      insert cs3;
    PPA_UploadController.createCaseWithPPAs(new list<string>, acc.id, 'testdata','[email protected]');
  }

From the above test method, I'm getting the error in last line which says PPA_UploadController.createCasewithPPAs(arguments).

I'm getting the error as follows

System.QueryException: List has more than 1 row for assignment to SObject

This error is related to the apex class method which is as follows

public static String createCaseWithPPAs(List<String> recordIds, String acctId, String webName, String webEmail) {
        System.debug('creating new Case...');
        Group ppaQueue = [SELECT Id FROM Group WHERE Type='Queue' AND Name LIKE '%PPA%'];
        Case newCase = new Case(
            C3MS_Case_Category__c = 'Purchase Adjustment',
            SuppliedName = webName,
            SuppliedEmail = webEmail,
            AccountId = acctId
        );
        insert newCase;

        List<Adjustment_Object__c> newPPAs = [SELECT Id FROM Adjustment_Object__c WHERE Id IN :recordIds];
        for (Adjustment_Object__c ppa : newPPAs) {
            ppa.Associated_Case__c = newCase.Id;
        }
        update newPPAs;

        return newCase.Id;
    }

In the above apex class, I have a PPAQueue variable which stores queues which are having term 'PPA' in their name. I have such queues around 6 which matches this condition. This PPAQueue, I'm using it in lightning component.

I can limit this query by giving limit condition but is there any way I can get this done without passing limit in my apex class.

Please suggest

1
  • 1
    Just change Group ppaQueue = [SELECT Id ... to List<Group> ppaQueues = [SELECT Id ... to get back all the PPA queue instances and modify any use of "ppaQueue" appropriately (there aren't any usages in the current code shared in your question).
    – Phil W
    Commented Jun 29, 2020 at 11:12

1 Answer 1

1

The reason behind this is SOQL returning more than 1 row. Ideally you should always use a List where you are not sure of limits and make sure to have empty check before the usage -

Group[] ppaQueue = [SELECT Id FROM Group WHERE Type='Queue' AND Name LIKE '%PPA%'];
if(ppaQueue.isEmpty()){
  return;
}
1
  • 2
    Avoid using the word "null" incorrectly. It's an empty check. The result of a query is never a null list.
    – sfdcfox
    Commented Jun 29, 2020 at 13:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .