Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a controller that parses values from a record and attaches them as an XFDF file to the SF record. It works perfectly fine on a standard record (Contact, Opportunity, etc.) However, when I try and run it on a custom object (Merchant Application) I recieve this error "List has no rows for assignment to SObject".

"Name" field is a standard field in both objects.

Controller:

public with sharing class myXFDFControllerMerchantApplication {
public myXFDFControllerMerchantApplication() {

}


public myXFDFControllerMerchantApplication(ApexPages.StandardController controller) {

}

private String getXmlString(Merchant_Application__c m)
{
String s = '<?xml version="1.0" encoding="UTF-8"?>' +
'<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">' +
'<f href="https://cs4.salesforce.com/resource/PDFForm"/>' +
'<fields>' +
'<field name="Account"><value>' + m.Name + '</value></field>' +
'</fields><ids original="924410F40FE3EA488AF4E54256CC4300" modified="0D19D7B81C416D44B1EBCB0D6A585227"/>' +
'</xfdf>';

return s;
}
public PageReference XFDFInit() {
Merchant_Application__c m = [SELECT Id, Name, (Select Id From Attachments)
FROM Merchant_Application__c
WHERE id = :ApexPages.currentPage().getParameters().get('id')];

String xmlContent = getXmlString(m);

Attachment attachment = new Attachment();
attachment.Body = Blob.valueOf(xmlContent);
attachment.Name = m.Name + '.XFDF';
attachment.contenttype = 'application/vnd.adobe.xfdf';
attachment.ParentId = m.Id;

insert attachment;

PageReference attachmentPage = new PageReference('/' + attachment.id);
//attachmentPage.setRedirect(true);
return attachmentPage;
}

} 
share|improve this question

2 Answers 2

It's this

Merchant_Application__c m = [SELECT Id, Name, (Select Id From Attachments)
FROM Merchant_Application__c
WHERE id = :ApexPages.currentPage().getParameters().get('id')];

On the left you have an SObject, in this case an instance of Merchant_Application__c.

On the right you have a [SELECT ..] statement which results in a list.

The list (the result of your select) is not returning any rows, which means the assignment to your SObject m is failing.

Make sure that the query is correct. Is the parameter you use in the query being set on the page? Is there data that fulfils the criteria? Also consider adding some Apex Pagemessage error elements.

share|improve this answer

SOQL typically returns a List.

There are certain things you can do to limit it only one record (such as using LIMIT), but typically you will want to assign a SOQL query to a list because of runtime exceptions that are thrown if anything other than only a single record is returned (which is what you are seeing). From their documentation:

SOQL queries can be used to assign a single sObject value when the result list contains only one element. When the L-value of an expression is a single sObject type, Apex automatically assigns the single sObject record in the query result list to the L-value. A runtime exception results if zero sObjects or more than one sObject is found in the list. For example:

List<Account> accts = [SELECT Id FROM Account];

// These lines of code are only valid if one row is returned from
// the query. Notice that the second line dereferences the field from the
// query without assigning it to an intermediary sObject variable.
Account acct = [SELECT Id FROM Account];
String name = [SELECT Name FROM Account].Name;

In your specific scenario, :ApexPages.currentPage().getParameters().get('id') is either returning nothing or the Id that is specified is not found in the database for that object. This is resulting in no records being returned and Salesforce throwing the System.QueryException: List has no rows for assignment to SObject. Note that this is unlike some other programming languages where you may expect the query to just set your sObject to null. Salesforce's documentation for System.QueryException states it is thrown when:

Any problem with SOQL queries, such as assigning a query that returns no records or more than one record to a singleton sObject variable.

share|improve this answer
    
I believe the issue is the Id is not found in the database. The Opportunity object automatically knows to use the Id of the record I’m on but the custom object apparently does not (or the Id is not apparent to the object). I’m new with SF. Anything that I could provide you to help troubleshoot the issue? –  Mark Tauterouff Mar 20 at 18:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.