+ Start a Discussion
santhosh konathala 17santhosh konathala 17 

Hi can anybody resolve this where I did wrong If I insert a new record get inserted into notification object.If I update a record ,a new record is created?Below is my code check it?

Trigger insertNotification on Account(after insert,After update) {

 List<Notification__c> member1 = new list<Notification__c>();
 for(Account member:Trigger.new)
{
       Notification__c memb = new Notification__c();
       memb.name=member.name;
       member1.add(memb);
       }
       
       if(member1.size()>0 &&member1.size()!=null){
       insert member1;
       }
       }
Dilip_VDilip_V
Santosh,

try this code.
Trigger insertNotification on Account(after insert) {

 List<Notification__c> member1 = new list<Notification__c>();
 for(Account member:Trigger.new)
{
       Notification__c memb = new Notification__c();
       memb.name=member.name;
       member1.add(memb);
       }
       
       if(member1.size()>0 &&member1.size()!=null){
       insert member1;
       }
       }

thanks.

 
Dilip_VDilip_V
Santosh,

You added after update in the trigger context that's y a new record is creating every time you edit account.

Thanks.
santhosh konathala 17santhosh konathala 17
Hi Thermo,

As you said I deleted After update event in Trigger but once existing record updated It was not updated in Notification object.So could you tell what is the solution for it .
Dilip_VDilip_V
Santosh,

Create a field called account(text)  on account.
And then try this code.
Trigger insertNotification on Account(after insert) {

 List<Notification__c> member1 = new list<Notification__c>();
if(Trigger.isinsert){
 for(Account member:Trigger.new)
      {
       Notification__c memb = new Notification__c();
       memb.name=member.name;
       Member.account__c=Memb.id;
       member1.add(memb);
       }
       
       if(member1.size()>0 &&member1.size()!=null){
       insert member1;
       }
}
if(Trigger.IsUpdate)
{
Map<id,id> membermap=new map<id,id>();
for(Notification__c Memb:[select id,name,Account__C from member])
MemberMap.put(memb.account__C,memb.id);
for(Account member:Trigger.new)
{
  if(MemberMap.containskey(Member.id))
   {
       Notification__c memb1 = new Notification__c();
       memb1.name=member.name;
       Member.account__c=Memb.id;
       memb1.id=membermap.get(Member.id);
       member1.add(memb1);
    }
}
 if(member1.size()>0 &&member1.size()!=null){
 Update member1; }
}     
}
}
Let me know if it helps.

If it helps make it as best answer.
Thanks.

 
santhosh konathala 17santhosh konathala 17
Hi Thermo ,

A great thanks to you code is working very well.

Could you give any suggestion for this scenario in above code:

Only at the time of changing Account name I want to update a record in Notification object.But problem is ,even If  I changed field values in an account a new record is being inserted with the same record name.
Dilip_VDilip_V
Santosh,

For that you have to check old account name and new account name name using oldmap and new map.
I have updated code accordingly.
Trigger insertNotification on Account(after insert,After Update) {

 List<Notification__c> member1 = new list<Notification__c>();
if(Trigger.isinsert&&Trigger.IsAfter){
 for(Account member:Trigger.new)
      {
       Notification__c memb = new Notification__c();
       memb.name=member.name;
       Member.account__c=Memb.id;
       member1.add(memb);
       }
       
       if(member1.size()>0 &&member1.size()!=null){
       insert member1;
       }
}
if(Trigger.IsUpdate&&trigger.isafter)
{
Map<id,id> membermap=new map<id,id>();
for(Notification__c Memb:[select id,name,Account__C from member])
MemberMap.put(memb.account__C,memb.id);
for(Account member:Trigger.new)
{
  if(MemberMap.containskey(Member.id)&&Trigger.oldmap.get(member.id).name!=Trigger.newmap.get(member.id).name)
   {
       Notification__c memb1 = new Notification__c();
       memb1.name=member.name;
       Member.account__c=Memb.id;
       memb1.id=membermap.get(Member.id);
       member1.add(memb1);
    }
}
 if(member1.size()>0 &&member1.size()!=null){
 Update member1; }
}     
}
}

Please let me know if it works.

Make it as best answer if it helps.


Thanks.