I want to create a trigger to check if an account exist in another object prior to submission . The newmember object stores new member request but it has verify if there is an account once the newmember request is submitted for approval.
trigger VerifyAcc on Account (after update) {
Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'newmember'].Id;
if (recordTypeID =='newmember'){
MAP<Id,List<NewMember__c>>Mapnewmemberstoaccounts new MAP<Id,List<NewMember__c>>();
for(NewMember__c objNewMember:[select Id,name from NewMember__c where
Name IN:Trigger.newmap.keyset()])
{
if(Mapnewmemberstoaccounts.containsKey(objNewMember.Name)!= null)
{
CalloutException e = new CalloutException();
e.setMessage('There is no account for this member.');
throw e;
}
}
} else{
CalloutException e = new CalloutException();
e.setMessage('Couldnt Find anything.');
throw e;
}
}
Your assistance is highly appreciated.
trigger VerifyAcc on Account (after update) {
Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'newmember'].Id;
if (recordTypeID =='newmember'){
A record type Id will never be equal to the text "newmember". You probably mean to be iterating over Trigger.new
and checking for records whose Record Type Id matches this one.
MAP<Id,List<NewMember__c>>Mapnewmemberstoaccounts new MAP<Id,List<NewMember__c>>();
for(NewMember__c objNewMember:[select Id,name from NewMember__c where
Name IN:Trigger.newmap.keyset()])
Trigger.newMap.keySet()
is a Set<Id>
. Unless the Name field of the NewMember__c
object contains an Account Id, this query will return no data.
{
if(Mapnewmemberstoaccounts.containsKey(objNewMember.Name)!= null)
You never populate the variable Mapnewmemberstoaccounts
, so checking it here will not achieve anything. The method containsKey()
returns a Boolean. It will never return a null
value.
{
CalloutException e = new CalloutException();
e.setMessage('There is no account for this member.');
throw e;
Your code should not throw a CalloutException
for an error that has nothing to do with a callout. If you wish to stop a specific record from being committed to the database, use the addError()
method on that record alone. Throwing an exception will result in rolling back the entire transaction.
}
}
} else{
CalloutException e = new CalloutException();
e.setMessage('Couldnt Find anything.');
throw e;
Both arms of this if
statement are coded to throw an exception. That does not make sense; the result will be that (if the enclosing for
loop ever executes) you cannot insert any Accounts because your trigger always throws an exception.
In rough outline, your trigger should follow a logical path like this:
Create a collection variable
for each Account in the trigger set:
if the Account's record type is "New Member":
Add the Account's identifier to the set.
Query New Member records matching the Account identifiers.
Build a Map between Identifiers and New Member records.
for each Account in the trigger set:
if the Account's record type is "New Member":
Check if the Account's Identifier is in the New Member collection
If it is, call addError() for this Account