• Anastasiya Komar 8
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

Hi!

I need to add gift cards to an opportunity if the checkbox is enabled, otherwise it will throw an error. In this case, the entered card is deactivated. In the future, I don't have the ability to change any of the opportunity field as the validation fires. How can I solve this problem?
public class OpportunityTriggerHandler {
    
         public void afterInsert (List<Opportunity> newOpportunities){
            List<String> cardNames = new List<String>();
            for(Opportunity oppItem : newOpportunities) {
                cardNames.add(oppItem.Gift_Card__c);
            }
            List<Gift_Card__c> giftCardToUpdate = [SELECT Active__c, Amount__c, Name 
                                                  FROM Gift_Card__c 
                                                  WHERE Name IN :cardNames];    
           
            for(Gift_Card__c giftCard: giftCardToUpdate ) {
                giftCard.Active__c = false;
            }
            
            update giftCardToUpdate;
        } 
        
         
        public void beforeInsert (List<Opportunity> newOpportunities){
            List<String> cardNames = new List<String>();
            for(Opportunity oppItem : newOpportunities) {
                cardNames.add(oppItem.Gift_Card__c);
        }
            List<Gift_Card__c> allGiftCards = [SELECT Active__c, Amount__c, Name 
                                           FROM Gift_Card__c 
                                           WHERE Name IN :cardNames];
            for (Opportunity newOpp: newOpportunities) {
                for(Gift_Card__c giftCard: allGiftCards ) { 
                        if (giftCard.Active__c == true) {
                        newOpp.Amount -= giftCard.Amount__c;
                     } else {
                     newOpp.Gift_Card__c.addError('Gift Card is inactive');
                         }
                    }
                }  
            }
        } 

Hi!

I need to add gift cards to an opportunity if the checkbox is enabled, otherwise it will throw an error. In this case, the entered card is deactivated. In the future, I don't have the ability to change any of the opportunity field as the validation fires. How can I solve this problem?
public class OpportunityTriggerHandler {
    
         public void afterInsert (List<Opportunity> newOpportunities){
            List<String> cardNames = new List<String>();
            for(Opportunity oppItem : newOpportunities) {
                cardNames.add(oppItem.Gift_Card__c);
            }
            List<Gift_Card__c> giftCardToUpdate = [SELECT Active__c, Amount__c, Name 
                                                  FROM Gift_Card__c 
                                                  WHERE Name IN :cardNames];    
           
            for(Gift_Card__c giftCard: giftCardToUpdate ) {
                giftCard.Active__c = false;
            }
            
            update giftCardToUpdate;
        } 
        
         
        public void beforeInsert (List<Opportunity> newOpportunities){
            List<String> cardNames = new List<String>();
            for(Opportunity oppItem : newOpportunities) {
                cardNames.add(oppItem.Gift_Card__c);
        }
            List<Gift_Card__c> allGiftCards = [SELECT Active__c, Amount__c, Name 
                                           FROM Gift_Card__c 
                                           WHERE Name IN :cardNames];
            for (Opportunity newOpp: newOpportunities) {
                for(Gift_Card__c giftCard: allGiftCards ) { 
                        if (giftCard.Active__c == true) {
                        newOpp.Amount -= giftCard.Amount__c;
                     } else {
                     newOpp.Gift_Card__c.addError('Gift Card is inactive');
                         }
                    }
                }  
            }
        }