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
rajesh kumar 10rajesh kumar 10 

I have facing a problem with the Test class for my trigger can any one please solve my problem below i am providing my trigger and test calss and error i was facing..

My Trigger is:

trigger UpdatePrimaryApllicationAllocationSet on Primary_Application_Allocation__c (after insert,after delete) {

    list<id> lst = new list<id>();
    if(trigger.isInsert){
    for(Primary_Application_Allocation__c paa : trigger.new) {
        lst.add(paa.opportunity__c);
    }}
    if(trigger.isDelete){
        for(Primary_Application_Allocation__c paa : trigger.old) {
        lst.add(paa.opportunity__c);
        }
    }
   
    list<Opportunity> opplst = new list<Opportunity>();
    opplst=[SELECT Primary_Application_Allocation_Set__c FROM Opportunity where Id in :lst];

    if(trigger.isInsert) {
        for(Primary_Application_Allocation__c paa : trigger.new) {
            for(Opportunity o : opplst) {
                if(paa.Opportunity__c == o.Id) {
                    o.Primary_Application_Allocation_Set__c = true;
                }
            }
        }
        update opplst;
    }
   
    if(trigger.isDelete) {
        for(Primary_Application_Allocation__c paa : trigger.old) {
            for(Opportunity o : opplst) {
                if(paa.Opportunity__c == o.id) {
                    o.Primary_Application_Allocation_Set__c = False;
                }
            }
        }
        update opplst;
    }
           
}

and my 
Test calss is:


@isTest

public class TestUpdatePrimaryApllicationAllocation {

    RecordType rt = [select id,name from RecordType where name='Owner' and SobjectType='Account' limit 1];
    RecordType rt1 = [select id,name from RecordType where name='NC Power' and SobjectType='Opportunity' limit 1];
    static testMethod void insertPrimaryApllicationAllocationSet() {
         
        Account acc = new Account(name = 'Test Account',country__c = 'Bangladesh');
        insert acc;
        Opportunity opp = new Opportunity(Name = 'Test Opp',stageName ='Sales Lead',CloseDate = Date.Today(),
                                            ForecastCategoryName = 'Pipeline',CurrencyIsoCode = 'USD - U.S. Dollar');
        insert opp;
        Primary_Application_Allocation__c paa = new Primary_Application_Allocation__c( Opportunity__c = opp.id);
        Test.startTest();
        insert paa;
        Test.stopTest();
        Opportunity updatedOpp = [Select Id, Primary_Application_Allocation_Set__c from Opportunity where id =: opp.id];
        System.assert(updatedOpp.Primary_Application_Allocation_Set__c);
        
    }
    static testMethod void deletePrimaryApllicationAllocationSet(){

        Account acc = new Account(name = 'Test Account');
        insert acc;
        Opportunity opp = new Opportunity(Name = 'Test Opp', Amount = 10020);
        insert opp;
        Primary_Application_Allocation__c paa = new Primary_Application_Allocation__c( Opportunity__c = opp.id);
        insert paa;
        Test.startTest();
        delete paa;
        Test.stoptest();
        Opportunity updatedOpp = [Select Id, Primary_Application_Allocation_Set__c from Opportunity where id =: opp.id];
        System.assert(!updatedOpp.Primary_Application_Allocation_Set__c);
         
    }
}


and i was facing the errors as:

Error Message  :       System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, Opportunity                                             Currency: invalid currency code: USD - U.S. Dollar: [CurrencyIsoCode]
Stack Trace Class :    TestUpdatePrimaryApllicationAllocation.insertPrimaryApllicationAllocationSet: line 13, column 1


Error Message :                     System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,                                                           AssignAccountTerritorytoOpportunity: execution of BeforeInsert

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

                                    Trigger.AssignAccountTerritorytoOpportunity: line 2, column 1: []

Stack Trace        :                   Class.TestUpdatePrimaryApllicationAllocation.deletePrimaryApllicationAllocationSet: line 27, column 1
rajesh kumar 10rajesh kumar 10
Hi first error i have solved but it was gettin 2 errors agian the first one is

Error Message System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AssignAccountTerritorytoOpportunity: execution of BeforeInsert

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

Trigger.AssignAccountTerritorytoOpportunity: line 2, column 1: []

and the second error remains same
can any pls suggest how to solve this problem. 

thanks 
rajesh
Tad Aalgaard 3Tad Aalgaard 3
I know this post is old but how did you solve the first error?  I am facing the same error on a different object.
 
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Upsert failed. First exception on row 0 with id aAK1b0000008h9zGAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, blng.UsageSummaryTrigger: execution of AfterUpdate

caused by: System.DmlException: Update failed. First exception on row 0 with id aAK1b0000008h9zGAA; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, Currency ISO Code: invalid currency code: U.S. Dollar: [CurrencyIsoCode]

(blng)


: []-566-(blng)



: []