Salesforce - 对象 UserRecordAccess 的 SOQL 查询语法错误
Salesforce - SOQL Query Syntax Error at Object UserRecordAccess
根据描述使用对象 "UserRecordAccess" [here] 的访问控制的 Salesforce 手册,我想获取一个列表
Salesforce 中给定 "User" 和 "Case" 的权限。
使用交互式 workbench 工具,我在构建此查询时收到以下语法错误:
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;
}
}
WHERE 子句中的 SOQL 查询将 return 一组 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 查询将获取案例 ID 并将其转换为列表类型。第二个将使用该列表。
根据描述使用对象 "UserRecordAccess" [here] 的访问控制的 Salesforce 手册,我想获取一个列表 Salesforce 中给定 "User" 和 "Case" 的权限。
使用交互式 workbench 工具,我在构建此查询时收到以下语法错误:
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;
}
}
WHERE 子句中的 SOQL 查询将 return 一组 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 查询将获取案例 ID 并将其转换为列表类型。第二个将使用该列表。