function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Himanshu GhateHimanshu Ghate 

Can someone please help me out,with the solution in my code.Error-->System.QueryException: List has no rows for assignment to SObject

System.QueryException: List has no rows for assignment to SObject
public class sendEmail {
public List<String> toList{get;set;}
public List<String> ccList{get;set;}
public String To{get;set;}
public String cc{get;set;}
public String Subject{get;set;}
public String EmailBody{get;set;}
public CustomOpportunity__c Customopp{get;set;}
public Id  oppid{get;set;}

public sendEmail()
{
  ApexPages.PageReference Pageref=ApexPages.CurrentPage();
  oppid=Pageref.getParameters().get('Id');
  Customopp=[Select id, Name , AccountName__c,AccountName__r.Name,ContactName__r.Name,AccountName__r.Emailid__c FROM CustomOpportunity__c WHERE Id=:oppid];
  Subject='Opportunity Details For Account'+Customopp.AccountName__r.Name;
  EmailBody='<p><b>Account Status Update</b></p>'+'</br></br>'+
  'Dear'+'</br></br>'+Customopp.ContactName__r.Name+'</br></br>'+
  '<p>We are happy to inform you that your Account Order is under process.Please Find Below Current Status of your Account Ordered Product by you:</p>'+
   '<b>Account Name:</b>'+' '+Customopp.AccountName__r.Name+'<br/>'+
   '<b>Contact Name:</b>'+' '+Customopp.ContactName__r.Name+'<br/>'+
   '<b>Email ID:</b>'+' '+Customopp.AccountName__r.Emailid__c +'<br/><br/>'+
   'Please Contact your Account Service Manager For any queries releated to this update.For any Concerns,You can write to us at himanshughate2999@gmail.com.<br/><br/>'+
   
   'Regards,</br>'+
 '<b>IBM Accounts Team.</b>';
}
public pagereference SendingEmail()
{
tolist=new List<String>();
tolist.add(To);
cclist=new List<String>();
tolist.add(cc);
List<Messaging.Email> EmailObjs=new List<Messaging.Email>();
Messaging.SingleEmailMessage Emailobj=new Messaging.SingleEmailMessage();
Emailobj.setToAddresses(tolist);
Emailobj.setToAddresses(cclist);
Emailobj.SetReplyTo('support@acme.com');
Emailobj.SetSenderDisplayName('Salesforce Support');
Emailobj.SetSubject(Subject);
Emailobj.Sethtmlbody(EmailBody);
//EmailObj.SetBccSender(false);
//EmailObj.SetUseSignature(false);
EmailObjs.add(Emailobj);
Messaging.sendEmail(EmailObjs);
pagereference pr = new pagereference('/');
return pr;    
}
}
Best Answer chosen by Himanshu Ghate
SwethaSwetha (Salesforce Developers) 
HI Himanshu,
Try this updated code
public sendEmail()
{
  ApexPages.PageReference Pageref=ApexPages.CurrentPage();
  oppid=Pageref.getParameters().get('Id');
  List<CustomOpportunity__c> customOpps = [Select id, Name , AccountName__c,AccountName__r.Name,ContactName__r.Name,AccountName__r.Emailid__c FROM CustomOpportunity__c WHERE Id=:oppid];
  if (!customOpps.isEmpty()) {
    Customopp = customOpps[0];
    Subject='Opportunity Details For Account'+Customopp.AccountName__r.Name;
    EmailBody='<p><b>Account Status Update</b></p>'+'</br></br>'+
      'Dear'+'</br></br>'+Customopp.ContactName__r.Name+'</br></br>'+
      '<p>We are happy to inform you that your Account Order is under process.Please Find Below Current Status of your Account Ordered Product by you:</p>'+
       '<b>Account Name:</b>'+' '+Customopp.AccountName__r.Name+'<br/>'+
       '<b>Contact Name:</b>'+' '+Customopp.ContactName__r.Name+'<br/>'+
       '<b>Email ID:</b>'+' '+Customopp.AccountName__r.Emailid__c +'<br/><br/>'+
       'Please Contact your Account Service Manager For any queries releated to this update.For any Concerns,You can write to us at himanshughate2999@gmail.com.<br/><br/>'+
       'Regards,</br>'+
     '<b>IBM Accounts Team.</b>';
  }
}

Basically, the error is occurring because the SOQL query is not returning any records for the specified oppid. You can handle this situation by checking if the query returned any records using the List.isEmpty() method


If this information helps, please mark the answer as best. Thank you

All Answers

SwethaSwetha (Salesforce Developers) 
HI Himanshu,
Try this updated code
public sendEmail()
{
  ApexPages.PageReference Pageref=ApexPages.CurrentPage();
  oppid=Pageref.getParameters().get('Id');
  List<CustomOpportunity__c> customOpps = [Select id, Name , AccountName__c,AccountName__r.Name,ContactName__r.Name,AccountName__r.Emailid__c FROM CustomOpportunity__c WHERE Id=:oppid];
  if (!customOpps.isEmpty()) {
    Customopp = customOpps[0];
    Subject='Opportunity Details For Account'+Customopp.AccountName__r.Name;
    EmailBody='<p><b>Account Status Update</b></p>'+'</br></br>'+
      'Dear'+'</br></br>'+Customopp.ContactName__r.Name+'</br></br>'+
      '<p>We are happy to inform you that your Account Order is under process.Please Find Below Current Status of your Account Ordered Product by you:</p>'+
       '<b>Account Name:</b>'+' '+Customopp.AccountName__r.Name+'<br/>'+
       '<b>Contact Name:</b>'+' '+Customopp.ContactName__r.Name+'<br/>'+
       '<b>Email ID:</b>'+' '+Customopp.AccountName__r.Emailid__c +'<br/><br/>'+
       'Please Contact your Account Service Manager For any queries releated to this update.For any Concerns,You can write to us at himanshughate2999@gmail.com.<br/><br/>'+
       'Regards,</br>'+
     '<b>IBM Accounts Team.</b>';
  }
}

