+ Start a Discussion
KevinSFKevinSF 

With a trigger, set Contact checkbox to TRUE when a related child record is created or updated.

My goal: Update Contact.Major_Gift_Given_Contact checkbox to TRUE when a child "Advancement Given" record is created or updated.

 

This is what I want to do, except for the error:

"Error: Compile Error: Expression cannot be assigned at line -1 column -1"

 

trigger UpdateMajorGiftGivenContactCheckbox on Advancement_Giving__c (after insert, after update) {

Contact.Major_Gift_Given_Contact__C = TRUE;

}

 

Brand new to APEX, thanks for any help.

 

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Kevin ,

 

Change the code as follows :-

 

from

 

contacts.put(record.contact__c, new contact(Major_Gift_Given_Contact__C = TRUE));

 

to

 

contacts.put(record.contact__c, new contact(id=record.contact__c,Major_Gift_Given_Contact__C = TRUE));

All Answers

sfdcfoxsfdcfox

Your trigger is trying to access "Schema.Contact.Major_Gift_Given_Contact__c", which is a static field reference (a static variable that represents a field's presence in the database), which is not the same as actual data being inserted, which is located in "Trigger.new". You'll need to do something like this:

 

trigger updatemajorgiftgivencontactcheckbox on advancement_giving__c( after insert, after update ) {
  map< id, contact > contacts = new map< id, contact >();
  for(advancement_giving__c record:trigger.new)
    contacts.put(record.contact__c, new contact(id=record.contact__c));
  update contacts.values();
}

"Contact__c" should be replaced by the field that is the relationship field to the contact.

Vinit_KumarVinit_Kumar

Hi Kevin,

 

Try below code :-

 

trigger UpdateMajorGiftGivenContactCheckbox on Advancement_Giving__c (after insert, after update) {

List<Contact> conList = new List<Contact>();
List<Contact> conListupdated = new List<Contact>();
List<id> conIdList = new List<ID>();
for(Advancement_Giving__c ac :trigger.new){
conIdList.add(ac.Contact__c);
}

conList = [select Major_Gift_Given_Contact from Contact where id in:conIdList];

for(Contact c :conList){
c.Major_Gift_Given_Contact = true;
conListupdated.add(c);
}
update conListupdated;
}

KevinSFKevinSF

Thanks for the reply sfdcfox.  I modified the trigger a bit to see if I could set the checkbox field on the Contact record.

I wrote something closer to what I think I need to do.  It compiles but it fails when i try it in real use.

 

// Update Contact field: "Major_Gift_Given_Contact__C" to TRUE when a new Advancement_Giving__C record is inserted or updated.  Advancement_Giving__C is a related child object of Contact.

 

trigger UpdateMajorGiftGivenContactCheckbox on Advancement_Giving__c (after insert, after update) {

 

// Will store related Contact record ID ?

map< id, contact > contacts = new map< id, contact >();

 

// Create trigger for new or selected Advancement_giving__c record ?

for(advancement_giving__c record:trigger.new)        

 

// Update checkbox field on the parent Contact record to TRUE.  Looks like this code will "put" data on the parent contact record.  Not sure how to write this line.  The code compiles but the trigger fails when I actually try it out.    

 

contacts.put(record.contact__c, new contact(Major_Gift_Given_Contact__C = TRUE));

 

update contacts.values();  

 

}

 

This is the error when I actually use the trigger in Salesforce:

Hopefully I'm pretty close to getting this going.

 

Thanks again for any further help!

 

Error: Invalid Data.
Apex trigger UpdateMajorGiftGivenContactCheckbox caused an unexpected exception, contact your administrator: UpdateMajorGiftGivenContactCheckbox: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.UpdateMajorGiftGivenContactCheckbox: line 19, column 1

Vinit_KumarVinit_Kumar

Kevin ,

 

Change the code as follows :-

 

from

 

contacts.put(record.contact__c, new contact(Major_Gift_Given_Contact__C = TRUE));

 

to

 

contacts.put(record.contact__c, new contact(id=record.contact__c,Major_Gift_Given_Contact__C = TRUE));

This was selected as the best answer
KevinSFKevinSF

Thanks Vinit_Kumar and sfdcfox, really appreciate it, my trigger is working now.  Here it is in it's complete form for reference:

 

// Update Contact field: "Major_Gift_Given_Contact__C" to TRUE when a new Advancement_Giving__C record is inserted or updated. 

// Advancement_Giving__C is a related child object of Contact.

trigger UpdateMajorGiftGivenContactCheckbox on Advancement_Giving__c (after insert, after update) {

 

// Will store related Contact record ID

map< id, contact > contacts = new map< id, contact >();

 

// Create trigger for new or selected Advancement_giving__c record

for(advancement_giving__c record:trigger.new)        

 

     // Update checkbox field on the parent Contact record to TRUE    

     contacts.put(record.contact__c, new contact(id=record.contact__c, Major_Gift_Given_Contact__C = TRUE));      

 

update contacts.values();  

 

}

 

Chandrika NagapuriChandrika Nagapuri
how to update contacts related to account checkbox?