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
test777test777 

list has no rows for assignment to SObject.

Hi all,

   i need to write a test class for the following class it shows one failure as list has no rows for assignment to SObject. and it covers 54%.How to fix this issue.Any one can u please help me this .

public class ManagementController {
    public Setting__c Setting {get;set;}    private Boolean NewRecord;    public ManagementController(){        List<Setting__c> Settings = [Select Id, Click_License__c, User_License__c, Country_Name_Style__c, Country_List__c, Use_International_Data__c, Use_Royal_Mail_UK_Data__c, Use_USPS_Data__c, Royal_Mail_Company__c, Royal_Mail_Reverse__c, USPS_Reverse__c from Setting__c where Name = 'Master'];                if ((Settings != null) && (Settings.size() > 0)) {            Setting = Settings[0];            NewRecord = false;        }        else {            Setting = new Setting__c();            Setting.Name = 'Master';            NewRecord = true;        }    }        public PageReference UpdateRecord() {        upsert Setting;                if (NewRecord) {            SetupStandardRecords standardrecords = new SetupStandardRecords(Setting);            NewRecord = false;        }                UpdateSettingsDocument();                            return null;    }        public String getScriptId() {        return [select Id from Document where DeveloperName = 'Lookup_Component'].Id;    }        private void UpdateSettingsDocument() {        List<Address__c> Addresses = [select Street_Field__c, City_Field__c, State_Field__c, Postalcode_Field__c, Country_Field__c, Company_Field__c from Address__c where Setting__c = :Setting.Id];        List<Action__c> Actions = [select Event__c, Code__c from Action__c where Setting__c = :Setting.Id];            SettingsPublisher Publisher = new SettingsPublisher();        Publisher.UpdateDocument(Setting, Addresses, Actions);    }        static testMethod void testManagementController() {     SettingsPublisher Publisher = new SettingsPublisher();        ManagementController controller = new ManagementController();        Setting__c s=new Setting__c(name='Master');        insert s;                controller.UpdateRecord();        controller.getScriptId();        controller.UpdateSettingsDocument() ;   

}}

 

 

Thanks in advance

kiranmutturukiranmutturu

just put a conidtion where ever you are using the list Settings .....like if (!results.IsEmpty() >0 )... and try

test777test777

Hi ,

 

   Thank u for ur replay.

    i put the condition like.if(!results.isEmpty()).But its not getting.can u please give me the sample code for this.

 

Thanks in advance.

steve456steve456

For dat sobbject Soql query give Limit=1.......it will work

vishal@forcevishal@force

in your test method, you are only creating a setting__c with the name = 'Master'

but you are using 2 other lists where you are qyerying Address and Action having this setting__c. So for that case you need to create a test Address and a test Action record and give setting__c = s.Id for those records.

 

That should sove your problem!