+ Start a Discussion
Nihar Annamaneni 7Nihar Annamaneni 7 

populate contact description with modified user name when user update contact and when user creates contact and when user delete contact

when i am updating the trigger the contact description is not changing and how to write code for delete trigger for contacts in the same code.....

trigger contactbeforeinsert on Contact (before insert,before update) {
    for(contact c:trigger.new){
         if (c.Description == null)
        {
            c.Description = '(before Contact insert trigger wrote this)';
        }
        else
        {
            c.Description = c.Description + '(before Contact insert trigger wrote this)';
        } 
        for(contact con:trigger.old){
        if (trigger.isBefore) {
        if(trigger.isUpdate)
        {
        contact con1 = [select id , lastname , Description from contact];
            if(con1.Description == null){
        con1.Description = 'New description';
       
        }
             update con; 
        }
            
    }
    }
}
}
Best Answer chosen by Nihar Annamaneni 7
Om PrakashOm Prakash
Hi Nihar,
Trigger.New is not availble for delete.
Please use bellow modified code.
trigger ContactBeforeInsert on Contact (before insert, before update, before delete) {
    if(!trigger.isDelete){
        for(Contact objContact : Trigger.new){
            if(trigger.isBefore){
                objContact.Description = 'Contact is created by ' + UserInfo.getUserName() + '. Description is added in before insert Trigger.';
             }
            if(trigger.isUpdate)
            {  
               objContact.Description = objContact.Description + 'Contact is updated by ' +UserInfo.getUserName() + '. Description is updated in before update Trigger.';
            }
        }
    }
    else{
        // trigger.isDelete
        List<Task> lstTask = new List<Task>();
        for(Contact objContact : Trigger.old){
         // Create a task to track that contact is deleted
              Task objTask = new Task();     
              objTask.Subject = 'Contact Deleted';  
              objTask.Priority = 'Normal';      
              objTask.Status = 'Completed';               
              objTask.Description = 'Contact ' + objContact.Id + ' is deleted by ' + UserInfo.getUserName() + '. Description on contact was ' + objContact.Description;
              lstTask.add(objTask);
        }    
        try{
              insert lstTask;
          }
          catch(Exception ex){
              
          }     
    }
}

 

All Answers

ManojjenaManojjena
HI Nihar ,

What exactly you want in above trigger ,Incase delete where you update user deatil as your contact record will delete on delete event .
Second thing please explain your requirment properly so that we can help you  .

Thanks 
Manoj
Om PrakashOm Prakash
Hi Nihar ,
If you want to update description on contact after insert and update of contcat then bellow sample code will work which is modified version of your code.
After delete you need to save deleted by on other  record. I have created a  new task for that case.  
 
trigger ContactBeforeInsert on Contact (before insert, before update, before delete) {
    for(Contact objContact : Trigger.new){
        if(trigger.isBefore){
            objContact.Description = 'Contact is created by ' + UserInfo.getUserName() + '. Description is added in before insert Trigger.';
         }
        if(trigger.isUpdate)
        {  
           objContact.Description = objContact.Description + 'Contact is updated by ' +UserInfo.getUserName() + '. Description is updated in before update Trigger.';
        }
       if(trigger.isDelete)
        {  
          // Create a task to track that contact is deleted
          Task objTask = new Task();       
          objTask.Subject = 'Contact Deleted';    
          objTask.Priority = 'Normal';        
          objTask.Status = 'Completed';                  
          objTask.Description = 'Contact ' + objContact.Id + ' is deleted by ' + UserInfo.getUserName() + '. Description on contact was ' + objContact.Description;
          try{
              insert objTask;
          }
          catch(Exception ex){
              
          }
        }
    }
}

 
Nihar Annamaneni 7Nihar Annamaneni 7
hi Om Prakash,
i am facing errors when deleting contact records...........

Error is :

Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger ContactBeforeInsert1 caused an unexpected exception, contact your administrator: ContactBeforeInsert1: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.ContactBeforeInsert1: line 2, column 1". 

Click here to return to the previous page.
Om PrakashOm Prakash
Hi Nihar,
Trigger.New is not availble for delete.
Please use bellow modified code.
trigger ContactBeforeInsert on Contact (before insert, before update, before delete) {
    if(!trigger.isDelete){
        for(Contact objContact : Trigger.new){
            if(trigger.isBefore){
                objContact.Description = 'Contact is created by ' + UserInfo.getUserName() + '. Description is added in before insert Trigger.';
             }
            if(trigger.isUpdate)
            {  
               objContact.Description = objContact.Description + 'Contact is updated by ' +UserInfo.getUserName() + '. Description is updated in before update Trigger.';
            }
        }
    }
    else{
        // trigger.isDelete
        List<Task> lstTask = new List<Task>();
        for(Contact objContact : Trigger.old){
         // Create a task to track that contact is deleted
              Task objTask = new Task();     
              objTask.Subject = 'Contact Deleted';  
              objTask.Priority = 'Normal';      
              objTask.Status = 'Completed';               
              objTask.Description = 'Contact ' + objContact.Id + ' is deleted by ' + UserInfo.getUserName() + '. Description on contact was ' + objContact.Description;
              lstTask.add(objTask);
        }    
        try{
              insert lstTask;
          }
          catch(Exception ex){
              
          }     
    }
}

 
This was selected as the best answer
Om PrakashOm Prakash
The same Trigger is optimized here and issue has been fixed.
Try bellow code and let me know if any query.
trigger ContactBeforeInsert on Contact (before insert, before update, before delete) {
    if(trigger.isInsert){
        for(Contact objContact : Trigger.new){
            objContact.Description = 'Contact is created by ' + UserInfo.getUserName() + '. Description is added in before insert Trigger.';
        }
    }
    else if(trigger.isUpdate){
        for(Contact objContact : Trigger.new){ 
             objContact.Description = objContact.Description + 'Contact is updated by ' +UserInfo.getUserName() + '. Description is updated in before update Trigger.';
        }
    }
    else if(trigger.isDelete){
        List<Task> lstTask = new List<Task>();
        for(Contact objContact : Trigger.old){
         // Create a task to track that contact is deleted
              Task objTask = new Task();     
              objTask.Subject = 'Contact Deleted';  
              objTask.Priority = 'Normal';      
              objTask.Status = 'Completed';               
              objTask.Description = 'Contact ' + objContact.Id + ' is deleted by ' + UserInfo.getUserName() + '. Description on contact was ' + objContact.Description;
              lstTask.add(objTask);
        }    
        try{
           insert lstTask;
        }
        catch(Exception ex){
              
        }     
    }
}

Nihar Annamaneni 7Nihar Annamaneni 7
Thank you Om Prakash,
My requirment is done thanks for your help with the code......................