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
EvertonSzekeresEvertonSzekeres 

REQUIRED_FIELD_MISSING - DurationInMinutes

Hi,

I'm trying to deploy a trigger and a test class, but I got an error:
 
trigger CriarEventoOp_visita_agendada on Opportunity (after insert, after update) {

    
    
List<Event> EventRe = new List<Event>();
        for (Opportunity op : Trigger.new){
        if (op.StageName == 'Visita Agendada' || op.StageName == 'Visita reagendada'){
                EventRe.add (new Event(
                         EndDateTime = op.data_da_visita__c,
                         OwnerId = op.OwnerId,
                         Respons_lvel__c = op.Respons_lvel__c,
                         StartDateTime = op.data_da_visita__c,
                         Subject = 'Visita',
                         Whatid = op.id
                         ));
            }

            }
    insert EventRe;
}
 
@IsTest(SeeAllData=true)
public class CriarEventoOp_visita_agendada_test {
	static testmethod void MyUnitTest(){
        
    Opportunity op = new Opportunity();
        op.Name = 'teste';
        op.StageName = 'Visita Agendada';
        op.CloseDate = date.today();
        
    insert op;

        
        op.OwnerId = 'op.OwnerId';
        op.Respons_lvel__c = 'op.Respons_lvel__c';
    update op;
        
    Event ev = new Event();
        ev.DurationInMinutes = 1;
        
        ev.EndDateTime = op.data_da_visita__c;
        ev.OwnerId = op.OwnerId;
        ev.Respons_lvel__c = op.Respons_lvel__c;
        ev.StartDateTime = op.data_da_visita__c;
        ev.Subject = 'Visita';
        ev.Whatid = op.id;
    insert ev;

    }
}

Error:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CriarEventoOp_visita_agendada: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Campos obrigatórios ausentes: [DurationInMinutes]: [DurationInMinutes] Trigger.CriarEventoOp_visita_agendada: line 19, column 1: [] 
Stack Trace: Class.CriarEventoOp_visita_agendada_test.MyUnitTest: line 10, column 1

"DurationInMinutes" it is not a Opportunity field.
But it is a field in Event.
Why I got this?
Edgar MoranEdgar Moran
You should add : ev.DurationInMinutes =1440;//any Integer and that should work.

Best!
Abhilash Mishra 13Abhilash Mishra 13
Hi  you have missed DurationInMinutes​ field in your Trigger. You should add assign this field any Integer value you liked.
here try this.
trigger CriarEventoOp_visita_agendada on Opportunity (after insert, after update) {

    
    
List<Event> EventRe = new List<Event>();
        for (Opportunity op : Trigger.new){
        if (op.StageName == 'Visita Agendada' || op.StageName == 'Visita reagendada'){
                EventRe.add (new Event(
                         EndDateTime = op.data_da_visita__c,
                         OwnerId = op.OwnerId,
                         Respons_lvel__c = op.Respons_lvel__c,
                         StartDateTime = op.data_da_visita__c,
                         Subject = 'Visita',
                         Whatid = op.id,
                         DurationInMintues=1 
                         ));
            }

            }
    insert EventRe;
}
I have added DurationInMinutes as the last property.

Please mark the Question solved by selecting a best answer, if it resolves your issue.

Good luck :)