You need to sign in to do that
Don't have an account?
LauraBTN
Trigger help please - need to have this run for only personal accounts
I am a true newbie in the Trigger world - I have this trigger I pulled together that works well when you are only checking Personal account types - has issues with Business accounts - I need to modify it so it will run only on Personal Accounts. I have a custom field that I populate with (FirstName, LastName and Phone) to check for duplicates. Would appreciate any help given:
Trigger:
trigger accountDuplicateFullNamePreventer on Account (before insert, before update) {
Map<String, Account> accountMap = new Map<String, Account>();
for (Account account : System.Trigger.New) {
// Make sure we don't treat an FullName__c that
// isn't changing during an update as a duplicate.
if ((account.FullName__c !=null) && (System.Trigger.isInsert || (account.FullName__c != System.Trigger.oldMap.get(account.Id).FullName__c))) {
//make sure another new account isn't also a duplicate
if (accountMap.containsKey(account.FullName__c)) {
account.FullName__c.addError('Another new account has the '
+ 'same Name.');
} else {
accountMap.put(account.FullName__c, account);
}
}
}
//Using a single database query, find all the accounts in
//the database that have the same FullName as any
// of the accounts being inserted or updated.
for (Account account : [SELECT FullName__c FROM Account WHERE FullName__c IN :accountMap.KeySet()]) {
Account newAccount=accountMap.get(account.FullName__c);
newAccount.FullName__c.addError('An account with this Name and Phone Number already exists.');
}
}
Trigger:
trigger accountDuplicateFullNamePreventer on Account (before insert, before update) {
Map<String, Account> accountMap = new Map<String, Account>();
for (Account account : System.Trigger.New) {
// Make sure we don't treat an FullName__c that
// isn't changing during an update as a duplicate.
if ((account.FullName__c !=null) && (System.Trigger.isInsert || (account.FullName__c != System.Trigger.oldMap.get(account.Id).FullName__c))) {
//make sure another new account isn't also a duplicate
if (accountMap.containsKey(account.FullName__c)) {
account.FullName__c.addError('Another new account has the '
+ 'same Name.');
} else {
accountMap.put(account.FullName__c, account);
}
}
}
//Using a single database query, find all the accounts in
//the database that have the same FullName as any
// of the accounts being inserted or updated.
for (Account account : [SELECT FullName__c FROM Account WHERE FullName__c IN :accountMap.KeySet()]) {
Account newAccount=accountMap.get(account.FullName__c);
newAccount.FullName__c.addError('An account with this Name and Phone Number already exists.');
}
}
The only way to track this is the API names of Fields of Person account which end with a _pc ,
See the code and information in the below link,
You can have a loop with condition if field contains _pc , etc
// Get the __pc field first if this matches as both Account and Contact can have the same custom field like PaymentMethod__c
String pcFieldName = fieldName.replace('__c', '__pc');
http://force746.wordpress.com/2014/02/19/invoke-contact-triggers-logic-on-person-accounts/
Also see the discussion,
http://salesforce.stackexchange.com/questions/1209/is-there-any-way-to-invoke-contact-triggers-on-a-person-account
Regards,
Ashish