• Nihar Annamaneni 7
  • NEWBIE
  • 60 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 2
    Replies
 Users report multiple intermittent errors and unexpected results when saving a record. Upon investigation, the developer finds that trigger are executing more than one. What is a potential solution to solve this situation?
a)    Use a one trigger per object approach
b)    Use a static variable to check if the trigger has already run.
c)    Disable all triggers and use workflow rule…..
d)    Use private
Hi all,
I have a batch class for two users to send mails regarding there opportunitys closed date is tommorow
but my batch class i sending to only one user 
how can i send to another user mail
can anyone help me

my code :
global class sendemail implements Database.Batchable < sobject > {
global Database.QueryLocator start(Database.BatchableContext bc) {
       String Query;
       Date dt = date.today().addDays(1);
       Query = 'SELECT Name,Id From Opportunity WHERE CloseDate =: dt ';
       return Database.getquerylocator(Query);
       }

global void execute(Database.BatchableContext bc, List < Opportunity > opplist) {
       List < Messaging.SingleEmailMessage > emails = new List < Messaging.SingleEmailMessage > ();
       for (Opportunity opp: opplist) {
       // opp.CloseDate = 'createddate+1'; 
       Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
       email.setToAddresses(new String[] {'nihar.annamaneni@gmail.com'});
       email.setSubject('opportunity closed date'); 
       String body='';
       body += '<html></br> </br>' + 'Dear user, Your opportunity ' + '<a href="'+URL.getSalesforceBaseUrl().toExternalForm() + '/' + opp.name + '">' + opp.name + '</a>' + ' closed date is tommorow';
       body +=  '</body></html>';
       email.setHtmlBody(body);
       emails.add(email);
       }
       Messaging.sendEmail(emails);
       // update opplist;
       }

global void finish(database.BatchableContext bc) {}
}

thanks inadvance..........
HI,
I have created an email template (Dear student , Your registration is deleted successfully. Thanks Capital Info) 
How to assign this email template to the trigger
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". 

My code :
trigger ContactBeforeInsert1 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){
              
          }
        }
    }
}
 
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; 
        }
            
    }
    }
}
}
global class CsvFileExporter implements System.Schedulable {
   global void execute(SchedulableContext sc) {
List<Account> accList = [Select Id,Name, AccountNumber, Industry, CreatedDate,LastModifiedDate,Phone from Account where CreatedDate=TODAY];
       List<Account> latestModiaccList = [Select Id,Name, AccountNumber, Industry, CreatedDate,LastModifiedDate,Phone from Account where LastModifiedDate=TODAY AND CreatedDate !=TODAY];

       String header = '\bAccounts inserted Today\b\r\n'+ '\bId, Name, Account Number, Industry,Phone\b\r\n';
       String header1 ='\r\n\bAccounts Modified Today\b\r\n'+ '\bId, Name, Account Number, Industry,Phone\b\r\n';
String generatedCSVFile = header+'';
List<String> queryFields = new List<String>{'Id','Name','AccountNumber','Industry','Phone'};
       String fileRow = '';
for(Account a: accList){            
fileRow = '';
fileRow = fileRow +','+ a.Id;
fileRow = fileRow +','+ a.Name;
       fileRow = fileRow +','+ a.AccountNumber;
fileRow = fileRow +','+ a.Industry;
       fileRow = fileRow +','+ a.Phone;
fileRow = fileRow.replaceFirst(',','');
generatedCSVFile = generatedCSVFile + fileRow + '\n';
}
       generatedCSVFile = generatedCSVFile+header1+'';
       for(Account ac: latestModiaccList){
fileRow = fileRow +','+ ac.Id;
fileRow = fileRow +','+ ac.Name;
        fileRow = fileRow +','+ ac.AccountNumber;
fileRow = fileRow +','+ ac.Industry;
        fileRow = fileRow +','+ ac.Phone;
           fileRow = fileRow.replaceFirst(',','');
           generatedCSVFile = generatedCSVFile + fileRow + '\n';
       }
       system.debug('File:'+generatedCSVFile);
Messaging.EmailFileAttachment csvAttachment = new Messaging.EmailFileAttachment();
Blob csvBlob = blob.valueOf(generatedCSVFile);
String csvName = 'Accounts data inserted and modified Today.csv';
csvAttachment.setFileName(csvName);
csvAttachment.setBody(csvBlob);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{'ranga.vangalapudi@gmail.com'};
String subject = 'Accounts data inserted and modified Today';
email.setSubject(subject);
email.setToAddresses(toAddresses);
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; 
        }
            
    }
    }
}
}