1

i have two object one is standard object(Contact) another one is custom object(Conference). in that custom object i put one field(Attendance).

my question is:

suppose i created one contact my attendance field value automatically increase. for ex: already attendance value 3 means it will automatically change.

How to write trigger? pls help me

3
  • So As I understand you have lookup from contact to Conference? Also "file" like attachment or it is typo and you mean field? Commented Feb 8, 2014 at 9:29
  • 1
    please, try to rephrase the question. It is not clear, and what is "changa" ? Commented Feb 8, 2014 at 9:31
  • Is there any relation between "Contact" and "Conference" ?
    – user40138
    Commented Feb 8, 2014 at 14:46

1 Answer 1

1

If I understand you well: Contact is child here and you want just update parent object by number of child?

In that case you should create trigger(after insert, before delete) on child:

contact[] contacts;
Set<Id> parentIds = new Set<id>();
Conference__c[] contToUpdate = new Conference__c[] {};

if(trigger.old == null){
    contacts = trigger.new;
}
else{
    contacts = trigger.old;
}
for(Conact con : contacts){
    if(con.Conference__c != null)   
        parentIds.add(con.Conference__c)
    }
}

if(parentIds.isEmpty()){
    return;
}

for(Conference__c confs = [SELECT Id, Attendance__c,
                                (SELECT Id 
                                 FROM Contacts__r) 
                         FROM Conference__c
                         WHERE Id IN : parentIds]){

     if(confs.Attendance__c != confs.Contacts__r.size()){
          confs.Attendance__c = confs.Contacts__r.size()
          contToUpdate.add(confs);
     }
}
if(!contToUpdate.isEmpty()){
     update contToUpdate;
}

You must log in to answer this question.

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