防止自定义对象中出现重复记录
我有一个自定义对象。月份_c、项目_c、联系人_c 和角色_c 等字段的组合确定记录是唯一的。
我可以在插入之前编写一个触发器,以检查是否存在任何具有相同组合的记录。我想问的问题是
如何才能停止插入。一旦我发现已经有一条记录,那么它不应该插入该记录。它不需要抛出/显示错误。
谢谢普拉迪
I have a custom object. There are combination of fields like month_c,Project_c,contact_c and role_c which determine the record as unique.
I can write a trigger on before insert to check if there are any records with the same combination already existing. The question i wanted to ask was
how can i make the insertion stop. once i find there is already an record then it should just not insert the record. it doesnt need to throw / show an error.
Thanks
Prady
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尽管其他人已经用更好的解决方案回答了这个问题(通常非代码解决方案对于系统的最终用户来说更灵活),但阻止插入特定记录的答案是向该记录上的字段添加错误。
例如,如果我要插入帐户,我可能会遇到这样的情况:
另一种选择是创建一个扩展 Exception 的类,然后抛出该异常类的新实例,这将阻止所有记录传递给触发器进行处理,而不是特定记录。
Although others have answered this with better solutions (generally non-code solutions are more flexible for the end users of the system), the answer to stop a particular record from being inserted is to add an error to a field on that record.
For instance, if I was inserting accounts I might have something like this:
Another alternative is to create a class extending
Exception
, and then throw a new instance of that exception class, this will prevent all of the records passed to the trigger from being processed as opposed to specific records.几天前,我发现了一个非常有用且简单的解决方案(在 ForceTree.com 文章中),它使我不必编写一个触发器。它允许您使用工作流程规则和自定义字段组合字段以检查唯一性。
这是一个演练:
http://www.forcetree.com/2010/07 /unique-field-combination-in-salesforce.html
A few days ago I found an incredibly useful and simple solution (in a ForceTree.com article) that prevented me from having to write a trigger. It allows you to combine fields to check for uniqueness using a workflow rule and a custom field.
Here's a walk-through:
http://www.forcetree.com/2010/07/unique-field-combination-in-salesforce.html
我在 Salesforce 也有类似的情况。它是通过创建一个字段来处理的,该字段包含一个由保证唯一性所需的所有值组成的值(在您的情况下为 Month_c,Project_c,contact_c 和 role_c )。然后,选择该字段的“唯一”复选框。重复项现在将被扔进垃圾桶。
就我而言,这个新字段由外部程序填充(并推送到 Salesforce)。在您的情况下,您需要填写触发器中的值。我认为这比在触发器中执行 SOQL 查询更有效,但我没有进行任何检查来确认这一点。
I have a similar situation in Salesforce. It is handled by creating a field that contains a value composed of all the values needed to guarantee uniqueness (in your case, month_c,Project_c,contact_c and role_c ). You then select the "Unique" checkbox for that field. Duplicates will now end up in the trash.
In my case, this new field is filled in (and pushed into Salesforce) by an external program. In your case, you'll need to fill in the value in your trigger. I think this is more efficient than doing SOQL queries in a trigger, but I have not done any checking to confirm this.