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
phantom1982phantom1982 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

Hi,

 

I am trying to create an Event through apex, everything seems fine in the Sandbox but the deployment fails

 

System.DMLException:Insert Failed. First exception on row 0; First error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateCalendar, Execution of Before Insert.

 

with the following warning in IDE.

 

WARN [2011-02-15 14:34:40,447] (DeploymentController.java:deployWork:993) - Deployment FAILED.  Got the following deploy messages [3]:
 (1)  filename = classes/UnitTestsUpdateCalendar.cls, result = SUCCESS, affect = changed, id = 01pA00000023yriIAA, fullname = UnitTestsUpdateCalendar
 (2)  filename = package.xml, result = SUCCESS, affect = changed, id = null, fullname = package.xml
 (3)  filename = triggers/UpdateCalendar.trigger, result = SUCCESS, affect = changed, id = 01qA0000000dSiqIAE, fullname = UpdateCalendar

 

 

The trigger is below, its basically creating an event using values from another object..

 

 

trigger UpdateCalendar on SFDC_PTO_Request__c (before insert) {
    if (trigger.isInsert){
        for(SFDC_PTO_Request__c pto: trigger.new){
            Event evt = new Event();
            evt.StartDateTime = pto.Request_Start_Date__c;
            evt.EndDateTime = pto.Request_End_Date__c;
            evt.Description = 'The user is having a ' + pto.Request_Type__c;
            //evt.ShowAs = 'Out of Office';
            SFDC_Employee__c emp = [Select Name from SFDC_Employee__c
            WHERE Id =: pto.Employee__c];
            User u = [SELECT Id from User WHERE Name =: emp.Name];
            
            evt.OwnerId = u.Id;
            evt.Subject = pto.Request_Type__c;
            insert evt;            
        }
    }
}

 

 

 

The test class is below:

 

 

@isTest
private class UnitTestsUpdateCalendar {

    static testMethod void myUnitTest() {
        SFDC_PTO_Request__c pto = new SFDC_PTO_Request__c();
        
        DateTime dT = System.now();
        Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());

        pto.Request_Start_Date__c = myDate;
        pto.Request_End_Date__c = myDate;
        
        list<SFDC_Employee__c> emp= [Select Id from SFDC_Employee__c where Name =: 'Admin Support Propertybase'];
       if (emp.size()>0){
               pto.Employee__c  = emp[0].Id;    
       }

        insert pto;
    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma

You can do one thing,

 

Run this test class in the environment where no records exists. Put debug statements for the queries and then track if you are getting the results from the queries in the trigger.

 

or in the test method delete the existing data using SOQL and then run the test class:

Try to write as below:

 

@isTest
private class UnitTestsUpdateCalendar {

static testMethod void myUnitTest() {

//delete the existing records
delete [Select id from SFDC_PTO_REQUEST__c];

//create the new user with the you are supposing in trigger
User creation will be here

SFDC_PTO_Request__c pto = new SFDC_PTO_Request__c();

DateTime dT = System.now();
Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());

pto.Request_Start_Date__c = myDate;
pto.Request_End_Date__c = myDate;

list<SFDC_Employee__c> emp= [Select Id from SFDC_Employee__c where Name =: 'Admin Support Propertybase'];
if (emp.size()>0){
pto.Employee__c = emp[0].Id;
}

insert pto;
}
}

All Answers

Bhawani SharmaBhawani Sharma

You need to create SFDC_Employee__c record with in your test method intead of fetching this from the database.

then create a user with   "emp.Name".

b-Forceb-Force

It looks 

 

list<SFDC_Employee__c> emp= [Select Id from SFDC_Employee__c where Name =: 'Admin Support Propertybase'];

 

this soql returns 0 records,

So as best practice , you need to insert SFDC_Employee__c records

 

Cheers ,

Bala

phantom1982phantom1982

Thanks for the prompt replies. I have added the Employee record in the test class but still getting the exact same error. Any thoughts?

b-Forceb-Force

Please post your trigger and test class,

so we can look into it

 

Thanks,

Bala

Bhawani SharmaBhawani Sharma

You can do one thing,

 

Run this test class in the environment where no records exists. Put debug statements for the queries and then track if you are getting the results from the queries in the trigger.

 

or in the test method delete the existing data using SOQL and then run the test class:

Try to write as below:

 

@isTest
private class UnitTestsUpdateCalendar {

static testMethod void myUnitTest() {

//delete the existing records
delete [Select id from SFDC_PTO_REQUEST__c];

//create the new user with the you are supposing in trigger
User creation will be here

SFDC_PTO_Request__c pto = new SFDC_PTO_Request__c();

DateTime dT = System.now();
Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());

pto.Request_Start_Date__c = myDate;
pto.Request_End_Date__c = myDate;

list<SFDC_Employee__c> emp= [Select Id from SFDC_Employee__c where Name =: 'Admin Support Propertybase'];
if (emp.size()>0){
pto.Employee__c = emp[0].Id;
}

insert pto;
}
}
This was selected as the best answer