function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Patrick Jones 37Patrick Jones 37 

illegal conversion from Case to List<case>

I wrote the following code to get a 10 char string, reformat it and then pass to the Case query. 
 
@RestResource(urlMapping='/caseLookupAni/*')
global with sharing class caseLookupAni {

   @HttpGet
  global static List<Case> getOpenCases() {
    //  String passedANI = RestContext.request.params.get('ANI');
    string passedANI = '5207739050';  
    String PhoneDigits;
    String nondigits = '[^0-9]';
    PhoneDigits = passedANI.replaceAll(nondigits,'');
    String npa = (PhoneDigits.substring(0,3));
    String nxx = (PhoneDigits.substring(3,6));
    String nxxArea = (PhoneDigits.substring(6,10));  
    String formattedANI = '('+ npa + ') '+ nxx + '-' + nxxArea;  

    Case contactPhone=[SELECT Id, CaseNumber, Subject, Status, ContactPhone, OwnerId, Owner.Name FROM Case WHERE ContactPhone=: formattedANI LIMIT 1];
    
   System.debug(cases);
   List<Case> cases = contactPhone.Cases;
    return cases;
      } 
   
}

When I look at the logging, I see that the variables are being assigned correctly up until the final assignment. I can see the correctly returned result prior ... when it comes to the last assignment,  I see this: "VARIABLE_ASSIGNMENT|[18]|cases|[]|0x3ef6276e" which debug shows as "0". I dont understand why this is happening. 

Thank you in advance for any guidance.
Regards,
PJ
Best Answer chosen by Patrick Jones 37
Amit Singh 1Amit Singh 1
Hi Patrick,

I got it what are you doing wrong in your code in Line no 16 where you are making a SOQL into Case object which retrurn only a single record. And you are assigning that case record to List of Cases that is gving an error. I have modified the code.

Use Below code.
@RestResource(urlMapping='/caseLookupAni/*')
global with sharing class caseLookupAni {

   @HttpGet
  global static List<Case> getOpenCases() {
    //  String passedANI = RestContext.request.params.get('ANI');
    string passedANI = '5207739050';  
    String PhoneDigits;
    String nondigits = '[^0-9]';
    PhoneDigits = passedANI.replaceAll(nondigits,'');
    String npa = (PhoneDigits.substring(0,3));
    String nxx = (PhoneDigits.substring(3,6));
    String nxxArea = (PhoneDigits.substring(6,10));  
    String formattedANI = '('+ npa + ') '+ nxx + '-' + nxxArea;  

    Case contactPhone=[SELECT Id, CaseNumber, Subject, Status, ContactPhone, OwnerId, Owner.Name FROM Case WHERE ContactPhone=: formattedANI LIMIT 1];
    
   System.debug(cases);
    List<Case> cases = new List<Case>{contactPhone};
    return cases;
      } 
   
}

Let me know if this works for you.

Thanks,
AMit Singh