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
Timothy SmithTimothy Smith 

Illegal Assignment from List to Id - Apex Test

Error:  Illegal Assignment from List to Id

on lines: 24, 32, 40, 48, 56, 64

Thank you
@isTest
private class ContactDNCOpportunityTest {
       
    private static testMethod void testCloseOpps(){
        //Create Account
        
        Account newAcc = FlowTestUtils.createHouseholdAccount();
    		insert newAcc;  
        
        
       //Create Contacts
           List<Contact> conList = new List<Contact> {
		new Contact(FirstName='test1',LastName='tester',AccountId = newAcc.Id, Email = 'test1@testing.com', Phone = '1234567891', Phone_Status__c = 'Do Not Call'),  //Has Phone Number - Do not call, Closed Opp
        new Contact(FirstName='test2',LastName='tester',AccountId = newAcc.Id, Email = 'test2@testing.com'), //Blank No Numbers  Closed Opp
        new Contact(FirstName='test3',LastName='tester',AccountId = newAcc.Id, Email = 'test3@testing.com', Normalized_Work_Phone__c = '1234567891',Work_Phone_Status__c = 'Active'), // Has Work Number, Active, Do Not Close Opp
		new Contact(FirstName='test4',LastName='tester',AccountId = newAcc.Id, Email = 'test4@testing.com', Phone = '1234567891', Phone_Status__c = 'Active'), //Has Phone Number, Active, Do not Close Opp
        new Contact(FirstName='test5',LastName='tester',AccountId = newAcc.Id, Email = 'test5@testing.com', Normalized_Mobile_Phone__c = '1234567891', Mobile_Phone_Status__c = 'Do Not Call'), //Has mobile number, DNC, Close Opp      
        new Contact(FirstName='test6',LastName='tester',AccountId = newAcc.Id, Email = 'test6@testing.com', Normalized_Work_Phone__c = '1234567891', Work_Phone_Status__c = 'Do Not Call') //Has Worknumber DNC is checked
            };	
                
            insert conList;
       
        
        Opportunity opptest1 = new Opportunity(Name = 'Opp1', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test1']);
        Test.startTest();
            insert opptest1;
        Test.stopTest();   
        
        System.assertEquals('Closed', opptest1.StageName);
                
            
        Opportunity opptest2 = new Opportunity(Name = 'Opp2', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test2']);
        Test.startTest();
            insert opptest2;
        Test.stopTest();   
        
        System.assertEquals('Closed', opptest2.StageName);
          
        
         Opportunity opptest3 = new Opportunity(Name = 'Opp3', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test3']);
         Test.startTest();
            insert opptest3;
         Test.stopTest();   
        
        System.assertEquals('Closed', opptest3.StageName);
           
        
         Opportunity opptest4 = new Opportunity(Name = 'Opp4', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test4']);
         Test.startTest();
            insert opptest4;
         Test.stopTest();   
        
        System.assertNotEquals('Closed', opptest4.StageName);
           
        
         Opportunity opptest5 = new Opportunity(Name = 'Opp5', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test5']);  
         Test.startTest();
            insert opptest5;
         Test.stopTest();   
        
        System.assertEquals('Closed', opptest5.StageName);
            
        
         Opportunity opptest6 = new Opportunity(Name = 'Opp6', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test6']);
         Test.startTest();
            insert opptest6;
         Test.stopTest();   
        
        System.assertEquals('Closed', opptest6.StageName);
            
         Opportunity opptest7 = new Opportunity(Name = 'Opp7', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today());                                            
         Test.startTest();
            insert opptest7;
         Test.stopTest();   
        
        System.assertEquals('Closed', opptest7.StageName);
        
    }
             
}



 
CharuDuttCharuDutt
Hii Timothy
Try Below Code
@isTest
private class ContactDNCOpportunityTest {
       
    private static testMethod void testCloseOpps(){
        //Create Account
        
        Account newAcc = FlowTestUtils.createHouseholdAccount();
            insert newAcc;  
        
        
       //Create Contacts
           List<Contact> conList = new List<Contact> {
        new Contact(FirstName='test1',LastName='tester',AccountId = newAcc.Id, Email = 'test1@testing.com', Phone = '1234567891', Phone_Status__c = 'Do Not Call'),  //Has Phone Number - Do not call, Closed Opp
        new Contact(FirstName='test2',LastName='tester',AccountId = newAcc.Id, Email = 'test2@testing.com'), //Blank No Numbers  Closed Opp
        new Contact(FirstName='test3',LastName='tester',AccountId = newAcc.Id, Email = 'test3@testing.com', Normalized_Work_Phone__c = '1234567891',Work_Phone_Status__c = 'Active'), // Has Work Number, Active, Do Not Close Opp
        new Contact(FirstName='test4',LastName='tester',AccountId = newAcc.Id, Email = 'test4@testing.com', Phone = 1234567891, Phone_Status__c = 'Active'), //Has Phone Number, Active, Do not Close Opp
        new Contact(FirstName='test5',LastName='tester',AccountId = newAcc.Id, Email = 'test5@testing.com', Normalized_Mobile_Phone__c = '1234567891', Mobile_Phone_Status__c = 'Do Not Call'), //Has mobile number, DNC, Close Opp      
        new Contact(FirstName='test6',LastName='tester',AccountId = newAcc.Id, Email = 'test6@testing.com', Normalized_Work_Phone__c = '1234567891', Work_Phone_Status__c = 'Do Not Call') //Has Worknumber DNC is checked
            };    
                
            insert conList;
       
        
        
      Test.startTest();
            Opportunity opptest1 = new Opportunity(Name = 'Opp1', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test1'].Id);
            insert opptest1;
        Test.stopTest();   
        
        
        System.assertEquals('Closed', opptest1.StageName);
                
            
        Test.startTest();
             Opportunity opptest2 = new Opportunity(Name = 'Opp2', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test2'].Id);
            insert opptest2;
        Test.stopTest();   
        
        System.assertEquals('Closed', opptest2.StageName);
            
         Test.startTest();
             Opportunity opptest3 = new Opportunity(Name = 'Opp3', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test3'].Id);
            insert opptest3;
         Test.stopTest();   
        
        System.assertEquals('Closed', opptest3.StageName);
            
         Test.startTest();
              Opportunity opptest4 = new Opportunity(Name = 'Opp4', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test4'].Id);
            insert opptest4;
         Test.stopTest();   
        
        System.assertNotEquals('Closed', opptest4.StageName);
            
         Test.startTest();
              Opportunity opptest5 = new Opportunity(Name = 'Opp5', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test5'].Id);
            insert opptest5;
         Test.stopTest();   
        
        System.assertEquals('Closed', opptest5.StageName);
            
         Test.startTest();
              Opportunity opptest6 = new Opportunity(Name = 'Opp6', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test6'].Id);
            insert opptest6;
         Test.stopTest();   
        
        System.assertEquals('Closed', opptest6.StageName);
            
                                                   
         Test.startTest();
              Opportunity opptest7 = new Opportunity(Name = 'Opp7', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today());    
            insert opptest7;
         Test.stopTest();   
        
        System.assertEquals('Closed', opptest7.StageName);
        }
    }
Please Close Your Query By Marking It As Best Answer So It Helps Others In Future
Thank You!

 
Bhaskar Muli 6Bhaskar Muli 6
Hi Charu,

Want to create a test class for below .. Can you please help me 
     
    @AuraEnabled
    public static List<ST_EventSchedulerCntrl.CalendarEvent> getAuditEventList(Id userId){

        //get current user Id
        Id currentUserId = userId;
        if(currentUserId == null){
            currentUserId = UserInfo.getUserId();
        }
        List<ST_EventSchedulerCntrl.CalendarEvent> calendarEventList = null;

        //get the relevant audit list
        List<Quality_Assurance__c> qaAuditList = [Select Id, Name, BOSS_Property__c, BOSS_Property__r.Name,BOSS_Evaluation_Date__c
        from Quality_Assurance__c where BOSS_Auditor_Name__c=:currentUserId OR OwnerId=:currentUserId];

        //if audit list is not empty, convert it into list of calendar event and send to client
        if(!qaAuditList.isEmpty()){
            calendarEventList = new List<ST_EventSchedulerCntrl.CalendarEvent>();
            for(Quality_Assurance__c qaAudit : qaAuditList){
                calendarEventList.add(new ST_EventSchedulerCntrl.CalendarEvent(qaAudit));
            }
        }

        //get the relevant events list
        List<Event> eventsList =[
            SELECT Id, OwnerId, WhoId, WhatId, AccountId,Account__c, RecordTypeId, RecordType.Name, StartDateTime,EndDateTime,Subject, Description,
            Temp_ID__c FROM Event WHERE RecordTypeId =: corpWideEventRecordTypeId OR ((RecordTypeId =: hotelEventRecordTypeId OR (RecordTypeId =:generalEventRecordTypeId and Account__c = null))
            AND (OwnerId =: currentUserId 
            OR Account__r.BOSS_QA_Auditor__c = :currentUserId))
            ORDER BY CreatedDate DESC
        ];

        //if event list is not empty, convert it into list of calendar event and send to client
        if(!eventsList.isEmpty()){
            if(calendarEventList == null)
                calendarEventList = new List<ST_EventSchedulerCntrl.CalendarEvent>();
            for(Event eventObj : eventsList){
                calendarEventList.add(new ST_EventSchedulerCntrl.CalendarEvent(eventObj));
            }
        }
        //final return
        return calendarEventList;

    }

   
    @AuraEnabled
    public static List<String> isSameDay(Id accountId, string startDateString, string endDateString, String alreadyExistingEventsString){
        List<String> startDateData = new List<String>();
        List<String> endDateData = new List<String>();
        List<String> conflictingEventSubjects = new List<String>();
        

        if(!String.isBlank(startDateString)){
            startDateData = startDateString.split('-');
        }

        if(!String.isBlank(endDateString)){
            endDateData = endDateString.split('-');
        }
        Date startDate = Date.newInstance(Integer.valueOf(startDateData[0]), Integer.valueOf(startDateData[1]), Integer.valueOf(startDateData[2]));
        Date endDate = Date.newInstance(Integer.valueOf(endDateData[0]), Integer.valueOf(endDateData[1]), Integer.valueOf(endDateData[2]));
        DateTime startDateTime = DateTime.newInstanceGMT(Integer.valueOf(startDateData[0]), Integer.valueOf(startDateData[1]), Integer.valueOf(startDateData[2]));
        
        DateTime endDateTime = DateTime.newInstanceGMT(Integer.valueOf(endDateData[0]), Integer.valueOf(endDateData[1]), Integer.valueOf(endDateData[2]));
        
        //all existing events are passed as json string and converted to List<ST_EventSchedulerCntrl.CalendarEvent> after removing timezone string
        alreadyExistingEventsString = alreadyExistingEventsString.replaceAll('T[0-9]*:[0-9]*:[0-9]*.[0-9]*Z', '');
        List<ST_EventSchedulerCntrl.CalendarEvent> alreadyExistingEvents = (List<ST_EventSchedulerCntrl.CalendarEvent>) Json.deserialize(alreadyExistingEventsString, List<ST_EventSchedulerCntrl.CalendarEvent>.class);
        Set<Id> allEventIds = new Set<Id>();
        
        //compare with already existing Events of the Current User 
        for(ST_EventSchedulerCntrl.CalendarEvent calEvent : alreadyExistingEvents){
            allEventIds.add(calEvent.targetEntityId);
            if(calEvent.isDeleted == true){
                continue;
            }
            
            //if account id is present for the newly created event on calendar UI
            if(accountId != null){

                //if iterated event is audit compare date and check account id, if they both match then add name to list of conflicting events
                if(calEvent.type == 'Audit Event'){ 
                    Quality_Assurance__c qaRecord = (Quality_Assurance__c)calEvent.getRawData();
                    Date eventStartDate = qaRecord.BOSS_Evaluation_Date__c;
                    Date eventEndDate  = qaRecord.BOSS_Evaluation_Date__c;
                    
                    if((eventStartDate == startDate || eventStartDate ==  endDate ||
                        eventEndDate == startDate || eventEndDate ==  endDate ||
                        (startDate >= eventStartDate && endDate <=  eventEndDate) ||
                        (eventStartDate >= startDate && eventEndDate <=  endDate)  ) 
                        && (qaRecord.BOSS_Property__c == accountId))
                    {   
                        conflictingEventSubjects.add(calEvent.title);
                        
                    }
                }

                //if iterated event is Hotel event, compare account id and date, if it matches then it is a conflicting event
                // if iterated event is Corp-Wide event, then compare dates, and if it matches then it is conflicting event
                if(calEvent.type =='Hotel Event' || calEvent.type =='Corporate-Wide Event'){
                    Event eventRecord = (Event)calEvent.getRawData();
                    DateTime eventStartDate = eventRecord.StartDateTime;
                    DateTime eventEndDate = eventRecord.EndDateTime;
    
                    if(eventStartDate == startDateTime || eventStartDate ==  endDateTime ||
                        eventEndDate == startDateTime || eventEndDate ==  endDateTime ||
                        (startDateTime >= eventStartDate && endDateTime <=  eventEndDate) ||
                        (eventStartDate >= startDateTime && eventEndDate <=  endDateTime)  ) 
                    {
                        if((calEvent.type =='Hotel Event' && eventRecord.Account__c == accountId) || calEvent.type =='Corporate-Wide Event')
                            conflictingEventSubjects.add(eventRecord.Subject);
                    }
                }
            }
            
            //if newly created event doesnt have account id,
            //Check if iterated event is personal event or corp-wide event present on the same dates,
            // if yes, then its a conflicting event
            else if(calEvent.type =='Corporate-Wide Event' || calEvent.type =='Personal Event'){
                
                Event eventRecord = (Event)calEvent.getRawData();
                DateTime eventStartDate = eventRecord.StartDateTime;
                DateTime eventEndDate = eventRecord.EndDateTime;

                if(eventStartDate == startDateTime || eventStartDate ==  endDateTime ||
                    eventEndDate == startDateTime || eventEndDate ==  endDateTime ||
                    (startDateTime >= eventStartDate && endDateTime <=  eventEndDate) ||
                    (eventStartDate >= startDateTime && eventEndDate <=  endDateTime)  ) 
                {
                    conflictingEventSubjects.add(eventRecord.Subject);
                }
            }
        }


        Id userId = UserInfo.getUserId();
           
        List<Event> events = [SELECT Id,Description,Subject, StartDateTime,EndDateTime
                              FROM Event
                              WHERE ((RecordTypeId =: corpWideEventRecordTypeId) OR
                                     (RecordTypeId =: generalEventRecordTypeId AND OwnerId =:userId AND Account__c = NULL) OR
                                     (RecordTypeId =: hotelEventRecordTypeId AND Account__c =:accountId AND Account__c != NULL)) AND
                              ((StartDateTime <= :startDate AND EndDateTime >= :endDate) OR 
                               (StartDateTime >=: startDate AND StartDateTime <=: endDate) OR 
                               (EndDateTime >=: startDate AND EndDateTime <=: endDate)) and Id not in :allEventIds
                             ];

        List<Quality_Assurance__c> qaList = [Select id, Name from Quality_Assurance__c where BOSS_Property__c = :accountId and BOSS_Evaluation_Date__c =: startDate and id not in :allEventIds];
        
        for(Event evRecord : events){
            conflictingEventSubjects.add(evRecord.Subject);
        }

        for(Quality_Assurance__c qaRecord : qaList){
            conflictingEventSubjects.add(qaRecord.Name);
        }
        
        return conflictingEventSubjects;
    }
}