简体中文
Groovy代码示例
约 593 字大约 2 分钟
2026-01-09
一、返回类型为QueryTemplate
QueryTemplate作为关联查询的条件的返回结果,更加高效。
除非QueryTemplate的查询方式不能满足需求,否则优先推荐用使用QueryTemplate返回类型函数,用此方法返回的范围数据没有上限,并且执行效率高。
1.1 函数编写模板:
QueryTemplate template1 = QueryTemplate.AND(
["name":Operator.LIKE("测试")],
["field_g7Zeh__c":Operator.LTE("测试单行文本")]
)
QueryTemplate template2 = QueryTemplate.AND(
["owner":Operator.LIKE("1000")],
["record_type":Operator.EQ("default__c")]
)
//QueryTemplate使用OR条件需要单独开通,请联系销售人员下订单开通产品:【对象列表筛选支持或者】
QueryTemplate template3 = QueryTemplate.OR(template1,template2)
return template3
//查询条件支持
// product_id 为查找关联字段,查询模版支持筛选查找关联对象下的name字段符合某种条件的结果
QueryTemplate template = QueryTemplate.AND(
["product_id.name":Operator.EQ("测试")]
)
return template二、返回类型为集合(List)
不推荐使用List返回类型函数,用此方法返回的数据有上限500条,并且查询和使用id作为返回值的执行效率较低。
2.1 函数编写模板:
//定义id List
List objectIds = []
...
//写函数逻辑,并将可选择数据Id添加到objectIds里
objectIds.add()
//最后返回的数据结果为 List`<String>` 里面包含满足条件的数据id
return objectIds实际场景:报价单明细按产品分类限制可选择产品范围,不同业务类型的报价单明细选择不同分类的产品数据。
根据以上实际场景的模板案例:
//获取当前操作对象实例的字段值
String product = context.data.field_wPnHu__c
//根据条件查找数据,根据需要的业务逻辑查询出需要的数据
def ret = Fx.object.find("查找关联字段所对应的对象ApiName",[["field_1tG48__c":product]],100,0)
//如果查询错误直接return返回
if( ret[0] ){
Fx.log.info("查询异常")
return []
}
//定义id List
List objectIds = []
QueryResult result = ret[1] as QueryResult
//遍历查询结果,将所有Id添加到objectIds中
result.dataList.each{ item ->
Map map = item as Map
objectIds.add(map._id)
}
//最后返回objectIds
return objectIds三、返回类型为RangeRule
选择和新建查找关联字段时指定默认业务类型。
3.1 函数编写模板:
QueryTemplate template = QueryTemplate.AND(
["name":Operator.LIKE("测试")]
)
RangeRule rangeRule = RangeRule.builder()
.queryTemplate(template)
.recordType("record_cbxZ8__c") // 查找关联新建时默认用该业务类型
.build()
return rangeRule