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
prasanth kumarprasanth kumar 

trigger not firing please help on event object

senario is that, if we create a new event on a account object then it should update the description field of account.  But it is not wokring. 
 
trigger event on Event (after insert) {
set<id> ids=trigger.newmap.keyset();

map<id,event> newevents =new map<id,event>();

 for(event e1:trigger.new)
 {
 if(((String)e1.whatid).startsWith('001')) {
 newevents.put(e1.id,e1);
  }
 }
// account acc=[select id,name,description from account where id in:newevents.keyset()];
 
 for(account a1:[select id,name,description from account where id in:newevents.keyset()])
 {

a1.description=newevents.get(a1.id).location;
update a1;
 
}
}

 
Best Answer chosen by prasanth kumar
AnjithKumarAnjithKumar
Prasanth,

While copying ids you are copying event ids not account ids, following is the updated code
trigger event on Event (after insert) {

map<id,event> newevents =new map<id,event>();
List<Account> acclist = new List<Account>();
 for(event e1:trigger.new)
 {
    if(((String)e1.whatid).startsWith('001')) {
     newevents.put(e1.whatId,e1);
    }
 }
// account acc=[select id,name,description from account where id in:newevents.keyset()];
 
 for(account a1:[select id,name,description from account where id in:newevents.keyset()])
 {

a1.description=newevents.get(a1.id).location;
acclist.add(a1);
 
}
update acclist;
}

Hope it helps you.

Thanks,
Anjith

All Answers

AnjithKumarAnjithKumar
Prasanth,

While copying ids you are copying event ids not account ids, following is the updated code
trigger event on Event (after insert) {

map<id,event> newevents =new map<id,event>();
List<Account> acclist = new List<Account>();
 for(event e1:trigger.new)
 {
    if(((String)e1.whatid).startsWith('001')) {
     newevents.put(e1.whatId,e1);
    }
 }
// account acc=[select id,name,description from account where id in:newevents.keyset()];
 
 for(account a1:[select id,name,description from account where id in:newevents.keyset()])
 {

a1.description=newevents.get(a1.id).location;
acclist.add(a1);
 
}
update acclist;
}

Hope it helps you.

Thanks,
Anjith
This was selected as the best answer
Abhishek_DEOAbhishek_DEO
Anjith, you beat me to it :)
prasanth kumarprasanth kumar
love u
AnjithKumarAnjithKumar
Prasanth, Can you mark it as best answer.

thanks,
prasanth kumarprasanth kumar
sute,  And can i hve test class for this...  and more over i modified the code that should work for spectific user name in if conition.    Can i have the test class for the below code. 
 
trigger event on Event (after insert) {
set<id> ids=trigger.newmap.keyset();

map<id,event> newevents =new map<id,event>();

 for(event e1:trigger.new)
 {
 if(((String)e1.whatid).startsWith('001') && e1.CreatedBy.Username=='prasanth523001@gmail.com') {
 newevents.put(e1.whatId,e1);
  }
 }
// account acc=[select id,name,description from account where id in:newevents.keyset()];
 
 for(account a1:[select id,name,description from account where id in:newevents.keyset()])
 {

a1.description=newevents.get(a1.id).subject+' ,'+newevents.get(a1.id).EndDateTime;
update a1;
 
}
}

 
Waqar Hussain SFWaqar Hussain SF
The following code works fine for me...
try this code
 
trigger changedescription on Event (after insert) {

    map<id,event> newevents =new map<id,event>();
    List<Account> acclist = new List<Account>();
     for(event e1:trigger.new)
     {
        if(String.valueOf(e1.whatid).startsWith('001')) {
         newevents.put(e1.whatId,e1);
        }
     }
     
     for(account a1:[select id,name,description from account where id in:newevents.keyset()])     {
        a1.description = newevents.get(a1.id).location;
        acclist.add(a1);
    }
    if(acclist.size()>0){
        update acclist;
    }
}