+ Start a Discussion
deepak kumar 13deepak kumar 13 

Email sending Trigger

hi,
i have two custom object called functional requirement and system requirement..system requirement object having lookup relationship with functional requirement..When a new SR is created,i need to send email to user which is a user lookup field(Technical_Lead__c) in FR...please help me to query to retrive that user lookup email from FR when SR created...i have created this trigger but when i create record in application it showing error as
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger SREmailSending caused an unexpected exception, contact your administrator: SREmailSending: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.SREmailSending: line 7, column 1
 
trigger SREmailSending on System_Requirements__c (after insert,after update) {
    List<Messaging.SingleEmailMessage>mails =new List<Messaging.SingleEmailMessage>();
    
  for(System_Requirements__c sr:trigger.new){
    if(sr.Status__c =='Development Completed'){  
         String emailAddr=[Select Email from User where id =:trigger.new[0].Functional_Requirement__r.Technical_Lead__c].Email; 
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         String[] toAddresses = new String[] {emailAddr};
         mail.setToAddresses(toaddresses);
         mail.setSubject('SR Created');
         mail.setPlainTextBody('Status of SR is  Development Completed');
         mails.add(mail);
      }
     
       if(mails.size()>0){
         Messaging.sendEmail(mails);
      }
}

Thanks in advance
Best Answer chosen by deepak kumar 13
HaiderRaza77HaiderRaza77
trigger SREmailSending on System_Requirement__c (after insert,after update) {
    List<Messaging.SingleEmailMessage>mails =new List<Messaging.SingleEmailMessage>();
    Map<String, String> SR_FR_Map = new Map<String, String>();
    Map<String, String> FR_Email_Map = new Map<String, String>();
    
    for(System_Requirement__c sr: trigger.newMap.values())
        SR_FR_Map.put(sr.Id, sr.Functional_Requirement__c);
        
    for(Functional_Requirement__c FR : [SELECT Id, Technical_Lead__r.Email FROM Functional_Requirement__c WHERE ID IN : SR_FR_Map.values()])
        FR_Email_Map.put(FR.Id, FR.Technical_Lead__r.Email);
    
  	for(System_Requirement__c sr:trigger.new){
    	if(sr.Status__c =='Development Completed'){ 
         	//String emailAddr=sr.TechnicalUserEmail__c; 
         	String emailAddr=FR_Email_Map.get(sr.Functional_Requirement__c);
         	Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         	String[] toAddresses = new String[] {emailAddr};
         	mail.setToAddresses(toaddresses);
         	mail.setSubject('SR Created');
         	mail.setPlainTextBody('Status of SR is  Development Completed');
         	mails.add(mail);
      	}
     
       	if(mails.size()>0){
         	Messaging.sendEmail(mails);
      	}
	}
}
try the above code

All Answers

SonamSonam (Salesforce Developers) 
  On line :
String emailAddr=[Select Email from User where id =:trigger.new[0].Functional_Requirement__r.Technical_Lead__c].Email; 

It seems the SOQL is not returning any records to pass to emailAddr.
Could you check if trigger.new[0].Functional_Requirement__r.Technical_Lead__c].Email
is gettting any records.

 
HaiderRaza77HaiderRaza77

This is failing because of the referenced data. You will have to query the Functional Requirement using
trigger.new[0].Functional_Requirement__c

More efficeintly, you can create a forumla field on the System_Requirement object to reference the Technical Lead as below
Functional_Requirement__r.Technical_Lead__r.Email
and then use 
trigger.new[0].TechnicalUserEmail__c

in your trigger.
 
trigger SREmailSending on System_Requirement__c (after insert,after update) {
    List<Messaging.SingleEmailMessage>mails =new List<Messaging.SingleEmailMessage>();
        
  for(System_Requirement__c sr:trigger.new){
    if(sr.Status__c =='Development Completed'){  
         String emailAddr=trigger.new[0].TechnicalUserEmail__c; 
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         String[] toAddresses = new String[] {emailAddr};
         mail.setToAddresses(toaddresses);
         mail.setSubject('SR Created');
         mail.setPlainTextBody('Status of SR is  Development Completed');
         mails.add(mail);
      }
     
       if(mails.size()>0){
         Messaging.sendEmail(mails);
      }
    }
}




