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
Tamar ErlichTamar 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
Best Answer chosen by Tamar Erlich
Ajay Nagar 7Ajay Nagar 7
Hi Tamar,

Try this code:
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 {
					if(accts!=null && accts.size()>0){
						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');
					}else{ 
						ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Invalid account name');
						ApexPages.addMessage(msg);
						return null ;
					}
            } catch (Exception e) {
                ApexPages.addMessages(e);
                return null;
            }
        }
    }
}