serviceterritory 对象上的 salesforce 更新触发器
我有以下要求,并且我是 SF 开发的新手,如果我的方法正确与否,则需要帮助。
当 ServiceTerritory 对象发生任何更新时,如果 ServiceTerritory 的 Time_Zone__c 字段与用户对象 TimeZoneSidKey 字段不匹配,则使用用户对象 TimeZoneSidKey 字段更新 ServiceTerritory 对象 Time_Zone__c 字段。
- ServiceTerritory 对象:具有标记为联系人对象中的 ID 字段的 Center_Instructor_Contact__c 字段。
- 联系人对象:具有 ID 字段和 AccountId 字段
- 用户对象:具有 AccountId 字段
public static void afterUpdate(List<ServiceTerritory> serviceTerritories, Map<Id, ServiceTerritory> oldRecords) {
Set<Id> recIds = new Set<Id>();
for (ServiceTerritory record : serviceTerritories) {
recIds.add(record.Id);
}
Set<Id> STMembers = new Set<Id>();
for (ServiceTerritory member : [SELECT Id, Center_Instructor_Contact__c FROM ServiceTerritory WHERE Id IN :recIds]) {
STMembers.add(member.Center_Instructor_Contact__c);
}
//Contact object : has ID field and AccountId field
Set<Id> ContactIDs = new Set<Id>();
for (Contact Cnt : [SELECT AccountId FROM Contact WHERE Id IN :STMembers]) {
ContactIDs.add(Cnt.AccountId);
}
//User Object : has AccountId field
Set<Id> UserIDs = new Set<Id>();
for (User Cnt : [SELECT AccountId, TimeZoneSidKey FROM User WHERE AccountId IN :ContactIDs]) {
UserIDs.add(Cnt.AccountId);
}
}
,如果对象之间的时区不匹配,这里如何比较和更新 ServiceTerritory 对象。
I have below requirement and I'm new to SF development, need assistance if my approach is correct or not.
When any update happens on ServiceTerritory object, if the Time_Zone__c field of ServiceTerritory is not matching with User Object TimeZoneSidKey field, then update ServiceTerritory object Time_Zone__c field with User Object TimeZoneSidKey field.
- ServiceTerritory object : has Center_Instructor_Contact__c field tagged to ID field in Contact object.
- Contact object : has ID field and AccountId field
- User Object : has AccountId field
public static void afterUpdate(List<ServiceTerritory> serviceTerritories, Map<Id, ServiceTerritory> oldRecords) {
Set<Id> recIds = new Set<Id>();
for (ServiceTerritory record : serviceTerritories) {
recIds.add(record.Id);
}
Set<Id> STMembers = new Set<Id>();
for (ServiceTerritory member : [SELECT Id, Center_Instructor_Contact__c FROM ServiceTerritory WHERE Id IN :recIds]) {
STMembers.add(member.Center_Instructor_Contact__c);
}
//Contact object : has ID field and AccountId field
Set<Id> ContactIDs = new Set<Id>();
for (Contact Cnt : [SELECT AccountId FROM Contact WHERE Id IN :STMembers]) {
ContactIDs.add(Cnt.AccountId);
}
//User Object : has AccountId field
Set<Id> UserIDs = new Set<Id>();
for (User Cnt : [SELECT AccountId, TimeZoneSidKey FROM User WHERE AccountId IN :ContactIDs]) {
UserIDs.add(Cnt.AccountId);
}
}
and here how to compare and update ServiceTerritory object if the timezone is not matching between the objects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有多少 Salesforce 实例有权访问“Field Service Lightning”,这是使用
ServiceTerritory
表的地方。如果您注册免费的 Salesforce Developer Edition,它可能不会存在。所以理解这个问题和提供帮助有点困难。另外,如果您编写“用户对象:具有 AccountId 字段”,听起来您正在使用 Experience Cloud(以前称为社区),这会进一步缩小专家范围。所以它是
服务区域 -> “向上”至->联系方式-> “向下”到(社区)->用户?
如果您使用社区用户,他们将拥有 AccountId 和 ContactId,无需通过帐户(事实上,这样您可能会得到愚蠢的结果...如果帐户中的所有联系人都不是社区启用的怎么办?他们是,但有不同的时区......)
尝试类似的事情,但你必须进行很多实验。并将您的代码更改为“更新前”运行,您将免费保存到数据库
Not many Salesforce instances will have access to "Field Service Lightning" which is where
ServiceTerritory
table is used. If you sign up for free Salesforce Developer Edition it probably won't exist there. So it's bit hard to understand the question and help. Plus if you wrote "User Object : has AccountId field" it sounds like you're using Experience Cloud (formerly known as communities), that narrows down the specialists even more.So it's
Service Territory -> "up" to -> Contact -> "down" to (community) -> User
?If you're using community users they'll have AccountId and ContactId in them, no need going via Account (and in fact you could get stupid results that way... What if not all contacts in account are community-enabled, what if they are but have different timezones...)
Try something like that but you'll have to experiment a lot. And change your code to run "before update", you'll get save to database for free