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
AshwAshw 

System.QueryException: List has no rows for assignment to SObject Error is in expression '{!change}' in page preCall: Class.preCall_cls.Change: line 5, column 1

public with sharing class preCall_cls {

    public Void Change() {
        if(conId != '--None--'){
            Contact obj = [select id,name,AccountId,Phone from Contact where name =: conId];
                co.AccountId= obj.AccountId;
                co.Phone= obj.Phone;
        }
        else
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please select correct value');
            apexpages.addmessage(myMsg);
            co.AccountId='';
            co.Phone='';
        }  
    }

        public list<SelectOption> AccValues {get;set;}
        public Contact co {get;set;}
        public string conId {get;set;} 
        
        public preCall_cls(){
                AccValues = new List<SelectOption>();
                list<Contact> lst = [select id,name,AccountId,Phone from Contact];
                    AccValues.add(new selectoption('--None--','--None--'));
                for(Contact con : lst){
                        AccValues.add(new selectoption(con.id,con.Name));
                }
                co = new Contact();
                //co = [select id,name,AccountId,Phone from Contact where name =: conId];
        }
}

AshwaniAshwani
Use code like:

public with sharing class preCall_cls {

    public Void Change() {
        if(conId != '--None--'){

            List<Contact> obj = [select id,name,AccountId,Phone from Contact where name =: conId];
               if( obj.size()>0) {
                co.AccountId= obj[0].AccountId;
                co.Phone= obj[0].Phone;
                }
        }
        else
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please select correct value');
            apexpages.addmessage(myMsg);
            co.AccountId='';
            co.Phone='';
        }  
    }

        public list<SelectOption> AccValues {get;set;}
        public Contact co {get;set;}
        public string conId {get;set;} 
        
        public preCall_cls(){
                AccValues = new List<SelectOption>();
                list<Contact> lst = [select id,name,AccountId,Phone from Contact];
                    AccValues.add(new selectoption('--None--','--None--'));
                for(Contact con : lst){
                        AccValues.add(new selectoption(con.id,con.Name));
                }
                co = new Contact();
                //co = [select id,name,AccountId,Phone from Contact where name =: conId];
        }
}

In your case query on Account return nothing.
Virendra ChouhanVirendra Chouhan
Hi

Your Contact name is uniqe or not ?
if not then write LIMIT 1 in your query.

Regards
Virendra
AshwAshw
Hi Avilion thanks for ur response

But still code is not working even phone field is also not populating 
AshlekhAshlekh
Hi 

When user select any value from controller than you will get Id in controller not name sos in where clause you have to apply id instead of name

public with sharing class preCall_cls {

    public Void Change() {
        if(conId != '--None--'){

            List<Contact> obj = [select id,name,AccountId,Phone from Contact where id=: conId];
               if( obj.size()>0) {
                co.AccountId= obj[0].AccountId;
                co.Phone= obj[0].Phone;
                }
        }
        else
        {
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Please select correct value');
            apexpages.addmessage(myMsg);
            co.AccountId='';
            co.Phone='';
        }  
    }

        public list<SelectOption> AccValues {get;set;}
        public Contact co {get;set;}
        public string conId {get;set;} 
        
        public preCall_cls(){
                AccValues = new List<SelectOption>();
                list<Contact> lst = [select id,name,AccountId,Phone from Contact];
                    AccValues.add(new selectoption('--None--','--None--'));
                for(Contact con : lst){
                        AccValues.add(new selectoption(con.id,con.Name));
                }
                co = new Contact();
                //co = [select id,name,AccountId,Phone from Contact where name =: conId];
        }
}

IF it helps you than please mark it as a solution and please like it ENJOY APEX