1

I am trying to update contact record field through Student custom object that once in Student object active field is checked than the active field in contact for that record should also be checked. I have made a lookup of contact in Student and I have tried a trigger but it is not working.

trigger is like------

trigger updateRecordOnContact on Student__c (after insert)
{
    List<Student__c> updateRecord = Trigger.new;    
    updateRecord = [SELECT Id,Active__c FROM Student__c];
    for(Student__c updateAct:updateRecord)
    {
        if(updateAct.Active__c == true)
        {
            List<Contact> conn = [SELECT Id,Active__c FROM Contact WHERE Id=:updateAct.Id];
            for(Contact con:conn)
            {
                con.Active__c = true;            
            }
            Update conn;     
        }
    } 
}

please help for this......

4
  • Can you paste your actual code? Or is this it?
    – eyedar
    Sep 5, 2014 at 6:46
  • this is my code
    – Himanshu
    Sep 5, 2014 at 6:49
  • What exactly isn't working? Is the trigger not compiling? Is it not updating the Contact record?
    – eyedar
    Sep 5, 2014 at 6:56
  • it is not showing any error but it is not updating contact record
    – Himanshu
    Sep 5, 2014 at 7:03

2 Answers 2

1

Here:

List<Contact> conn = [SELECT Id,Active__c FROM Contact WHERE Id=:updateAct.Id];

you are querying for Contact objects that have the same ID as your Student__c object which will never return any results.

Presumably you have a lookup or master-detail field on either Contact or Student__c? You will have to relate the two objects using that field.

You should also "bulkify" your code meaning that you should not be doing a query inside the trigger loop. In a case like this you do not even need to query Contact; instead you can just create new instances and update them by setting the correct ID.

Assuming there is a lookup field called Contact__c to the Contact object on Student__c the trigger would end up looking like this:

trigger updateRecordOnContact on Student__c (after insert) {
    Map<Id, Contact> contacts = new Map<Id, Contact>();
    for (Student__c s : Trigger.new) {
        if (s.Active__c) {
            contacts.put(
                    s.Contact__c,
                    new Contact(Id = s.Contact__c, Active__c = true)
                    );
        }
    }
    update contacts.values();
}

Using a map ensures that if multiple Student__c records are inserted that relate to the same Contact only one update to Contact will be done. (Perhaps it is intended to be a 1:1 relationship, but better to code defensively.)

(The re-querying of Student__c is not necessary: the trigger is supplied instances of the object with all its fields queried automatically.)

Depending on your overall design, you may also need to add trigger logic for when Student__c is updated or deleted.

1
  • can u please help me with one more scenario of triggers for which I have posted the question.
    – Himanshu
    Sep 5, 2014 at 8:04
-1

You're setting Active__c to true in a local variable con whose scope is within that for loop. You need to create a separate list specifically for updating.

Here is an example of what I might do:

    trigger updateRecordOnContact on Student__c (after insert) { 
        list<Contact> clist = new list<Contact>();
        for(Student__c updateAct:[SELECT Id FROM Student__c where Active__c = true]) { 
                List conn = [SELECT Id,Active__c FROM Contact WHERE Id=:updateAct.Id];
                for(Contact con:conn) { 
                   Contact c = new Contact(id=con.id, Active__c=true);
                   clist.add(c);
                } 
          }
          if (!clist.isEmpty()) database.update(clist, false);
      }

[Incidentally, this is not how I would implement this because you're likely going to run into governor limits pretty quickly.]

2
  • You don't need to create a separate list just for updating. Although the con instance is scoped to the for loop, it's a reference to the conn list member. So the questioner's code is ok in this regard, because changing con changes conn[n] as they are the same instance.
    – Dominic
    Sep 5, 2014 at 7:39
  • can u please help me with one more scenario of triggers for which I have posted the question.
    – Himanshu
    Sep 5, 2014 at 8:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .