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
whitfty1whitfty1 

System.DMLException: Insert failed. Error. ... CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY message

I can't seem to wrap my brain around what is wrong.  Below is my code but I receive the CANNOT_INSERT error message and the apex trigger has 6 lines not tested (11, 14, 16, 18, 19, and 20)

trigger Reassign on Case(Before Insert, before Update)
{	
		case[] c = trigger.new;	
	
	// First retrieve submitted users accountID
	contact[] myContact = [Select AccountId FROM contact where Email = :c[0].SuppliedEmail]; 
	
	//Now see if this account owner is associated with an account partner	
	accountpartner[] myAccountPartner = [Select AccountToId FROM AccountPartner WHERE AccountFromId = :myContact[0].AccountId];
	//accountpartner[] myAccountPartner = [Select AccountToId FROM AccountPartner WHERE AccountFromId = '001W0000006GWPQ'];
	                                     
	//make sure it is not null
	if (myAccountPartner[0].AccountToId <> NULL)
	{
	//if the account partner is a delegated partner then reassign 
	account[] partnerInfo = [Select ispartner FROM account where ID  = :myAccountPartner[0].AccountToId]; 
	
	if (partnerInfo[0].ispartner==true) { 	 
           // c.OwnerID = myAccountPartner[0].AccountToId.getUserId();  
              c[0].OwnerID = myAccountPartner[0].AccountToId; 
              c[0].StatusReason__c = 'Assigned';
              c[0].Status = 'Waiting Activation';
                    
	 }
	}
}

 

@isTest
private class TestReassign {

    static testMethod void myUnitTest() {
    	Test.startTest(); 
    	
    	
    	//Create portal account owner
		UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
		Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
		User portalAccountOwner1 = new User(
			UserRoleId = portalRole.Id,
			ProfileId = profile1.Id,
			Username = System.now().millisecond() + 'test2@test.com',
		   	Alias = 'batman',
			Email='bruce.wayne@wayneenterprises.com',
			EmailEncodingKey='UTF-8',
			Firstname='Bruce',
			Lastname='Wayne',
			LanguageLocaleKey='en_US',
			LocaleSidKey='en_US',
			TimeZoneSidKey='America/Chicago'
		);
		Database.insert(portalAccountOwner1);
		
		//Create account
		Account portalAccount1 = new Account(
			Name = 'Test - Partner',
			//isPartner = 'true',
			OwnerId = portalAccountOwner1.Id
		);
		Database.insert(portalAccount1);
		
		Account endAccount = new Account(
			Name = 'Test - End',			
			OwnerId = portalAccountOwner1.Id
		);
		Database.insert(endAccount);
		
		Account standardAccount = new Account(
			Name = 'Test - Standard',
			OwnerId = portalAccountOwner1.Id
		);
		Database.insert(standardAccount);
		
		/* Add to Account Partner Database
		AccountPartner partnerAccount1 = new AccountPartner(
			AccountFromId = portalAccount1.Id,
			AccountToId = endAccount.Id,
			Role = 'ACME Partner (Support)'
		);
		Database.insert(partnerAccount1);
		
				
		AccountPartner partnerAccount2 = new AccountPartner(
			AccountFromId = endAccount.id,
			AccountToId = portalAccount1.Id,
			Role = 'End User'
		);
		Database.insert(partnerAccount2);
		*/
		    	
		//Create contact
		Contact contact1 = new Contact(
		   	FirstName = 'Partner',
		    	Lastname = 'User',
			AccountId = portalAccount1.Id,
		    	Email = System.now().millisecond() + 'user@2partner.com'
		);
		Database.insert(contact1);
		
			//Create contact
		Contact contact2 = new Contact(
		   	FirstName = 'End',
		    	Lastname = 'User',
			AccountId = endAccount.Id,
		    	Email = System.now().millisecond() + 'user@2end.com'
		);
		Database.insert(contact2);
		
			//Create contact
		Contact contact3 = new Contact(
		   	FirstName = 'Standard',
		    	Lastname = 'User',
			AccountId = standardAccount.Id,
		    	Email = System.now().millisecond() + 'user@2standard.com'
		);
		Database.insert(contact3);
		    	
		//Create user
		Profile portalProfile = [SELECT Id FROM Profile WHERE Name LIKE '%Custom - Gold Partner User%' Limit 1];
		User user1 = new User(
			Username = System.now().millisecond() + 'test12345@test.com',
			ContactId = contact1.Id,
			
			ProfileId = portalProfile.Id,
			Alias = 'test123',
			Email = 'test12345@test.com',
			EmailEncodingKey = 'UTF-8',
			LastName = 'User',
			CommunityNickname = 'test12345',
			TimeZoneSidKey = 'America/Los_Angeles',
			LocaleSidKey = 'en_US',
			LanguageLocaleKey = 'en_US'
	);
		Database.insert(user1);		

		List<Case> lstCases = new List<Case>();		   
		   // Create Contact
		   Contact conNew = new Contact(Lastname='Test Contact');
		   insert conNew;
		      // Create Cases		   	
		// lstCases.add(new Case(ContactId=contact2.Id, SuppliedEmail='test12345@test.com', Support_Level__c = 'Gold', Environment__c = 'Production', Status = 'Waiting Assignment', Version__c = 8.00, Product__c = 'AB',  Origin='Web Portal'));
		 lstCases.add(new Case(ContactId=contact3.Id, SuppliedEmail='user@2partner.com', Support_Level__c = 'Gold', Environment__c = 'Production', Status = 'Waiting Assignment', Version__c = 8.00, Product__c = 'AB',  Origin='Web Portal'));
		 lstCases.add(new Case(ContactId=conNew.Id, SuppliedEmail='user@2end.com', Support_Level__c = 'Gold', Environment__c = 'Production', Status = 'Waiting Assignment', Version__c = 8.00, Product__c = 'AB',  Origin='Web Portal'));
		   
		   insert lstCases;  
	        Test.stopTest();      
	        
	    }
	}

 

whitfty1whitfty1

After a little more research, it appears that my problem is more of having a NULL object.  The trigger doesn't test the code :

accountpartner[] myAccountPartner = [Select AccountToId FROM AccountPartner WHERE AccountFromId = :myContact[0].AccountId];

 

 

 

This would make sense as I haven't placed any information into these fields in the test class.  However, when I try to put place something in these fields, I receive a "field is not writeable: AccountPartner.AccountToId" Using this code:

"accountpartner[] myAccountPartner = [Select AccountToId FROM AccountPartner WHERE AccountFromId = :myContact[0].AccountId];"

 

Since this is a different problem, I will repost.