Basically, the error is occurring because the SOQL query is not returning any records for the specified oppid. You can handle this situation by checking if the query returned any records using the List.isEmpty() method


If this information helps, please mark the answer as best. Thank you
This was selected as the best answer
Himanshu GhateHimanshu Ghate
I have used your code,But it showing: System.NullPointerException: Attempt to de-reference a null object public class sendEmail { public List toList{get;set;} public List ccList{get;set;} public String To{get;set;} public String cc{get;set;} public String Subject{get;set;} public String EmailBody{get;set;} public CustomOpportunity__c Customopp{get;set;} public List Customopps{get;set;} public Id oppid{get;set;} public sendEmail() { if(!Customopps.isEmpty()) { Customopp=Customopps[0]; ApexPages.PageReference Pageref=ApexPages.CurrentPage(); oppid=Pageref.getParameters().get('Id'); List Customopps=[Select id, Name , AccountName__c,AccountName__r.Name,ContactName__r.Name,AccountName__r.Emailid__c FROM CustomOpportunity__c WHERE Id=:oppid]; Subject='Opportunity Details For Account'+Customopp.AccountName__r.Name; EmailBody='

Account Status Update

'+''+ 'Dear'+''+Customopp.ContactName__r.Name+''+ '

We are happy to inform you that your Account Order is under process.Please Find Below Current Status of your Account Ordered Product by you:

'+ 'Account Name:'+' '+Customopp.AccountName__r.Name+'
'+ 'Contact Name:'+' '+Customopp.ContactName__r.Name+'
'+ 'Email ID:'+' '+Customopp.AccountName__r.Emailid__c +'

'+ 'Please Contact your Account Service Manager For any queries releated to this update.For any Concerns,You can write to us at himanshughate2999@gmail.com.

'+ 'Regards,'+ 'IBM Accounts Team.'; } } public pagereference SendingEmail() { tolist=new List(); tolist.add(To); cclist=new List(); tolist.add(cc); List EmailObjs=new List(); Messaging.SingleEmailMessage Emailobj=new Messaging.SingleEmailMessage(); Emailobj.setToAddresses(tolist); Emailobj.setToAddresses(cclist); Emailobj.SetReplyTo('support@acme.com'); Emailobj.SetSenderDisplayName('Salesforce Support'); Emailobj.SetSubject(Subject); Emailobj.Sethtmlbody(EmailBody); //EmailObj.SetBccSender(false); //EmailObj.SetUseSignature(false); EmailObjs.add(Emailobj); Messaging.sendEmail(EmailObjs); pagereference pr = new pagereference('/'); return pr; } }
Himanshu GhateHimanshu Ghate
Error-->List has no rows for assignment to SObject. *please help me out.This code is of custom pdf.* *Visualforce Page--->*
Salesforce NExtJUngle
Account Name:
Country:
City:
Zip Code:
Phone:
Email:


Tracking Number: Date : {!Today()}

Bill To Ship To
Country:Country:
City:City:
Zip Code:Zip Code:
Phone:Phone:


NameProduct NameQuantityUnit PriceAmount
{!cl.name}{! cl.CustomProduct__r.name }{!cl.Quantity__c}{!cl.UnitPrice__c}{!cl.ActualPrice__c}




Subtotal {!subtotal}
Discount {!discount}
Tax {!tax}
Total {!total}
*ApexCode---->>* public class CustomPDF { public Id oppId{get;set;} public CustomOpportunity__c CustomOpp{get;set;} public List Customlineitem{get;set;} public decimal subtotal{get;set;} public decimal discount{get;set;} public decimal total{get;set;} public decimal tax{get;set;} public CustomPDF(){ ApexPages.Pagereference pf=ApexPages.CurrentPage(); oppId=pf.getParameters().get('Id'); CustomOpp=[Select id,Name,TrackingNumber__c,AccountName__r.Name,AccountName__r.BillingAddress,AccountName__r.BillingCountry,AccountName__r.BillingCity,AccountName__r.BillingPostalCode,AccountName__r.Phone,AccountName__r.Emailid__c FROM CustomOpportunity__c WHERE Id=:oppId]; Customlineitem=[Select Name,ActualPrice__c,Quantity__c,UnitPrice__c,CustomProduct__r.Name FROM CustomOpportunitylineitem__c WHERE CustomOpportunity__c=:oppId]; subtotal=0; discount=0; total=0; tax=0; for(CustomOpportunitylineitem__c co:Customlineitem) { subtotal=subtotal+co.ActualPrice__c; } total=subtotal-discount+tax; } }
SwethaSwetha (Salesforce Developers) 
checking...