I'm running tests on a trigger I've created and can't figure out why the following error is being produced.
Error: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Not a valid gift certificate. Either gift certificate with that code does not exist or the balance is $0: []
To explain this error a little more, the text "Not a valid gift certificate..." above is an intentional error I created in the trigger (code below) to prevent users from creating opportunities with the codes of gift certificates that are not valid or don't have a balance.
All of the records that I'm inserting in the test class, however, reference a gift certificate code of a valid gift certificate with ample funds. I'm trying to figure out why this piece of code is being called and I'm stuck. Any help would be appreciated.
Trigger
trigger GiftCertDonation on Opportunity (after insert, after update) {
//If this is an update, check to see if there's a gift certificate that was already debited.
set<id> doNotProcess = new set<id>();
if (trigger.isUpdate){
for(Opportunity oldDonation : trigger.old) {
if (oldDonation.Gift_Certificate__c != null){
doNotProcess.add(oldDonation.id);
}
}
}
//SOQL to return a list of gift certificates whose balance is greater than zero
list<Journal_Gift_Card_Usages__c> giftcertlist = [SELECT id, Gift_Card__r.id, Gift_Card__r.Gift_Certificate_Code__c, Balance__c FROM Journal_Gift_Card_Usages__c WHERE Balance__c > 0];
//Convert the list into two maps - one with the gc code and jgcu id, the other with gc code and gc balance
map<string, id> jgcuMap = new map<string, id>();
map<string, decimal> balanceMap = new map<string, decimal>();
for (Journal_Gift_Card_Usages__c g :giftcertlist){
jgcuMap.put(g.Gift_Card__r.Gift_Certificate_Code__c, g.id);
balanceMap.put(g.Gift_Card__r.Gift_Certificate_Code__c, g.Balance__c);
}
//declared list of Gift Cart Usages to later insert
list<Gift_Card_Usage__c> lstgcusToInsert = new List<Gift_Card_Usage__c>();
//declared list of Opportunities to later insert
list<Opportunity> oppsToUpdate = new List<Opportunity>();
//declare the gcamount variable to be assigned later
decimal gcamount;
for (Opportunity o : trigger.new){
//Has this opportunity already created a gift card usage?
if (doNotProcess.contains(o.id)){
return;
}
//Determine if the opportunity in question has a gift certificate as one of the payment methods?
else if (((o.Pay_Method__c == 'Gift Certificate') || (o.Pay_Method__c == 'Multiple Methods' && (o.Multiple_Methods_1__c == 'Gift Certificate' || o.Multiple_Methods_2__c == 'Gift Certificate' ))) && o.createdById != '005G0000001cbVV' ){
//Does the gift certifiacte code refer to a gift certificate with a
if (jgcuMap.containsKey(o.Gift_Certificate__c)){
//Depending on the payment method, set the gcamount
if (o.Pay_Method__c == 'Gift Certificate'){
gcamount = o.Amount ;
}
else if (o.Multiple_Methods_1__c == 'Gift Certificate'){
gcamount = o.Multiple_Methods_Amount_1__c ;
}
else if (o.Multiple_Methods_2__c == 'Gift Certificate'){
gcamount = o.Multiple_Methods_Amount_2__c ;
}
//Create New Gift Card Usage
Gift_Card_Usage__c gcu = new Gift_Card_Usage__c();
gcu.Journal_Gift_Card_Usages__c = jgcuMap.get(o.Gift_Certificate__c) ;
if ((balanceMap.get(o.Gift_Certificate__c) - gcamount) < 0){
gcu.Amount_Used__c = balanceMap.get(o.Gift_Certificate__c);
}
else {
gcu.Amount_Used__c = gcamount;
}
//add to list
lstgcusToInsert.add(gcu);
}
else {
o.addError(' Not a valid gift certificate. Either gift certificate with that code does not exist or the balance is $0');
}
}
}
if(lstgcusToInsert.size() > 0) {
insert lstgcusToInsert;
}
if(oppsToUpdate.size() > 0){
update oppsToUpdate;
}
}
TestClass
@isTest
private class GiftCertTriggerTest {
static testMethod void MyUnitTest(){
//Gift Cert as pay method
Opportunity opp1 = new Opportunity();
opp1.name = 'Test 1';
opp1.RecordType = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' and Name = 'Single Donation'];
opp1.Type = 'Individual Gift';
opp1.Amount = 10;
opp1.CloseDate = system.today();
opp1.StageName = 'Received';
opp1.Pay_Method__c = 'Gift Certificate';
opp1.Gift_Certificate__c = '6127225559';
//Gift Cert as Multiple Method 1
Opportunity opp2 = new Opportunity();
opp2.name = 'Test 2';
opp2.RecordType = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' and Name = 'Single Donation'];
opp2.Type = 'Individual Gift';
opp2.Amount = 15;
opp2.CloseDate = system.today();
opp2.StageName = 'Received';
opp2.Pay_Method__c = 'Multiple Methods';
opp2.Multiple_Methods_Amount_1__c = 10;
opp2.Multiple_Methods_1__c = 'Gift Certificate';
opp2.Multiple_Methods_Amount_2__c = 5;
opp2.Multiple_Methods_2__c = 'Cash';
opp2.Gift_Certificate__c = '6127225559';
//Gift Cert as Multiple Method 2
Opportunity opp3 = new Opportunity();
opp3.name = 'Test 3';
opp3.RecordType = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' and Name = 'Single Donation'];
opp3.Type = 'Individual Gift';
opp3.Amount = 15;
opp3.CloseDate = system.today();
opp3.StageName = 'Received';
opp3.Pay_Method__c = 'Multiple Methods';
opp3.Multiple_Methods_Amount_1__c = 5;
opp3.Multiple_Methods_1__c = 'Cash';
opp3.Multiple_Methods_Amount_2__c = 10;
opp3.Multiple_Methods_2__c = 'Gift Certificate';
opp3.Gift_Certificate__c = '6127225559';
//Opportunity with Gift Cert that has already been debited.
list<Opportunity> oppsToInsert = new list<Opportunity>();
oppsToInsert.add(opp1);
oppsToInsert.add(opp2);
oppsToInsert.add(opp3);
//Testing
Test.startTest();
insert oppsToInsert;
Test.stopTest();
}
}
