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
BobPBobP 

First exception on row 0; first error: ENTITY_IS_DELETED

I have a test class that is giving me an error I've never seen before. When I try to send it over to my production org via outbound change set and then validated it via inbound change set I receive the error below.
 
System.DmlException: Insert failed. First exception on row 0; first error: ENTITY_IS_DELETED, entity is deleted: [] 
Stack Trace: Class.Test_YushProdFileUpload.testAttachments: line 38, column 1


My test class is also below. any help i can get wuold be appreciated. 
 
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class Test_YushProdFileUpload {

    static testMethod void testAttachments()
    
    
    {
      Yushin_Product__c ypd=new Yushin_Product__c(Discharge_Direction__c='Clamp Traverse');
        
        ypd.Opportunity__c='0063900000rYAez';
        ypd.Account__c ='0013900001dOm8T';
        ypd.Discharge_Direction__c='Clamp Traverse';
        ypd.IMM_Mfr__c='Arburg';
        ypd.IMM_Model__c='NT450';
        ypd.Plant_Voltage__c='110';
        ypd.Interface_Voltage__c='110 VDC';
        insert ypd;
        
        ypd.Opportunity__c='0063900000rYAez';
        ypd.Account__c ='0013900001dOm8T';
        ypd.Discharge_Direction__c='Clamp Traverse';
        ypd.IMM_Mfr__c='Engel';
        ypd.IMM_Model__c='110t';
        ypd.Plant_Voltage__c='110';
        ypd.Interface_Voltage__c='110 VDC';
        update ypd;
        
        VRController_YProdFileUpload controller=new VRController_YProdFileUpload(new ApexPages.StandardController(ypd));
 
      controller.fileName='Unit Test Attachment';
      controller.fileBody=Blob.valueOf('Unit Test Attachment Body');
        controller.uploadFile();
      
      List<Attachment> attachments=[select id, name from Attachment where parent.id=:ypd.id];
      System.assertEquals(1, attachments.size());
    }
}

 
Best Answer chosen by BobP
Raj VakatiRaj Vakati
Try this
 
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class Test_YushProdFileUpload {

    static testMethod void testAttachments()
    
    
    {
		
		 Account a = new Account();
        a.Name = 'Test';
        a.Industry = 'Retail';
        insert a;
        Opportunity o = new Opportunity();
        o.name = 'Test';
        o.AccountId = a.Id;
        o.StageName = 'Closed Won';
        o.CloseDate = date.today();
        o.Type = 'New Customers';
        
        insert o;
		
		
      Yushin_Product__c ypd=new Yushin_Product__c(Discharge_Direction__c='Clamp Traverse');
        
      ypd.Opportunity__c=o.id;
        ypd.Account__c =a.Id; 
        ypd.Discharge_Direction__c='Clamp Traverse';
        ypd.IMM_Mfr__c='Arburg';
        ypd.IMM_Model__c='NT450';
        ypd.Plant_Voltage__c='110';
        ypd.Interface_Voltage__c='110 VDC';
        insert ypd;
     ypd.Opportunity__c=o.id;
        ypd.Account__c =a.Id ; 
        ypd.Discharge_Direction__c='Clamp Traverse';
        ypd.IMM_Mfr__c='Engel';
        ypd.IMM_Model__c='110t';
        ypd.Plant_Voltage__c='110';
        ypd.Interface_Voltage__c='110 VDC';
        update ypd;
        
        VRController_YProdFileUpload controller=new VRController_YProdFileUpload(new ApexPages.StandardController(ypd));
 
      controller.fileName='Unit Test Attachment';
      controller.fileBody=Blob.valueOf('Unit Test Attachment Body');
        controller.uploadFile();
      
      List<Attachment> attachments=[select id, name from Attachment where parent.id=:ypd.id];
      System.assertEquals(1, attachments.size());
    }
}

 

All Answers