Please mark as solved if this solution helps
deepak kumar 13deepak kumar 13
Thanks for your reply sonam..i tried that way also but not getting any values in query..Can you tell me in any otherway to do that to get Email address from Functional requirement.....
deepak kumar 13deepak kumar 13
hi haider raza, thanks for your reply i also tried by using formula fields but its sending email for 3 times when i create record in SR..is there any other way to query without using formula fields..
HaiderRaza77HaiderRaza77
trigger SREmailSending on System_Requirement__c (after insert,after update) {
    List<Messaging.SingleEmailMessage>mails =new List<Messaging.SingleEmailMessage>();
    
    system.debug('**DEBUG: '+trigger.new[0].TechnicalUserEmail__c);
    
  for(System_Requirement__c sr:trigger.new){
    if(sr.Status__c =='Development Completed'){  
         String emailAddr=sr.TechnicalUserEmail__c; 
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         String[] toAddresses = new String[] {emailAddr};
         mail.setToAddresses(toaddresses);
         mail.setSubject('SR Created');
         mail.setPlainTextBody('Status of SR is  Development Completed');
         mails.add(mail);
      }
     
       if(mails.size()>0){
         Messaging.sendEmail(mails);
      }
    }
}
above code is working fine for me and is sending only one email per SR.
deepak kumar 13deepak kumar 13
Hi Haider raza, i want to send email without using any extra fields like formula fields..is there any way we can achieve through using target object id..if possible can you guide me...

thanks in advance..
 
HaiderRaza77HaiderRaza77
trigger SREmailSending on System_Requirement__c (after insert,after update) {
    List<Messaging.SingleEmailMessage>mails =new List<Messaging.SingleEmailMessage>();
    Map<String, String> SR_FR_Map = new Map<String, String>();
    Map<String, String> FR_Email_Map = new Map<String, String>();
    
    for(System_Requirement__c sr: trigger.newMap.values())
        SR_FR_Map.put(sr.Id, sr.Functional_Requirement__c);
        
    for(Functional_Requirement__c FR : [SELECT Id, Technical_Lead__r.Email FROM Functional_Requirement__c WHERE ID IN : SR_FR_Map.values()])
        FR_Email_Map.put(FR.Id, FR.Technical_Lead__r.Email);
    
  	for(System_Requirement__c sr:trigger.new){
    	if(sr.Status__c =='Development Completed'){ 
         	//String emailAddr=sr.TechnicalUserEmail__c; 
         	String emailAddr=FR_Email_Map.get(sr.Functional_Requirement__c);
         	Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         	String[] toAddresses = new String[] {emailAddr};
         	mail.setToAddresses(toaddresses);
         	mail.setSubject('SR Created');
         	mail.setPlainTextBody('Status of SR is  Development Completed');
         	mails.add(mail);
      	}
     
       	if(mails.size()>0){
         	Messaging.sendEmail(mails);
      	}
	}
}
try the above code
This was selected as the best answer
deepak kumar 13deepak kumar 13
i have another requirement in same trigger..i have one more object System Test Case which is lookup with System requirement..i have to send mail to techical lead which is user in functional requirement when new record created in system test case with status 'Test pass' and if the status is 'Test Fail ' i have to send mail which is user name developer in system requirement...


Last trigger i solved like below trigger..how can i solve the new one..

trigger SREmailSending on System_Requirements__c (after insert,after update) {
     List<Messaging.SingleEmailMessage>mails =new List<Messaging.SingleEmailMessage>();
     map<id,User>usermap=new map<id,User>([Select id,Email from User limit 1000]);
     system.debug('%%%%%%'+usermap);
     
     set<id>frids=new set<id>();
    for(System_Requirements__c sr:trigger.new){
     if(sr.Functional_Requirement__c != null){
         frids.add(sr.Functional_Requirement__c );
         }
       }
     system.debug('@@@@@@@'+frids);
  map<id,System_Requirements__c >srmap=new map<id,System_Requirements__c >([Select id,Developer__c from System_Requirements__c ]) ;
  map<id,Functional_Requirement__c>Frmap=new map<id,Functional_Requirement__c>([Select id,Technical_Lead__c,Test_Lead__c,Business_Analyst__c from Functional_Requirement__c where id in:frids]);
    system.debug('*******'+Frmap);
      
    for(System_Requirements__c s:trigger.new){
        if(s.Status__c =='Development Completed'){             
            Functional_Requirement__c fr =Frmap.get(s.Functional_Requirement__c);
            system.debug('///////////'+Fr);
                User u=usermap.get(fr.Technical_Lead__c);
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setTargetObjectid(u.id);
                mail.setSaveAsActivity(false);
                mail.setSubject('SR Created');
                mail.setPlainTextBody('Status of SR is  Development Completed');
                mails.add(mail);          
        }
   if(mails.size()>0){
         Messaging.sendEmail(mails);
      }
 }
}