trigger DetectJellyBeanDuplicates_BeforeInsert on JellyBean__c (before insert) {
// Obtain the record that was just inserted. We will search for similar records below.
JellyBean__c newlyCreatedJellyBean = trigger.new[0];
// Most duplicate records tend to occur within 24 hours.
DateTime oneDayAgo = System.now().addHours(-24);
// See if a similar record was created for the same user. Use as much criteria as possible.
JellyBean__c[] existingJellyBeans = [
SELECT Id
FROM JellyBean__c
WHERE Color__c = :newlyCreatedJellyBean.Color__c
AND CreatedById = :System.UserInfo.getUserId()
AND CreatedDate > :oneDayAgo
LIMIT 1
];
if (existingJellyBeans.size() > 0) {
// Duplicate was detected. Throw an error.
newlyCreatedJellyBean.addError('Duplicate detected! Please fix on mobile!');
}
}
See the BEFORE INSERT Trigger behavior in the app in the Videos for Workarounds for duplicate records created in the Field Service app article .
trigger DetectJellyBeanDuplicates_AfterInsert on JellyBean__c (after insert) {
// Obtain the record that was just inserted. We will search for similar records below.
JellyBean__c newlyCreatedJellyBean = Trigger.New[0];
// Most duplicate records tend to occur within 24 hours.
DateTime oneDayAgo = System.now().addHours(-24);
// See if a similar record was created for the same user. Use as much criteria as possible.
JellyBean__c[] existingJellyBeans = [
SELECT Id
FROM JellyBean__c
WHERE Color__c = :newlyCreatedJellyBean.Color__c
AND CreatedById = :System.UserInfo.getUserId()
AND CreatedDate > :oneDayAgo
AND Id != :newlyCreatedJellyBean.Id
LIMIT 1
];
if (existingJellyBeans.size() > 0) {
// Duplicate was detected. Chances are, mobile doesn't even yet know about the duplicate
// and the record that was just inserted prior to calling this trigger will be successful.
// Therefore, do something with the detected duplicate -- delete it, mark it as inactive,
// send to a review queue, etc.
delete existingJellyBeans;
} else {
newlyCreatedJellyBean.addError('Did not work: ' + newlyCreatedJellyBean.Id);
}
}
See the AFTER INSERT Trigger video in the Videos for Workarounds for duplicate records created in the Field Service app article.
trigger DetectJellyBeanDuplicates_BeforeInsert on JellyBean__c (before insert) {
// Obtain the record that was just inserted. We will search for similar records below.
JellyBean__c newlyCreatedJellyBean = trigger.new[0];
// See if a similar record was created for the same unique id (and user id)
JellyBean__c[] existingJellyBeans = [
SELECT Id
FROM JellyBean__c
WHERE UniqueId__c = :newlyCreatedJellyBean.UniqueId__c
AND CreatedById = :System.UserInfo.getUserId()
LIMIT 1
];
if (existingJellyBeans.size() > 0) {
// Duplicate was detected. Throw an error.
newlyCreatedJellyBean.addError('Duplicate detected! Please fix on mobile!');
}
}
000389435

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.