You need to sign in to do that
Don't have an account?
Joanne Butterfield
List has no rows for assignment to SObject (VF Page & controller extension)
Hi All,
I am trying to display opportunity products on the standard case page layout (The case is related to an opportunity using a look-up)
I am getting the error "Content cannot be displayed: List has no rows for assignment to SObject" I am not a developer and any help would be much appreciated in completing this. I'll owe you some beers at Dreamforce :)
Controller Extension
Public With Sharing Class crossObjectOpportunityInfo {
Public Opportunity o {get; private set;}
Public Case c {get; private set;}
Public List<Schema.FieldSetMember> getFields() {
return SObjectType.OpportunityLineItem.FieldSets.Display_On_Case.getFields();
}
Public crossObjectOpportunityInfo(ApexPages.StandardController sc) {
this.o = [SELECT Id, AccountId FROM Opportunity WHERE RELATED_OPPORTUNITY__C = :sc.getId() LIMIT 1];
String queryString = 'SELECT Id';
List<Schema.FieldSetMember> querySet = SObjectType.OpportunityLineItem.FieldSets.Display_On_Case.getFields();
for(Schema.FieldSetMember f : querySet) {
queryString += ', '+ f.getFieldPath();
}
Id cid = [SELECT Id FROM Contact WHERE AccountId = :o.AccountId LIMIT 1].Id;
queryString += ' FROM Opportunity WHERE id = \''+ cid +'\' LIMIT 1';
}
}
VF Page
<apex:page standardController="Case" extensions="crossObjectOpportunityInfo">
<apex:pageBlock title="Opportunity Products">
<apex:pageBlockSection columns="1">
<apex:pageBlockTable value="{!Case.Related_Opportunity__c}" var="line">
<apex:column headerValue="Product">
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
I am trying to display opportunity products on the standard case page layout (The case is related to an opportunity using a look-up)
I am getting the error "Content cannot be displayed: List has no rows for assignment to SObject" I am not a developer and any help would be much appreciated in completing this. I'll owe you some beers at Dreamforce :)
Controller Extension
Public With Sharing Class crossObjectOpportunityInfo {
Public Opportunity o {get; private set;}
Public Case c {get; private set;}
Public List<Schema.FieldSetMember> getFields() {
return SObjectType.OpportunityLineItem.FieldSets.Display_On_Case.getFields();
}
Public crossObjectOpportunityInfo(ApexPages.StandardController sc) {
this.o = [SELECT Id, AccountId FROM Opportunity WHERE RELATED_OPPORTUNITY__C = :sc.getId() LIMIT 1];
String queryString = 'SELECT Id';
List<Schema.FieldSetMember> querySet = SObjectType.OpportunityLineItem.FieldSets.Display_On_Case.getFields();
for(Schema.FieldSetMember f : querySet) {
queryString += ', '+ f.getFieldPath();
}
Id cid = [SELECT Id FROM Contact WHERE AccountId = :o.AccountId LIMIT 1].Id;
queryString += ' FROM Opportunity WHERE id = \''+ cid +'\' LIMIT 1';
}
}
VF Page
<apex:page standardController="Case" extensions="crossObjectOpportunityInfo">
<apex:pageBlock title="Opportunity Products">
<apex:pageBlockSection columns="1">
<apex:pageBlockTable value="{!Case.Related_Opportunity__c}" var="line">
<apex:column headerValue="Product">
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
You have mentioned that you want to display this page as related list. How are you planning to do so? Are you planning to include this page as an inline visualforce page?
I would suggest the best way will be to give a button on the detail page and when the user clicks the button take the user to this page.
Apex Class
VF Page
Let me know if this helps.
Thanks,
Kaustav
All Answers
this.c = (Case)sc.getRecord();
this.o = [SELECT Id, AccountId FROM Opportunity WHERE Id = :c.related_opportunity__c LIMIT 1];
The variable "cId" contains the Id of a contact. If you query based on this Id from the Opportunity object the result set will always be null.
Thanks,
Kaustav
Thank you!
I would suggest you post your requirement and you entire apex class and VF page.
We can then provide you with a working piece.
Ideally if you are trying to display opportunities then you should iterate over a list of opportunities.
Thanks,
Kaustav
The requirement is to be able to view opportunity product line items related list on a standard case page layout, the case is related to an opportunity via a look-up field (RELATED_OPPORTUNITY__C).
This is the entire apex class and VF page I have completed, as mentioned I am not a developer and haven’t done any coding before.
Any help will be greatly appreciated.
Thank you!
Visualforce Page
<apex:page standardController="Case" extensions="crossObjectOpportunityInfo">
rendered="{!NOT(ISNULL(Case.Related_Opportunity__c))}"
<apex:pageBlock title="Opportunity Products">
<apex:pageBlockSection columns="1">
<apex:pageBlockTable value="{!Case.Related_Opportunity__c}" var="line">
<apex:column headerValue="Product">
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
Apex Class
Public With Sharing Class crossObjectOpportunityInfo {
Public Opportunity o {get; private set;}
Public Case c {get; private set;}
Public List<Schema.FieldSetMember> getFields() {
return SObjectType.OpportunityLineItem.FieldSets.Display_On_Case.getFields();
}
Public crossObjectOpportunityInfo(ApexPages.StandardController sc) {
this.c = (Case)sc.getRecord();
this.o = [SELECT Id, AccountId FROM Opportunity WHERE RELATED_OPPORTUNITY__C = :sc.getId() LIMIT 1];
String queryString = 'SELECT Id';
List<Schema.FieldSetMember> querySet = SObjectType.OpportunityLineItem.FieldSets.Display_On_Case.getFields();
for(Schema.FieldSetMember f : querySet) {
queryString += ', '+ f.getFieldPath();
}
}
}
You have mentioned that you want to display this page as related list. How are you planning to do so? Are you planning to include this page as an inline visualforce page?
I would suggest the best way will be to give a button on the detail page and when the user clicks the button take the user to this page.
Apex Class
VF Page
Let me know if this helps.
Thanks,
Kaustav
This is great, Thank you!
For the test class I thought I would just have to create a new case, however the code coverage is still at 0%.
This is the test class I created, as always any help would be appreciated.
@IsTest
Public Class TestOppProductsOnCaseController {
Static testmethod void insertCase () {
Case c = new Case();
c.AccountId = '001J000001RmXg8';
c.ContactId = '003J0000010M0vN';
c.Origin = 'Sales';
c.OwnerId = '005U00000011NVp';
c.RecordTypeId = '012U0000000URMk';
c.Status = 'Unassigned';
c.Type = 'Installation';
c.Related_Opportunity__c = '006J000000GTZTR' ;
insert c;
}
}
Thank you again, I can’t seem to figure out how to write the test script, I keep getting errors.
How to I associate everything?
Thank you