You need to sign in to do that
Don't have an account?
Tamar Erlich
Site.com VF page error message
I am trying to adapt my first VF page with Force.com Site for a simpe input form. I have adapted the code found here for my custom object:
https://developer.salesforce.com/page/Creating_Custom_Web-To-Case_Forms_Using_Visualforce_and_Sites
My intake page shows correctly in my site but when I click "Submit Project" I get the following error:
"List has no rows for assignment to SObject"
I am assuming the problem is in this line of code:
pr.Account__c = accts.get(0).Id;
but I do not know why my list has no rows if I enter a valid account name on my form? If I enter an invalid account name or leave the account name blank, i do get the correct error message 'Invalid Account Name' as expected.
Here is my code:
APEX CLASS:
public with sharing class SubmitProjectController {
public Projects__c pr { get; set; }
public String acctName { get; set; }
public String opptyName { get; set; }
public SubmitProjectController() {
pr = new Projects__c ();
}
public PageReference submitProject() {
List<Account> accts = [SELECT Id FROM Account WHERE Name = :acctName];
if (accts.size() != 1) {
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Invalid account name');
ApexPages.addMessage(msg);
return null;
} else {
try {
pr.Account__c = accts.get(0).Id;
// now look for an associated opportunity with the same name
Opportunity opt = [SELECT Id FROM Opportunity WHERE AccountId = :pr.Account__c AND Name = :opptyName LIMIT 1];
if (opt != null)
pr.Opportunity__c = opt.Id;
// Insert the project
INSERT pr;
return new PageReference('/Thank_You');
} catch (Exception e) {
ApexPages.addMessages(e);
return null;
}
}
}
}
VF PAGE:
<apex:page controller="SubmitProjectController">
<h1>Submit New Project </h1>
<apex:form >
<apex:pageMessages />
<table>
<tr>
<th>Project Name:</th>
<td><apex:inputText value="{!pr.Name}"/></td>
</tr>
<tr>
<th>Account Name:</th>
<td><apex:inputText value="{!acctName}"/></td>
</tr>
<tr>
<th>Opportunity Name:</th>
<td><apex:inputText required="true" value="{!opptyName}"/></td>
</tr>
<tr>
<th>Project Description:</th>
<td><apex:inputTextArea required="true" rows="5" value="{!pr.Description__c}"/></td>
</tr>
<tr>
<td><apex:commandButton value="Submit Project" action="{!submitProject}"/></td>
</tr>
</table>
</apex:form>
</apex:page>
I am an absolute beginner in Apex and VF and I would appreciate any help, thank you.
Tamar Erlich
https://developer.salesforce.com/page/Creating_Custom_Web-To-Case_Forms_Using_Visualforce_and_Sites
My intake page shows correctly in my site but when I click "Submit Project" I get the following error:
"List has no rows for assignment to SObject"
I am assuming the problem is in this line of code:
pr.Account__c = accts.get(0).Id;
but I do not know why my list has no rows if I enter a valid account name on my form? If I enter an invalid account name or leave the account name blank, i do get the correct error message 'Invalid Account Name' as expected.
Here is my code:
APEX CLASS:
public with sharing class SubmitProjectController {
public Projects__c pr { get; set; }
public String acctName { get; set; }
public String opptyName { get; set; }
public SubmitProjectController() {
pr = new Projects__c ();
}
public PageReference submitProject() {
List<Account> accts = [SELECT Id FROM Account WHERE Name = :acctName];
if (accts.size() != 1) {
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Invalid account name');
ApexPages.addMessage(msg);
return null;
} else {
try {
pr.Account__c = accts.get(0).Id;
// now look for an associated opportunity with the same name
Opportunity opt = [SELECT Id FROM Opportunity WHERE AccountId = :pr.Account__c AND Name = :opptyName LIMIT 1];
if (opt != null)
pr.Opportunity__c = opt.Id;
// Insert the project
INSERT pr;
return new PageReference('/Thank_You');
} catch (Exception e) {
ApexPages.addMessages(e);
return null;
}
}
}
}
VF PAGE:
<apex:page controller="SubmitProjectController">
<h1>Submit New Project </h1>
<apex:form >
<apex:pageMessages />
<table>
<tr>
<th>Project Name:</th>
<td><apex:inputText value="{!pr.Name}"/></td>
</tr>
<tr>
<th>Account Name:</th>
<td><apex:inputText value="{!acctName}"/></td>
</tr>
<tr>
<th>Opportunity Name:</th>
<td><apex:inputText required="true" value="{!opptyName}"/></td>
</tr>
<tr>
<th>Project Description:</th>
<td><apex:inputTextArea required="true" rows="5" value="{!pr.Description__c}"/></td>
</tr>
<tr>
<td><apex:commandButton value="Submit Project" action="{!submitProject}"/></td>
</tr>
</table>
</apex:form>
</apex:page>
I am an absolute beginner in Apex and VF and I would appreciate any help, thank you.
Tamar Erlich
Try this code: