You need to sign in to do that
Don't have an account?

Getting a unique record Id in an after update trigger
I am trying to reform this trigger to remove the SOQL queries from inside the for loop. I need all the contacts for the account that is the parent of the contact record being accted on by the trigger in a list so I can test them all for the status of a set of fields. I can't figure out how to set a variable to the id of contact the trigger is acting on. here is the codebut you really only need to look at the stuff in bold:
rigger update_account_after on Contact (after insert, after update, after delete) {
map<Id, date> AcctNoAppeal = new map<Id, date>();
map<Id, boolean> AcctAnonymous = new map<Id,boolean>();
map<Id, boolean> AcctNoMail = new map<Id, boolean>();
boolean anon = null;
boolean nomail = null;
date noappeal = null;
//Contact cforlist = ??? I need the id of the record the trigger is acting on. I am trying to use it to make a list of the all the contacts //related to its parent account
//List<Contact> contactlist = new List<contact>();
//IF (trigger.isupdate || trigger.isdelete){
//contactlist = [Select ID, anonymous__c, no_appeal__c, no_mail__c FROM contact where accountid = :cforlist.accountid];
//}
for(contact c: trigger.isDelete ? trigger.Old : trigger.new){
//Account updateacc = [Select ID , anonymous__c, no_mail__c, no_appeal__c from Account where id = :c.accountid];
If (c.anonymous__c == true || c.no_mail__c != null || c.no_appeal__c != null) {
//if the contact is being inserted then update account according the contacts values
If (trigger.isinsert){
if (c.anonymous__c == true) AcctAnonymous.put(c.accountid, c.anonymous__c);
if(c.no_mail__c != null) {nomail = true;
AcctNoMail.put(c.accountid, nomail);
}
AcctNoAppeal.put(c.accountid, c.no_appeal__c);
}//insert
//the contact is being updated or deleted. iterate the contact list and set variables
//update according to the variables
If (trigger.isupdate || trigger.isdelete) {
list<contact> contactlist = [Select ID, anonymous__c, no_appeal__c, no_mail__c FROM contact where accountid = :c.accountid];
for(contact con :contactlist) {
if(con.anonymous__c == true) anon = true;
if(con.no_mail__c != null) nomail = true;
If (noappeal != null) {
if (con.no_appeal__c >= noappeal) noappeal = con.no_appeal__c;
}
else if (con.no_appeal__c != null) noappeal = con.no_appeal__c;
}
IF (anon == true) AcctAnonymous.put(c.accountid,anon);
else {anon = false;
AcctAnonymous.put(c.accountid,anon);
}
IF (nomail == true) AcctNoMail.put(c.accountid, nomail);
else {nomail = false;
AcctNoMail.put(c.accountid, nomail);
}
IF (noappeal != null) AcctNoAppeal.put(c.accountid, c.no_appeal__c);
else {noappeal = null;
AcctNoAppeal.put(c.accountid, c.no_appeal__c);
}
}//update and delete
}//if statement
}//for loop
List<Account> AcctAnon = [SELECT Id, anonymous__c FROM Account WHERE Id IN :AcctAnonymous.KeySet()];
List<Account> AcctnoMaillist = [SELECT Id, no_mail__c FROM Account WHERE Id IN :AcctNoMail.KeySet()];
List<Account> AcctnoAppeallist = [SELECT Id, no_appeal__c FROM Account WHERE Id IN :AcctNoAppeal.KeySet()];
List<Account> AccountUpdateList = new List<Account>();
for(Account a: AcctAnon)
{
a.anonymous__c = AcctAnonymous.get(a.id);
a.anonymous__c = Anon;
System.debug('The value is: ' + a.anonymous__c);
System.debug('The variable is: ' + Anon);
AccountUpdateList.add(a);
}
// for(Account a: AcctnoMaillist)
// {
// a.no_mail__c = AcctNoMail.get(a);
// AccountUpdateList.add(a);
// }
// for(Account a: AcctnoAppeallist)
// {
// a.no_appeal__c = AcctNoAppeal.get(a);
// AccountUpdateList.add(a);
// }
if(AccountUpdateList.size() > 0)
{
update AccountUpdateList;
}
}//whole routine
rigger update_account_after on Contact (after insert, after update, after delete) {
map<Id, date> AcctNoAppeal = new map<Id, date>();
map<Id, boolean> AcctAnonymous = new map<Id,boolean>();
map<Id, boolean> AcctNoMail = new map<Id, boolean>();
boolean anon = null;
boolean nomail = null;
date noappeal = null;
//Contact cforlist = ??? I need the id of the record the trigger is acting on. I am trying to use it to make a list of the all the contacts //related to its parent account
//List<Contact> contactlist = new List<contact>();
//IF (trigger.isupdate || trigger.isdelete){
//contactlist = [Select ID, anonymous__c, no_appeal__c, no_mail__c FROM contact where accountid = :cforlist.accountid];
//}
for(contact c: trigger.isDelete ? trigger.Old : trigger.new){
//Account updateacc = [Select ID , anonymous__c, no_mail__c, no_appeal__c from Account where id = :c.accountid];
If (c.anonymous__c == true || c.no_mail__c != null || c.no_appeal__c != null) {
//if the contact is being inserted then update account according the contacts values
If (trigger.isinsert){
if (c.anonymous__c == true) AcctAnonymous.put(c.accountid, c.anonymous__c);
if(c.no_mail__c != null) {nomail = true;
AcctNoMail.put(c.accountid, nomail);
}
AcctNoAppeal.put(c.accountid, c.no_appeal__c);
}//insert
//the contact is being updated or deleted. iterate the contact list and set variables
//update according to the variables
If (trigger.isupdate || trigger.isdelete) {
list<contact> contactlist = [Select ID, anonymous__c, no_appeal__c, no_mail__c FROM contact where accountid = :c.accountid];
for(contact con :contactlist) {
if(con.anonymous__c == true) anon = true;
if(con.no_mail__c != null) nomail = true;
If (noappeal != null) {
if (con.no_appeal__c >= noappeal) noappeal = con.no_appeal__c;
}
else if (con.no_appeal__c != null) noappeal = con.no_appeal__c;
}
IF (anon == true) AcctAnonymous.put(c.accountid,anon);
else {anon = false;
AcctAnonymous.put(c.accountid,anon);
}
IF (nomail == true) AcctNoMail.put(c.accountid, nomail);
else {nomail = false;
AcctNoMail.put(c.accountid, nomail);
}
IF (noappeal != null) AcctNoAppeal.put(c.accountid, c.no_appeal__c);
else {noappeal = null;
AcctNoAppeal.put(c.accountid, c.no_appeal__c);
}
}//update and delete
}//if statement
}//for loop
List<Account> AcctAnon = [SELECT Id, anonymous__c FROM Account WHERE Id IN :AcctAnonymous.KeySet()];
List<Account> AcctnoMaillist = [SELECT Id, no_mail__c FROM Account WHERE Id IN :AcctNoMail.KeySet()];
List<Account> AcctnoAppeallist = [SELECT Id, no_appeal__c FROM Account WHERE Id IN :AcctNoAppeal.KeySet()];
List<Account> AccountUpdateList = new List<Account>();
for(Account a: AcctAnon)
{
a.anonymous__c = AcctAnonymous.get(a.id);
a.anonymous__c = Anon;
System.debug('The value is: ' + a.anonymous__c);
System.debug('The variable is: ' + Anon);
AccountUpdateList.add(a);
}
// for(Account a: AcctnoMaillist)
// {
// a.no_mail__c = AcctNoMail.get(a);
// AccountUpdateList.add(a);
// }
// for(Account a: AcctnoAppeallist)
// {
// a.no_appeal__c = AcctNoAppeal.get(a);
// AccountUpdateList.add(a);
// }
if(AccountUpdateList.size() > 0)
{
update AccountUpdateList;
}
}//whole routine
- Get a set of Account Ids from the contact record(s).
- Query the accounts (and perhaps utilize a relationship query to get all the children contact records) based on the set of account ids.
- Loop through the accounts and perform your flag and date calculation logic.
I took a stab at a simplified trigger for you. The logic in it may not be 100% correct, but should point you in the right direction.Also, when you post code or Visualforce, please use the code format (< >) button. The button allows us to easily read your code and also copy and paste the code if necessary.
So if a new contact is created we can easily set the nomail and anonymous checkboxes on the account. And if there is a no appeal date for the new contact we just need to see if it is later or early than the date already set. easy. But if we are updating or deleting we need to look at all of the contacts related to the account of the contact being updated to figure out what to do.
This is all happening in the NSPS so we are really dealing with households as in household accounts.
So James, I don't need all the accounts related to a contact. There should not be more than 1. I need all of its siblings and only if I am updating or deleting. that said I think your code may still be doing what I want do I will test it.