You need to sign in to do that
Don't have an account?
deepak 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
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
All Answers
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.
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.
Please mark as solved if this solution helps
thanks in advance..
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);
}
}
}