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
Roberto NistiRoberto Nisti 

when delete a record: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Event.Subject

When we try delete in 26 line (evtToCancel), this message show in log: "System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Event.Subject"
 
public class CancelSchedule {
@InvocableMethod
        
    public static list<id> CancelSchedule(list<string> Ids){
        
        Try{
            Id OppId;
            for(string IDaux : ids){
                OppId = ID.valueOf(IDaux.left(15));
            }
            integer pos = 0;
            integer postocancel;
            integer postopernoite;
            event evtToCancel;
            Oportunidade_de_venda__c Opp = [select id, tw1_agendamento__c, tw1_terapeuta__c from Oportunidade_de_venda__c where id = :OppId];
            id terapeutaId = Opp.tw1_terapeuta__c;
            datetime agenda = Opp.TW1_Agendamento__c;
            date dataagenda = Opp.TW1_Agendamento__c.date();
            evtToCancel = [Select id, Subject, whatid, startdatetime from event where Ownerid = :terapeutaid and StartDateTime = :agenda and Subject = 'Visita' limit 1];
            event evtForCancel =  [Select id, Subject, whatid, startdatetime from event where id = :evtToCancel.Id]; 
            system.debug('Passei pelo primeiro evento');
            event[] evt = [Select id, whatid, startdatetime from event where Ownerid = :terapeutaid and DAY_ONLY(StartDateTime) = :dataagenda order by StartDateTime];
            for(event evtaux : evt){
                system.debug('Evento '+ evtaux.whatid);
                if(evtaux.id == evtToCancel.Id){
                     delete evtForCancel;
                }
                if(evtaux.Subject == 'Pernoite'){
                    CheckandFeed.recalculaprimeiro(dataagenda.addDays(1), terapeutaId);
                    delete evtaux;
                }
                pos++;
            }
            Opp.TW1_Agendamento__c = Null;
            Opp.TW1_Terapeuta__c = Null;
            Opp.TW1_Status_Agendamento__c = 'Pré-agendado';
            
            update Opp;
            List<ID> atdId = new List<ID>();
                atdId.add(Opp.Id);
    
    
                return atdId;
        } catch (DmlException e) {
            System.debug('A DML exception has occurred: ' + e.getMessage());
            return NUll;
        }
       
    }

}

 
Best Answer chosen by Roberto Nisti
Khan AnasKhan Anas (Salesforce Developers) 
Hi Roberto,

Greetings to you!

The error means you're trying to access something that you didn't include in the query, in this case, subject, you need to either remove the reference to that, or update your query to include that info.

You are accessing evtaux.Subject but didn't query for it first in your SOQL.

Change your query to include subject:
event[] evt = [Select id, whatid, startdatetime, subject from event where Ownerid = :terapeutaid and DAY_ONLY(StartDateTime) = :dataagenda order by StartDateTime];

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Roberto,

Greetings to you!

The error means you're trying to access something that you didn't include in the query, in this case, subject, you need to either remove the reference to that, or update your query to include that info.

You are accessing evtaux.Subject but didn't query for it first in your SOQL.

Change your query to include subject:
event[] evt = [Select id, whatid, startdatetime, subject from event where Ownerid = :terapeutaid and DAY_ONLY(StartDateTime) = :dataagenda order by StartDateTime];

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Raj VakatiRaj Vakati
Use this code and you need to include all fields in Select cause 
 
public class CancelSchedule {
@InvocableMethod
        
    public static list<id> CancelSchedule(list<string> Ids){
        
        Try{
            Id OppId;
            for(string IDaux : ids){
                OppId = ID.valueOf(IDaux.left(15));
            }
            integer pos = 0;
            integer postocancel;
            integer postopernoite;
            event evtToCancel;
            Oportunidade_de_venda__c Opp = [select id, tw1_agendamento__c, tw1_terapeuta__c ,TW1_Status_Agendamento__c
			from Oportunidade_de_venda__c where id = :OppId];
            id terapeutaId = Opp.tw1_terapeuta__c;
            datetime agenda = Opp.TW1_Agendamento__c;
            date dataagenda = Opp.TW1_Agendamento__c.date();
            evtToCancel = [Select id, Subject, whatid, startdatetime 
			from event where Ownerid = :terapeutaid and StartDateTime = :agenda and Subject = 'Visita' limit 1];
            event evtForCancel =  [Select id, Subject, whatid, startdatetime from event where id = :evtToCancel.Id]; 
            system.debug('Passei pelo primeiro evento');
            event[] evt = [Select id, whatid, Subject ,startdatetime from event where Ownerid = :terapeutaid and DAY_ONLY(StartDateTime) = :dataagenda order by StartDateTime];
            for(event evtaux : evt){
                system.debug('Evento '+ evtaux.whatid);
                if(evtaux.id == evtToCancel.Id){
                     delete evtForCancel;
                }
                if(evtaux.Subject == 'Pernoite'){
                    CheckandFeed.recalculaprimeiro(dataagenda.addDays(1), terapeutaId);
                    delete evtaux;
                }
                pos++;
            }
            Opp.TW1_Agendamento__c = Null;
            Opp.TW1_Terapeuta__c = Null;
            Opp.TW1_Status_Agendamento__c = 'Pré-agendado';
            
            update Opp;
            List<ID> atdId = new List<ID>();
                atdId.add(Opp.Id);
    
    
                return atdId;
        } catch (DmlException e) {
            System.debug('A DML exception has occurred: ' + e.getMessage());
            return NUll;
        }
       
    }

}

 
Roberto NistiRoberto Nisti
Thank you, you both are right. Tahnk you again!!!