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
AtiqahAtiqah 

Illegal assignment from List<SObject> to List<ProcessInstance>

Hi, need your help for this error
Illegal assignment from List<SObject> to List<ProcessInstance> (46:31)
public with sharing class ProcessInstance {
    @AuraEnabled(cacheable=true)
    public static List<ProcessInstance> getCases(
        String relatedTo,
        String submittedDate,
        String submittedBy
    ) {
        String query;
        String condition = (String.isNotBlank(relatedTo)
            ? 'TargetObject.name LIKE \'' + '%' + relatedTo + '%\''
            : '');

        condition += (String.isNotBlank(submittedDate)
            ? (String.isNotBlank(condition) ? +' AND ' : '') +
              ' LastModifiedDate LIKE \'' +
              '%' +
              submittedDate +
              '%\''
            : '');

        condition += (String.isNotBlank(submittedBy)
            ? (String.isNotBlank(condition) ? +' AND ' : '') +
              ' SubmittedBy.name LIKE \'' +
              '%' +
              submittedBy +
              '%\''
            : '');

        System.debug('condition ' + condition);
        if (String.isNotBlank(condition)) {
            query =
                'SELECT TargetObject.name,LastModifiedDate,SubmittedBy.name FROM ProcessInstance WHERE ' +
                condition +
                ' ORDER BY LastModifiedDate';
        } else {
            query = 'SELECT TargetObject.name,LastModifiedDate,SubmittedBy.name FROM ProcessInstance ORDER BY LastModifiedDate LIMIT 200';
        }

        List<ProcessInstance> records = Database.query(query);
        return records;
    }
}

 
Maharajan CMaharajan C
Hi Atiqah,

I am able to sucessfully execute your code except below problem.

Your code looks good expect below line:
condition += (String.isNotBlank(submittedDate)
            ? (String.isNotBlank(condition) ? +' AND ' : '') +
              ' LastModifiedDate LIKE \'' +
              '%' +
              submittedDate +
              '%\''
            : '');

EXCEPTION: System.QueryException: like operator only valid on string field


LastModifiedDate is data time field type so you can't use the like operator here. Like operator will works only for string.

Thanks,
Maharajan.C

 
AtiqahAtiqah
Hi 

Still the same, I removed submitted date, still face the error
/**
 * @description       : 
 * @author            : wannur@topglove.com.my
 * @group             : 
 * @last modified on  : 11-25-2022
 * @last modified by  : wannur@topglove.com.my
**/
public with sharing class ProcessInstance {
    @AuraEnabled(cacheable=true)
    public static List<ProcessInstance> getCases(
        String relatedTo,
        String submittedBy
    ) {
        String query;
        String condition = (String.isNotBlank(relatedTo)
            ? 'TargetObject.name LIKE \'' + '%' + relatedTo + '%\''
            : '');

        condition += (String.isNotBlank(submittedBy)
            ? (String.isNotBlank(condition) ? +' AND ' : '') +
              ' SubmittedBy.name LIKE \'' +
              '%' +
              submittedBy +
              '%\''
            : '');

        System.debug('condition ' + condition);
        if (String.isNotBlank(condition)) {
            query =
                'SELECT TargetObject.name,LastModifiedDate,SubmittedBy.name FROM ProcessInstance WHERE ' +
                condition +
                ' ORDER BY LastModifiedDate';
        } else {
            query = 'SELECT TargetObject.name,LastModifiedDate,SubmittedBy.name FROM ProcessInstance ORDER BY LastModifiedDate LIMIT 200';
        }

        List<ProcessInstance> records = Database.query(query);
        return records;
    }
}

Error
Illegal assignment from List<SObject> to List<ProcessInstance> (37:31)