function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
wpatterswpatters 

Field update trigger

Hello everybody, I'm pretty new with writing apex, and I'm trying to write a trigger which causes a checkbox to be checked or unchecked in my contacts object when a field is updated in another object (Legislature).  I'm not sure what is going wrong with the code I've written, but I've got the sneaking suspicion that it is entierly too simple.  I am trying to update all of the contacts from the state associated with the Legislature object that got updated.  Also, the Presently_in_Session__c field is a formula field that returns "TRUE" or "FALSE" depending on the current date, and is the field which will determine the state of the Legislature_in_Session__c checkbox on the contact object.  Anyway, here it is:

Code:
trigger Session_Check on Legislature__c (before update) {
List<Contact> updatedContacts = new List<Contact>();

for(Contact c : [SELECT id, Legislature_in_Session__c FROM contact WHERE Calculated_State_From_KeyPerson_Home_Bus__c = '{!Legislatures__c.State__c}']){
if('{!Legislature__c.Presently_In_Session__c}'=='TRUE'){
c.Legislature_in_Session__c = true;
}else{
c.Legislature_in_Session__c = false;
}
updatedContacts.add(c);
}
update updatedContacts;
}

I am not getting any errors, but it is not doing anything.  Any help would be greatly appreciated.



Message Edited by wpatters on 09-12-2008 01:15 PM

Message Edited by wpatters on 09-12-2008 01:34 PM
TehNrdTehNrd
This isn't exactly right but it should help you get on your way.


Code:
trigger Session_Check on Legislature__c (before update) {
 List<Contact> updatedContacts = new List<Contact>();
 Set<Id> legIds = new Set<Id>();
 
 for(Legistlature__c l : trigger.new){
  if(l.Presently_In_Session__c == true){
   legIds.add(l.Id):
  }
 }
 
 for(Contact c : [SELECT id, Legislature_in_Session__c FROM contact WHERE Legistature__c IN :legIds]){
  c.FieldToUpdate = 'whatever'
  updatedContacts.add(c);
 }
 update updatedContacts;
}

 

wpatterswpatters
This looks good, but i need the sql statement at the end to pull all the contacts where the state matches the legislature state field.  I think my main problem is referencing the state field in legislature... is the syntax just '{!object.field} like in s-controls?  Also, there is no reference field on contacts that connects it to the legislature object... they're only relationship is the state fields


Message Edited by wpatters on 09-12-2008 03:07 PM
TehNrdTehNrd
Syntax in apex is definitely not the same as s-controls. How about this:

Code:
trigger Session_Check on Legislature__c (before update) {
 List<Contact> updatedContacts = new List<Contact>();
 Set<Id> legIds = new Set<Id>();
 Set<String> legStates = new Set<String>();
 
 for(Legistlature__c l : trigger.new){
  if(l.Presently_In_Session__c == true){
   legIds.add(l.Id):
   legStates.add(l.State__c);
  }
 }
 
 for(Contact c : [SELECT id, Legislature_in_Session__c FROM contact WHERE Legistature__c IN :legIds AND State IN :legStates]){
  c.FieldToUpdate = 'whatever'
  updatedContacts.add(c);
 }
 update updatedContacts;
}

 

TehNrdTehNrd
Keep in mind due to the limits of apex you will only be able to update 100 contacts per legislature.
wpatterswpatters
hmm.. can this be done with a workflow rule? I'm thinking i might just have to make a relationship field and do this with a new formula field...
TehNrdTehNrd
Ya, a cross object formula field may be better in this instance.
wpatterswpatters
is there any way besides exporting the data and doing an excel vlookup to get all the relationship fields filled in automatically by using the state?
TehNrdTehNrd
I am not aware of any better way.
wpatterswpatters
ok, well thanks for your help man
ksnyderksnyder
Thanks so much. I have been working with databases for years, but new to Salesforce. This discussion and code snippet really helped!