Find your content:

Search form

You are here

Trigger Save Error - Variable Does Not Exist

 
Share

I'm getting an error when saving the following trigger (it's my first). It says that on line 38, the variable gcamount does not exist. This makes me think that the scope of the variables within the if statement is not broad enough. I then tried declaring the variable outside the for loop but then got an error the first time I tried declaring the gcamount variable saying it's a duplicate. So, how can I declare this variable and make it accessible throughout the code where I can both set and alter the value? I've tried using the public keyword but received errors. Any ideas? Thanks.

trigger GiftCertDonation on Opportunity (after insert, after update) {

    //SOQL to return a list of gift certificates whose balance is greater than zero
    list<Journal__c> giftcertlist = [SELECT Gift_Certificate_Code__c, Journal_Gift_Card_Usages__c, CreatedById FROM Journal__c WHERE Journal_Type__c = 'Gift Card' AND Gift_Card_Balance__c > 0];

    //Convert the list into a map
    map<string, string> mymap = new map<string, string>();

    for (Journal__c g :giftcertlist){
        mymap.put(g.Gift_Certificate_Code__c, g.Journal_Gift_Card_Usages__c);
    }

    //declared list of ops to later insert 
    list<Opportunity> lstgcusToInsert = new List<Opportunity>();

    for (Opportunity o : trigger.new){

        //Is the opportunity in question have a gift certificate as one of the payment methods?
        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 !== '**********' ){

            //Does the gift certifiacte code refer to a gift certificate with a 
            if(mymap.contains(o.Gift_Certificate__c)){

                //Depending on the payment method, set the gcamount
                if (o.Pay_Method__c == 'Gift Certificate'){
                    decimal gcamount = o.Amount;
                }
                else if (o.Multiple_Methods_1__c == 'Gift Certificate'){
                    decimal gcamount = o.Multiple_Methods_Amount_1__c ;
                }
                else if (o.Multiple_Methods_2__c == 'Gift Certificate'){
                    decimal 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_Usage__c = mymap.get(o.Gift_Certificate__c) ;
                gcu.Amount_Used__c = gcamount;

                //add to list
                lstgcusToInsert.add(gcu); *****LINE 38 - WHERE THE ERROR EXISTS*****
            }   
        }
    }

    if(lstgcusToInsert.size() > 0) {
        insert lstgcusToInsert;
    }


}

Attribution to: dannymorty

Possible Suggestion/Solution #1

Yes you need to declare / define the variable earlier like this...

           //Depending on the payment method, set the gcamount
            decimal gcamount = 0;
            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 ; 
            }

Attribution to: Andrew Fawcett

Possible Suggestion/Solution #2

Declare gcamount so it is in scope within the if statement.

        //Does the gift certifiacte code refer to a gift certificate with a 
        if(mymap.contains(o.Gift_Certificate__c)) {

            // Declaring gcamount here will make it in-scope within the above if statement.
            // A wider scope risks other iterations of the for loop changing the value.
            // Set a suitable default value here
            decimal gcamount = 0;

            //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_Usage__c = mymap.get(o.Gift_Certificate__c) ;

            // Error was most likely here in the question
            gcu.Amount_Used__c = gcamount;

            //add to list
            lstgcusToInsert.add(gcu);
        } 

Attribution to: Daniel Ballinger

Possible Suggestion/Solution #3

As far as your declaration for gcamount, I think you would be OK putting it right after the for statement. You then need to remove the multiple declarations in the if statements and just have the assignment: gcamount = o.Amount;, etc.

Also, in the first if statement, check the logical operators in your code; they should be doubled up - && and ||.

Here is my modification on your for loop-

for (Opportunity o : trigger.new){

    decimal gcamount;  //consider setting a default value here

    //Is the opportunity in question have a gift certificate as one of the payment methods?
//remember to double up logical operators
    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 !== '**********' ){

        //Does the gift certifiacte code refer to a gift certificate with a 
        if(mymap.contains(o.Gift_Certificate__c)){

            //Depending on the payment method, set the gcamount
//double check the logic here to make sure one of these will always be true
//or add a final else statement to set value, if a default was not set in declaration
            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_Usage__c = mymap.get(o.Gift_Certificate__c) ;
            gcu.Amount_Used__c = gcamount;

            //add to list
            lstgcusToInsert.add(gcu); *****LINE 38 - WHERE THE ERROR EXISTS*****
        }   
    }
}

Hope this helps


Attribution to: zjeh
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4373

My Block Status

My Block Content