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
mandycmandyc 

Trigger Test Code CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

Hello,

 

I have the below trigger which works but fails the test code (also included below). Any help is greatly appreciated!!

 

trigger createNewOpportunity on Order__c (after insert, after update) {

    Id orderRT = [SELECT Id FROM RecordType WHERE SobjectType = 'Order__c' and Name = 'Order'].Id;
    Id oppRT = [SELECT Id FROM RecordType WHERE SobjectType = 'Opportunity' AND Name = 'Non-Profit'].Id;
    Id TTO = [SELECT Id FROM Account WHERE Agreement_Company_Code__c = 'W'].Id;
    

    // collection of Group Names for creating opp/portfolio records
    Set<String> groups = new Set<String>();
    for (Order__c o : Trigger.new){
        if (o.Opportunity_Name__c <> Null && o.Status__c == 'Opportunity Created'){
            groups.add('Order Mgmt - ' + o.Group__c);
        }
    }

    // Map of Portfolios matching the groups set above
    Map<String, Portfolio_Lookup__c> ports = new Map<String, Portfolio_Lookup__c>();
    if (groups.size() > 0){
        for (Portfolio_Lookup__c pl : [SELECT Name FROM Portfolio_Lookup__c WHERE IsDeleted = False AND Name IN :groups]){
            ports.put(pl.Name, pl);
        }
    }

    // List of Organizations to get Org Name for opportunity name below
    List<Id> OrgIds = new List<Id>();
    for (Order__c ord : Trigger.new){
        OrgIds.add(ord.Account__c);
    }
    
    Map<Id, Account> mp = new map<Id, Account>([SELECT Id, Name FROM Account WHERE Id IN :OrgIds]);

    List<Opportunity> o = new List<Opportunity>();
    List<Portfolio_Opportunity__c> po = new List<Portfolio_Opportunity__c>();
        for (Order__c order: Trigger.New){
            if ((Trigger.isInsert && order.Contact__c <> Null && order.Account__c <> Null && order.RecordTypeId == orderRT && order.Status__c == 'Open') || (Trigger.isUpdate && order.Contact__c <> Null && order.Account__c <> Null && order.RecordTypeId == orderRT && order.Status__c == 'Open' && Trigger.oldMap.get(order.Id).Status__c == 'Open')){
                //Generate Opportunity Number
                getOppNumber controller = new getOppNumber();
                string oppNumber = controller.generateOpportunityNumber(TTO);
                //Insert New Opportunity
                o.add(new Opportunity(
                    RecordTypeId = oppRT,
                    Record_Sub_Type__c = 'Simple Agreement',
                    AccountId = order.Account__c,
                    Opportunity_Number__c = oppNumber,
                    StageName = 'Initial Contact/Exploration',
                    Suborder_Id__c = order.Id,
                    CloseDate = system.today().addDays(90),
                    Name = mp.get(order.Account__c).Name + oppNumber.removeStart('D'))); 
             }
             else if (Trigger.isUpdate && Trigger.oldMap.get(order.Id).Status__c != order.Status__c && order.Status__c == 'Opportunity Created'){
             //Create new Portfolio_Opportunity__c record to link the correct product group portfolio record
                 po.add(new Portfolio_Opportunity__c(
                    Opportunity_Name__c = order.Opportunity_Name__c,
                    Portfolio__c = ports.get('Order Mgmt - ' + order.Group__c).Id));       
             }
       }
       if (o.size() > 0) {
           insert o; //Insert Opportunities
       }    
       if (po.size() > 0){ //Insert Portfolio_Opportunity__c records
           insert po;
       }         
}

 

Test Code:

public class Trigger_TestCode_Order
{
   
    static testMethod void testcreateNewOpportunity()
    {        
        //Create New Account (Org) Record
        Account newAcct = new Account();
        newAcct.Name = 'Bubba Gump Shrimp Co';
        newAcct.RecordTypeId = '012500000005HjFAAU';
        insert(newAcct);
        Id OrgId = newAcct.Id; 
        
        //Create New Account (Org) Record for TTO
        Account newAct = new Account();
        newAct.Name = 'WiCell';
        newAct.RecordTypeId = '012500000005HjFAAU';
        newAct.Agreement_Company_Code__c = 'W';
        insert(newAct);
        
        //Create New Contact Record to link to Account
        Contact newCon = new Contact();
        newCon.FirstName = 'Tommy';
        newCon.LastName = 'Tucker';
        insert(newCon);
        Id ContactId = newCon.Id;        
        
        //Create New Order Record
        Id orderRT = [SELECT Id FROM RecordType WHERE SobjectType = 'Order__c' and Name = 'Order' and IsActive = True LIMIT 1].Id;
        Order__c odr = new Order__c();
        odr.RecordTypeId = orderRT;
        odr.Name = '999999';
        odr.Status__c = 'Open';
        odr.Group__c = 'WI';
        odr.PI_Name__c = 'Susie Smith';
        odr.Contact__c = ContactId;
        odr.Account__c = OrgId;
        insert(odr);
        String odrID = odr.Name;        
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
mandycmandyc

I resolved my errors by setting @isTest(SeeAllData=true) at the top of my class that contained the test code.

All Answers

bob_buzzardbob_buzzard
Which insert statement is failing? Can you post the exact error message?
mandycmandyc

Sorry! I forgot to include the error. 

 

Time Started2/8/2013 8:46 AM
ClassTrigger_TestCode_Order
Method NametestcreateNewOpportunity
Pass/FailFail
Error MessageSystem.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, createNewOpportunity: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateOpportunityRelated: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 0 with id a1FP00000005zDZMAY; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, createNewOpportunity: execution of AfterUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.createNewOpportunity: line 55, column 1

Trigger.CreateOpportunityRelated: line 24, column 1: []

Trigger.createNewOpportunity: line 61, column 1: []
Stack TraceClass.Trigger_TestCode_Order.testcreateNewOpportunity: line 39, column 1
mandycmandyc

I resolved my errors by setting @isTest(SeeAllData=true) at the top of my class that contained the test code.

This was selected as the best answer