Salesforce - 对象UserRecordAccess上的SOQL查询语法错误

时间:2017-12-15 20:07:00

标签: salesforce soql

根据使用对象“UserRecordAccess” [here]描述访问控制的Salesforce手册,我想获取一个列表 Salesforce中给定“用户”和“案例”的权限。

使用交互式工作台工具,在构造此查询时收到以下语法错误:

SOQL查询:

SELECT RecordId, HasReadAccess FROM  UserRecordAccess WHERE UserId = 'abc123xyz..' AND RecordId in (SELECT Id FROM Case WHERE CaseNumber = '10001026')

Workbench上的验证消息:

MALFORMED_QUERY: The left operand field in the where expression for outer query should be an id field, cannot use: 'RecordId'

SOQL中是否有替代语法可用于构建此类嵌套查询?

找到解决方案: 作为解决方案路径,按照下面的答案,我在带有REST接口的APEX脚本中编写了一个代码,该接口按顺序执行三个SOQL查询。见代码:

@RestResource(urlMapping='/sfcheckap/*')
// sample:  /services/apexrest/sfcheckap/00001028&myid@red.com  
//
global with sharing class MyRestResource1 {

    @HttpGet
    global static Boolean doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String myinput = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        List<String> inputList = myinput.split('&');
        String mycase = inputList[0];
        String myemail = inputList[1]; 
        sObject s1 = [SELECT Id FROM User WHERE Email = :myemail]; 
        ID MyUserId = s1.ID; 
        Case s2 = [SELECT Id FROM Case WHERE CaseNumber = :mycase LIMIT 1]; 
        ID MyCaseId = s2.ID; 
        System.debug('Dynamic MyCaseId is: ' + MyCaseId);
        UserRecordAccess s3 = [SELECT RecordId, HasReadAccess FROM UserRecordAccess 
                               WHERE UserId = :MyUserId AND RecordId = :MyCaseId]; 
        ID MyRecordId = s3.ID;
        Boolean result = s3.HasReadAccess;   
        return result;        
    }  
}

1 个答案:

答案 0 :(得分:1)

WHERE子句中的SOQL查询将返回一组id(SET<ID>),然后将该字段与该集合中的一个实例进行比较,我应该使用相同的数据类型。

以下是UserAccessRecord sObject的样子:

global class UserRecordAccess extends SObject 
{
    global Boolean HasAllAccess;
    global Boolean HasDeleteAccess;
    global Boolean HasEditAccess;
    global Boolean HasReadAccess;
    global Boolean HasTransferAccess;
    global String MaxAccessLevel;
    global String RecordId;
    global SObjectType SObjectType;
    global User User;
    global Id UserId;
    global APTTaskTemplate__c UserRecordAccess;

    global UserRecordAccess()
    {
    }
}

正如您所看到的,UserAccessRecord.RecordId的类型为String

你应该能够在2个SOQL查询中执行此操作我相信,首先SOQL查询将获得Case Ids并将其转换为List Type。第二个会使用该列表。