• Annie LaCourt
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
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
why does this work:
<apex:page standardController="Account" tabStyle="Account" sidebar="false">
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!Account.contacts}" var="c">
                <apex:column value="{!c.firstname}"/>
                <apex:column value="{!c.lastname}"/>
                <apex:column headerValue="Email">
                    <apex:inputField value="{!c.Email}"/>
                </apex:column>
            </apex:pageBlockTable>                       
        </apex:pageBlock>
    </apex:form>
</apex:page>

and this does not:


<apex:page standardController="Financial_Education_workshop__c" tabStyle="Financial_Education_workshop__c" sidebar="false">
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
              <apex:pageBlockTable value="{!Financial_Education_workshop__c.Workshop_Attendance__c}" var="w">
                <apex:column value="{!w.Attendee}"/>
                 <apex:column headerValue="Attendance">
                      <apex:inputField value="{!w.Attendance__c}"/>
                </apex:column>
             </apex:pageBlockTable>        
        </apex:pageBlock>
    </apex:form>
</apex:page>

Where Financial_Education_Workshop__c is a custom object and Workshop_Attendance__c is the name of a 
child relationship on another custom object. The error I am getting when I try to save the code is that
Workshop_Attendance is an invalid field on Sobject Financial_Education_workshop__c. I am trying figure out 
the correct synatx for referring to the relationship between the objects so that I can display a list of
the child records related to the parent object for editing in a grid. 

thx,
Annie
 
 
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
why does this work:
<apex:page standardController="Account" tabStyle="Account" sidebar="false">
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!Account.contacts}" var="c">
                <apex:column value="{!c.firstname}"/>
                <apex:column value="{!c.lastname}"/>
                <apex:column headerValue="Email">
                    <apex:inputField value="{!c.Email}"/>
                </apex:column>
            </apex:pageBlockTable>                       
        </apex:pageBlock>
    </apex:form>
</apex:page>

and this does not:


<apex:page standardController="Financial_Education_workshop__c" tabStyle="Financial_Education_workshop__c" sidebar="false">
    <apex:form >
        <apex:pageBlock >
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
              <apex:pageBlockTable value="{!Financial_Education_workshop__c.Workshop_Attendance__c}" var="w">
                <apex:column value="{!w.Attendee}"/>
                 <apex:column headerValue="Attendance">
                      <apex:inputField value="{!w.Attendance__c}"/>
                </apex:column>
             </apex:pageBlockTable>        
        </apex:pageBlock>
    </apex:form>
</apex:page>

Where Financial_Education_Workshop__c is a custom object and Workshop_Attendance__c is the name of a 
child relationship on another custom object. The error I am getting when I try to save the code is that
Workshop_Attendance is an invalid field on Sobject Financial_Education_workshop__c. I am trying figure out 
the correct synatx for referring to the relationship between the objects so that I can display a list of
the child records related to the parent object for editing in a grid. 

thx,
Annie