serviceterritory 对象上的 salesforce 更新触发器

发布于 2025-01-16 14:12:37 字数 1435 浏览 1 评论 0原文

我有以下要求,并且我是 SF 开发的新手,如果我的方法正确与否,则需要帮助。

当 ServiceTerritory 对象发生任何更新时,如果 ServiceTerritory 的 Time_Zone__c 字段与用户对象 TimeZoneSidKey 字段不匹配,则使用用户对象 TimeZoneSidKey 字段更新 ServiceTerritory 对象 Time_Zone__c 字段。

  1. ServiceTerritory 对象:具有标记为联系人对象中的 ID 字段的 Center_Instructor_Contact__c 字段。
  2. 联系人对象:具有 ID 字段和 AccountId 字段
  3. 用户对象:具有 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.

  1. ServiceTerritory object : has Center_Instructor_Contact__c field tagged to ID field in Contact object.
  2. Contact object : has ID field and AccountId field
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩物 2025-01-23 14:12:37

没有多少 Salesforce 实例有权访问“Field Service Lightning”,这是使用 ServiceTerritory 表的地方。如果您注册免费的 Salesforce Developer Edition,它可能不会存在。所以理解这个问题和提供帮助有点困难。另外,如果您编写“用户对象:具有 AccountId 字段”,听起来您正在使用 Experience Cloud(以前称为社区),这会进一步缩小专家范围。

所以它是服务区域 -> “向上”至->联系方式-> “向下”到(社区)->用户?

如果您使用社区用户,他们将拥有 AccountId 和 ContactId,无需通过帐户(事实上,这样您可能会得到愚蠢的结果...如果帐户中的所有联系人都不是社区启用的怎么办?他们是,但有不同的时区......)

尝试类似的事情,但你必须进行很多实验。并将您的代码更改为“更新前”运行,您将免费保存到数据库

List<ServiceTerritory> serviceTerritories; // passed to your function

Set<Id> contactIds = new Set<Id>();
for (ServiceTerritory st : serviceTerritories) {
    contactIds.add(st.Center_Instructor_Contact__c);
}
System.debug(contactIds);

// Grab all these Contacts and their community users (it's a related list so it'll be a subquery but really there will be at most one user
Map<Id, Contact> contacts = new Map<Id, Contact>([SELECT Id,
        (SELECT TimezoneSidKey FROM Users)
    FROM Contact
    WHERE Id IN :contactIds AND Id IN (SELECT ContactId FROM User)]);
    
// Loop again and check against "reference data"
for (ServiceTerritory st : serviceTerritories) {
    if(contacts.containsKey(st.Center_Instructor_Contact__c)){
        Contact c = contacts.get(st.Center_Instructor_Contact__c);
        System.debug(c);
        System.debug('comparing ' + st.Time_Zone__c + ' and ' + c.Users);
        if(st.Time_Zone__c != c.Users[0].TimezoneSidKey){
            System.debug('fixing ' + st.Id);
            st.Time_Zone__c = c.Users[0].TimezoneSidKey;
        }
    }
}

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

List<ServiceTerritory> serviceTerritories; // passed to your function

Set<Id> contactIds = new Set<Id>();
for (ServiceTerritory st : serviceTerritories) {
    contactIds.add(st.Center_Instructor_Contact__c);
}
System.debug(contactIds);

// Grab all these Contacts and their community users (it's a related list so it'll be a subquery but really there will be at most one user
Map<Id, Contact> contacts = new Map<Id, Contact>([SELECT Id,
        (SELECT TimezoneSidKey FROM Users)
    FROM Contact
    WHERE Id IN :contactIds AND Id IN (SELECT ContactId FROM User)]);
    
// Loop again and check against "reference data"
for (ServiceTerritory st : serviceTerritories) {
    if(contacts.containsKey(st.Center_Instructor_Contact__c)){
        Contact c = contacts.get(st.Center_Instructor_Contact__c);
        System.debug(c);
        System.debug('comparing ' + st.Time_Zone__c + ' and ' + c.Users);
        if(st.Time_Zone__c != c.Users[0].TimezoneSidKey){
            System.debug('fixing ' + st.Id);
            st.Time_Zone__c = c.Users[0].TimezoneSidKey;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文