Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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();


    }
}
share|improve this question

1 Answer 1

up vote 2 down vote accepted

I am not too sure but the reason why your code is throwing the error could be: 1) jgcuMap is empty Why is it empty? Because when u say in your test class

        opp2.Gift_Certificate__c = '6127225559';
        opp1.Gift_Certificate__c = '6127225559';
        opp3.Gift_Certificate__c = '6127225559';

I see that you are adding in your code

jgcuMap.put(g.Gift_Card__r.Gift_Certificate_Code__c, g.id);

In you test class you are not creating a gift_card__c record : before creating the opps do

Journal_Gift_Card_Usages__c gift = new Journal_Gift_Card_Usages__c();
gift.allrequiredfields;
gift.Gift_Certificate_Code__c = '6127225559';
insert gift;

and then

    opp2.Gift_Certificate__c = gift.Gift_Certificate_Code__c;
    opp1.Gift_Certificate__c = gift.Gift_Certificate_Code__c;
    opp3.Gift_Certificate__c = gift.Gift_Certificate_Code__c;
share|improve this answer
1  
Thanks, rao! I'll give that a try and let you know how it works out. –  dannymorty Jan 15 '13 at 22:34
    
@dannymorty any luck, were u able to get the error resolved? –  rao Jan 16 '13 at 3:34
    
No luck yet. The issue is that in order a new Journal_Gift_Card_Usages__c record, I actually have to create a Journal__c record which kicks off a trigger that i turn creates a Journal_Gift_Card_Usages__c. When inserting that Journal__c record, I receive an error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Journal: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Class.Journal.afterInsert: line 61, column 1 Trigger.Journal: line 5, column 1: [] –  dannymorty Jan 16 '13 at 14:24
    
I've followed the error back to line 61 on the appropriate trigger but can't figure out how it's related to my code. I'm digging in some more. –  dannymorty Jan 16 '13 at 14:27
    
I think its more a cascading effect!!! There must be some kind of a list that must be empty when the Journal__c is being created. LMK if i can be of any help here :) –  rao Jan 16 '13 at 18:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.