基本 Apex 对象 ID
快速提问。在下面的代码中,您可以看到 for 循环(它获取 newTimecards 中的所有记录并将它们作为名为 timecard 的变量)并将 Resource_c 添加到 resourceIds 集中。我对这个对象如何被视为 ID 数据类型感到困惑。当在 Salesforce 中创建对象时,它是否会自动创建一个 ID,以便它知道可以将 Resource_c ID 添加到集合中?请注意,Resource_c 对象中还有一个名为 Resource_ID_c 的字段。 Timecard_c 中的 Resource_c 是主从数据类型。 Resource_c 是 Timecard_c 的父级。
现在我想了一下,resourceIds.add(timecard.Resource_c),是否引用了两个对象之间的关系,然后搜索Resource_c并自动添加ID字段Resource_ID_c,因为它是唯一字段?
感谢您的帮助。
public class TimecardManager {
public class TimecardException extends Exception {}
public static void handleTimecardChange(List<Timecard__c> oldTimecards,
List<Timecard__c> newTimecards) {
Set<ID> resourceIds = new Set<ID>();
for (Timecard__c timecard : newTimecards) {
resourceIds.add(timecard.Resource__c);
}
Quick Question. In the below code, you can see that the for loop (which takes all of the records in newTimecards and puts them as a variable called timecard) and adds the Resource_c to the resourceIds set. I'm confused about how this object is considered an ID data type. When an object is made in Salesforce does it automatically have an ID made, so that it knows Resource_c ID can be added to a set? Note that within the Resource_c Object there is also a field called Resource_ID_c. Resource_c within Timecard_c is a Master-Detail data type. Resource_c is the parent of Timecard_c.
Now that I think about it, resourceIds.add(timecard.Resource_c), does that reference the relationship between the two objects and then searches through Resource_c and adds the ID field Resource_ID_c automactically since it's a unique field?
Thanks for your help.
public class TimecardManager {
public class TimecardException extends Exception {}
public static void handleTimecardChange(List<Timecard__c> oldTimecards,
List<Timecard__c> newTimecards) {
Set<ID> resourceIds = new Set<ID>();
for (Timecard__c timecard : newTimecards) {
resourceIds.add(timecard.Resource__c);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个对象实例(这意味着每个对象实例,包括工厂实例)都有一个唯一的组织级别 ID,其字段名称始终为
Id
,由 Apex 类型ID
覆盖,并且是一个15 个字符的区分大小写的字符串,也有一个 18 个字符的不区分大小写的表示形式。前三个字符是对象前缀代码(例如,案例为 500),因此同一对象的所有实例共享相同的前缀。您可以在整个 SF 中看到这些值(例如在 https://na1.salesforce.com/02s7000000BW59L 中URL中的02s7000000BW59L
就是ID)。当使用 INSERT DML 操作创建对象实例时,销售人员会根据前缀和下一个可用的事务子 ID 自动分配唯一值,这一切对您来说都是透明的。不要将其与对象
Name
字段混淆,对象Name
字段是您在创建对象时定义的字段,可以自动递增等(例如 MYOBJ-{00000}),并且可以具有对于用户来说,比神秘的 ID 更有意义当您创建查找或主从关系时,用于链接两个实例的是 ID,而不是名称。在上面的示例中,Resource__c 似乎是该查找字段,它包含行主的 Id 值。
该代码的作用是枚举时间线中使用的所有资源并构建一组 ID,其目的很可能是通过
WHERE Id IN :resourceIds
子句用于从 master 加载资源详细信息桌子。Every object instance (and that means EVERY, including factory ones) has a unique organization level ID, whose field name is always
Id
, is covered by Apex typeID
and is a case-sensitive string of 15 characters that also has an 18 character case-insensitive representation. The first three characters are object prefix code (e.g. 500 for a Case) so all instances of the same object share the same prefix. You see these values all across SF (for example in https://na1.salesforce.com/02s7000000BW59L the02s7000000BW59L
in the URL is the ID). When an instance of the object is created using INSERT DML operation, the salesforce automatically assigns unique value based on the prefix and the next available transactional sub ID, it all happens transparently to you.This is not to be confused with object
Name
field which is a field you define when you create an object and which can be auto-incremented and so on (e.g. MYOBJ-{00000}) and which can have more meaning to a user than a cryptic IDWhen you create a lookup or master-detail relationship it is ID that is being used to link the two instances, not the Name. In the above example Resource__c seems to be that lookup field and it contains Id value of row's master.
What the code does is it enumerates all resources used in timelines and builds a set of their IDs, the purpose of which is most probably to be used via
WHERE Id IN :resourceIds
clause to load resource details from master table.mmix 的答案很好地概述了 ID 是什么以及它来自哪里。回答我认为是您的具体问题:
任何时候存在从一个对象到另一个对象的引用(就像这里,在 Timecard_c 和 Resource_c 之间),表示引用的字段将是一个 ID。因此,调用
resourceIds.add(timecard.Resource__c)
的for
循环只是构建您的 ID 集(那些 15 个字符的字符串)。timecard.Resource__c
不会通过 Resource__c 表查找 ID,timecard.Resource__c
就是 ID。mmix's answer is a great overview to what an ID is and where it comes from. To answer what I think is your specific question:
Any time there is a reference from one object to another (like here, between Timecard_c and Resource_c), the field representing the reference will be an ID. So, the
for
loop that callsresourceIds.add(timecard.Resource__c)
is just building up your set of ID's (those 15-character strings). Thetimecard.Resource__c
doesn't look through the Resource__c table to find the ID,timecard.Resource__c
is the ID.