2

I'm attempting to put this into a VF page which will be the New page for an object. The code is supposed to run this query based upon the value of a field, but I'm getting this error when trying to create a New record :

List has no rows for assignment to SObject

I'm guessing it's because there is not yet a value in the field which is being used to query upon? But that's just my (semi) educated guess. I tried putting those IF ELSE statements in it :

public with sharing class MyController5 {

    public account acc {get;set;}
    public string names {get;set;}
    public Contract_Overview__c contract {get;set;}
    string contractid;

    public MyController5(apexpages.standardcontroller controller){
        acc = new account();
        contractid = system.currentpagereference().getparameters().get('id');
        contract=[select account__c,Subsidiaries_On_Contract__c from Contract_Overview__c  where id =:contractid];
    }

    public list<selectoption> getitems(){
        List<selectoption> options= new list<selectoption>();
        account a =[select name , (select name from Subsidiaries_and_Brands__r) from account where id =:contract.account__c];

        if (account.isEmpty(){
            name == ‘No Sub Selected Yet’;
        } else {


        For(SubsidiariesAndBrands__c s : a.Subsidiaries_and_Brands__r){
            options.add(new SelectOption(s.name,s.name));
        }
        return options;
    }

    public void execute(){}

    public void save(){
        contract.Subsidiaries_On_Contract__c=names;
        update contract;
    }

}
}

but that's giving me this error when I try to save the code :

Error: Compile Error: line 18:14 no viable alternative at character '‘' at line 18 column 14

Line 18 is this one :

name == ‘No Sub Selected Yet’;

I guess I'm referring to the wrong variable, or just not assigning it the correct alternative ? Does anybody have an idea on this ?

Thank you very much for any help.

2

There are a few issues here:

  • name - this variable is never initialized
  • name == ‘No Sub Selected Yet’ - What are you trying to do with this statement? All this does is try to compare the name variable with that string
  • Your if statement doesn't close off the else statement properly. Your if expression isn't even closed off properly with parentheses. I am not even sure how you could save that code.
  • account.isEmpty() doesn't make any sense. There is no account variable and isEmpty() is a List method.
  • The List has no rows for assignment to SObject error is being thrown because your SOQL query isn't returning any values. SOQL always returns a list of objects and it will only properly be assigned to a single variable if only exactly one is returned. If a SOQL query results in zero results or more than one result, your queries will fail. You should be assigning your SOQL queries to List as that will allow zero or more than one result.

Finally, I know we have talked about this before, but I would highly suggest cleaning up your code. I formatted the code for you again a bit, and maybe you are indenting your code in your IDE, but you should try and manage that better. Also, you should become consistent with your naming, camelCase, and spacing. You seem to randomly capitalize things and randomly have spaces between your equal signs and variables/assignments. Now, while this will work fine, it will make it difficult to work with other developers at times. I would suggest reading up on Robert Martin's Clean Code and trying to implement some of his practices.

  • Thank you for the insight Jesse. You're right, I need to clean up my code big time. – user2428 Mar 13 '13 at 16:44
  • and just ftr - that was just a copy\paste from a Notepad. Not all of my code is that unformatted. But yes, I do need to clean up a lot. – user2428 Mar 13 '13 at 16:49
2

You have a number of issues here, firstly the compile error is complaining because you are using ’ instead of ' on both lines (note the subtle, but important difference between the two).

Secondly, you are trying to assign 'No Sub Selected Yet' using the == (equality) operator rather than the = (assignment) operator =.

Thirdly, on the following line, i think you will get an error because that is a List method rather than SObject method (since you are querying by Id, it will return a single SObject rather than a list of SObjects). Additionally, Account is a type rather than a variable. You should check if the Account variable (acc) is Null instead.

    if (account.isEmpty(){

Finally, re your original question "Why is this query producing a “List has no rows for assignment to SObject” error?" this is probably because the Account you are looking for cannot be found, probably because the contract does not have a valid value in account__c (are you using a custom Account field rather than the AccountId on Contract?)

  • Phil, I will need to break this up into two comments because it can't all fit into one. first : Thank you very much for your feedback. I appeciate it so much. Obviously I'm a newbie so please bear with me. I don't understand what you mean about using ’ instead of '. I can see the very difference, but use an apostraphe here ? – user2428 Mar 13 '13 at 17:46
  • I have changed the one line to : if (account = null) { But I don't know what to do after that because this account__c field (yes it's a custom field) will only be blank when the user initially creates the record I the query to run again after the field is populated. So, I don't really know what to do with the code after I verify that the account is null. I don't want to just stop it. I just don't want it to give me an error because there isn't a value in the field yet. Thanks again for the help. – user2428 Mar 13 '13 at 17:46
  • So, with regards your first comment. In Apex, you must use the ' character (usually on the same key as ") to denote a String (text) value. You are getting the error because the compiler doesn't recognise the character you are using ( ’ ) to denote a String value. – Phil Hawthorn Mar 14 '13 at 12:13
  • Your second comment...you should change it to if(account==null) (instead of a single =. If your picklist relies on having an account to effectively populate its values, you of course need to make sure the account is somehow populated. If you need to have the user enter the Account first, it would need to be populated already on the Contract (hence why I asked about using a custom field vs the standard AccountId on the Contract). – Phil Hawthorn Mar 14 '13 at 12:15
  • Phil, It's something that is service produced. If I put a single quote mark in the code it gets transformed to ' once the code has been saved. Thanks – user2428 Mar 14 '13 at 13:37

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