简体   繁体   English

Salesforce:在相关列表上触发

[英]Salesforce: trigger on related list

Suppose I have two objects 假设我有两个对象
1.Account- standard object[it has a field name Status_ c which is a picklist having value inprogress and closed] 1.Account-standard对象[其字段名称为Status_ c,这是一个值正在进行且已关闭的选择列表]
2.Client _c - custom object[it also have same field name Status__c which is a picklist having value inprogress and closed] 2.Client _c-自定义对象[它也具有相同的字段名称Status__c,这是一个值正在进行且已关闭的选择列表]

and Client__c has lookup to Account name which means Account has a related list of client object . 并且Client__c具有对Account name的查找,这意味着Account具有与client object相关的列表。

My question is : 我的问题是:
I want to write a trigger where if I put account status to "closed" I can not put client status to "closed",it should throw an error message on client object or if I put client status to closed I can not put account status to closed vice versa. 我想编写一个触发器,如果​​我将帐户状态设置为“关闭”,则不能将客户状态设置为“关闭”,则应该在客户端对象上抛出错误消息,或者如果我将客户端状态设置为关闭,则不能将帐户状态设置为反之亦然。

Can any one please help me to write a trigger on this?? 谁能帮我写一个触发器吗?

Conceptually, I think what you are looking to do is set up Validation Rules on both of those objects. 从概念上讲,我认为您想要做的是在这两个对象上都设置验证规则。 Your validation rule on Client_ c should be pretty simple: TEXT(Status _c) == 'Closed' && TEXT(Account_ c.Status _c) == 'Closed' 您在Client_ c上的验证规则应该非常简单:TEXT(Status _c)=='Closed'&& TEXT(Account_ c.Status _c)=='Closed'

The more interesting piece is how you handle making sure none of your related items are Closed when you move the Account to Closed. 更有趣的是,如何将帐户移至已关闭状态时确保所有相关项目都未关闭。 I tend to prefer creating a field on the Account that keeps track of the status of the related items (checkbox) that basically tells me whether it is valid for me to change my status or not. 我倾向于在帐户上创建一个字段,以跟踪相关项目的状态(复选框),该字段基本上告诉我更改状态对我是否有效。 In this case, the validation rule becomes pretty simple. 在这种情况下,验证规则变得非常简单。 In order to set that boolean value, I end up using a Trigger on Client__c that basically just grabs all the Accounts when a Client is being modified in the batch (taking into account both inserts, upserts, and deletes): 为了设置该布尔值,我最终在Client__c上使用了一个Trigger,当批量修改Client时(考虑到插入,upsert和Deletes),基本上只捕获所有Accounts:

SELECT Account__c.Id FROM Client__c WHERE Id =: Trigger.new OR Id =: Trigger.old

Then create a Set of all the Account Ids (in this example, named accounts), and run a query to retrieve ALL Clients related to those Ids (in a single query to ensure you don't hit SOQL limits). 然后创建所有帐户ID的集合(在本示例中为命名帐户),并运行查询以检索与这些ID相关的所有客户端(在单个查询中,以确保您未达到SOQL限制)。

SELECT Account__c.Id, Status__c FROM Client__c WHERE Account__c.Id =: accounts

From the results of this, you will iterate over all of the entries, tossing them into a Map keyed by the Account Id where the value is a List of Clients. 从结果中,您将遍历所有条目,并将它们扔到以客户ID为关键字的Map中,其中值为客户列表。 When you are done, run a query to get all accounts based on the "accounts" list from earlier (which was just a list of strings, not actual Accounts), subsequently iterate over all the Clients associated with that Account, and if a Client is marked as Closed, you will update the metadata of that Account accordingly. 完成后,运行查询以根据前面的“帐户”列表(只是字符串列表,而不是实际帐户)获取所有帐户,然后遍历与该帐户关联的所有客户端,以及是否有客户端标记为已关闭,您将相应地更新该帐户的元数据。 If no Clients are closed, the Account will be marked as such. 如果没有客户被关闭,该帐户将被标记为此类。 Once you are finished, run an update statement to update the list of Accounts that you have modified. 完成后,运行更新语句以更新您已修改的帐户列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM