You need to sign in to do that
Don't have an account?
rajesh 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
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
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