Paul S.Paul S.
Try creating an opp and account in your test and reference those Ids rather than hard-coded ones.
Raj VakatiRaj Vakati
Try this code
 
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class Test_YushProdFileUpload {

    static testMethod void testAttachments()
    
    
    {
		
		 Account a = new Account();
        a.Name = 'Test';
         
        a.Industry = 'Retail';
        
        insert a;
        
        
        Opportunity o = new Opportunity();
        o.name = 'Test';
        o.AccountId = a.Id;
        o.StageName = 'Closed Won';
        o.CloseDate = date.today();
        o.Type = 'New Customers';
        
        insert o;
		
		
      Yushin_Product__c ypd=new Yushin_Product__c(Discharge_Direction__c='Clamp Traverse');
        
        ypd.Opportunity__c=o.id;
        ypd.Account__c =a.Id;
        ypd.Discharge_Direction__c='Clamp Traverse';
        ypd.IMM_Mfr__c='Arburg';
        ypd.IMM_Model__c='NT450';
        ypd.Plant_Voltage__c='110';
        ypd.Interface_Voltage__c='110 VDC';
        insert ypd;
        
        ypd.Opportunity__c=o.id';
        ypd.Account__c =a.Id ;
        ypd.Discharge_Direction__c='Clamp Traverse';
        ypd.IMM_Mfr__c='Engel';
        ypd.IMM_Model__c='110t';
        ypd.Plant_Voltage__c='110';
        ypd.Interface_Voltage__c='110 VDC';
        update ypd;
        
        VRController_YProdFileUpload controller=new VRController_YProdFileUpload(new ApexPages.StandardController(ypd));
 
      controller.fileName='Unit Test Attachment';
      controller.fileBody=Blob.valueOf('Unit Test Attachment Body');
        controller.uploadFile();
      
      List<Attachment> attachments=[select id, name from Attachment where parent.id=:ypd.id];
      System.assertEquals(1, attachments.size());
    }
}

 
BobPBobP
Hi Raj,
Thank you for helping. I am getting the following error when compiling the code.

Error: Compile Error: Missing closing quote character ' on string. at line 71 column 66. But it looks like it is there 
Raj VakatiRaj Vakati
Try this
 
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class Test_YushProdFileUpload {

    static testMethod void testAttachments()
    
    
    {
		
		 Account a = new Account();
        a.Name = 'Test';
        a.Industry = 'Retail';
        insert a;
        Opportunity o = new Opportunity();
        o.name = 'Test';
        o.AccountId = a.Id;
        o.StageName = 'Closed Won';
        o.CloseDate = date.today();
        o.Type = 'New Customers';
        
        insert o;
		
		
      Yushin_Product__c ypd=new Yushin_Product__c(Discharge_Direction__c='Clamp Traverse');
        
      ypd.Opportunity__c=o.id;
        ypd.Account__c =a.Id; 
        ypd.Discharge_Direction__c='Clamp Traverse';
        ypd.IMM_Mfr__c='Arburg';
        ypd.IMM_Model__c='NT450';
        ypd.Plant_Voltage__c='110';
        ypd.Interface_Voltage__c='110 VDC';
        insert ypd;
     ypd.Opportunity__c=o.id;
        ypd.Account__c =a.Id ; 
        ypd.Discharge_Direction__c='Clamp Traverse';
        ypd.IMM_Mfr__c='Engel';
        ypd.IMM_Model__c='110t';
        ypd.Plant_Voltage__c='110';
        ypd.Interface_Voltage__c='110 VDC';
        update ypd;
        
        VRController_YProdFileUpload controller=new VRController_YProdFileUpload(new ApexPages.StandardController(ypd));
 
      controller.fileName='Unit Test Attachment';
      controller.fileBody=Blob.valueOf('Unit Test Attachment Body');
        controller.uploadFile();
      
      List<Attachment> attachments=[select id, name from Attachment where parent.id=:ypd.id];
      System.assertEquals(1, attachments.size());
    }
}

 
This was selected as the best answer
BobPBobP
Yes, you beat me to it I removeed the ' from this b> ypd.Opportunity__c=o.id'; and it was good. I'll test and see what happen when i deploy it. 

Thank you
BobPBobP
When I try validation the code it gives me the following error. 
 
System.SObjectException: Field is not writeable: Yushin_Product__c.Opportunity__c 
Stack Trace: Class.Test_YushProdFileUpload.testAttachments: line 59, column 1

 
Raj VakatiRaj Vakati
Looks like Opportunity__c is read only field check it once and delete from the code 
BobPBobP
Yes, I removed the Yushin_Product__c.Opportunity__c from the update and it is working now. Thank you very much Raj!!! Have a good weekend.