[SalesForce] Illegal assigment from LIST

I am fairly new to Salesforce, I have a trigger which requires a test method for it to meet its code coverage.

I am getting errors in the test class stating

Error: Compile Error: Illegal assignment from
LIST to Id at line 10 column 9

Trigger: PopulateHolidaysFromObject

trigger PopulateHolidaysFromObject on Opportunity (before insert,before update) {

    List<Holiday__c> HolidayList = [SELECT Name,Holiday_Date__c FROM Holiday__c];

    for(Opportunity opp : Trigger.new){

        HolidayClass holidayclass= new HolidayClass();
        opp.No_Of_Survey_Holidays__c = holidayclass.NoOfHolidays(opp.Site_Visit_Survey_Request_Date__c,opp.Site_Visit_Survey_Completed_Date__c,HolidayList);
        system.debug(opp.No_Of_Survey_Holidays__c);
        opp.No_Of_Report_Holidays__c = holidayclass.NoOfHolidays(opp.Site_Visit_Survey_Request_Date__c,opp.Scope_and_Cost_Report_Completed_Date__c,HolidayList);
        system.debug(opp.No_Of_Report_Holidays__c);        
    }
}

Test Class:

@isTest
private class TerminateServiceTest
{
    static testMethod void testTermination() 
    {
        // Setup the invoice line record
        Invoice_Line__c inv = new Invoice_Line__c();
        inv.Invoicing_Output__c = 'New Invoice';
        inv.Installation_Date__c = system.today();
        inv.Invoicing_Output__c = [SELECT id FROM Invoicing_Output__c];
        insert inv;

        // Setup service instance record
        Service_Instance__c si = new Service_Instance__c();
        si.Opportunity__c = 'New Invoice';
        si.Account__c = [SELECT id FROM Account];
        si.Airport__c = 'GAT';
        si.Contract__c = [SELECT id FROM Contract__c];
        si.Local_Service__c = [SELECT id FROM Local_Service__c];        
        si.Quantity__c = 10;
        si.Service_Start_Date__c = system.today();
        si.Service_End_Date__c = date.newInstance(2016, 8, 11);      
        si.Address__c = [SELECT id FROM Address__c];
        insert si;

        test.startTest();

        inv.Invoicing_Output__c = 'Termination Invoice';
        update inv;

        Service_Instance__c lTest = [SELECT Id, isActive__c FROM Service_Instance__c WHERE Id=:inv.Id];
        system.assertEquals(false, lTest.isActive__c );

        test.stopTest();
    }
}

Can you help me to see what is causing this error?

Best Answer

You are trying to set a lookup field (inv.Invoicing_Output__c) which is an ID, to a SOQL statement that returns a list of objects.

If you want to SOQL for a particular value you would do something like this.

Id myIOId = [SELECT id FROM Invoicing_Output__c].[0].Id;
inv.Invoicing_Output__c = myIOId;

This however is going to come up empty as you have not yet created an Invoicing_Output__c record. You should first create that, insert it, and then set it on the Invoice_Line__c record.

//Setup the Invoicing Output record
Invoicing_Output__c invOP = new Invoicing_Output__c();
//Set the attributes you need
insert invOP;

// Setup the invoice line record
Invoice_Line__c inv = new Invoice_Line__c();
inv.Invoicing_Output__c = 'New Invoice';
inv.Installation_Date__c = system.today();
inv.Invoicing_Output__c = invOP.Id;
insert inv;
Related Topic