function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anastasiya Komar 8Anastasiya Komar 8 

HELP PLEASE! Apex and Validate

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');
                         }
                    }
                }  
            }
        } 

Best Answer chosen by Anastasiya Komar 8
Anastasiya Komar 8Anastasiya Komar 8
Thanks a lot! But I have a validation error not working when I select an inactive card...

All Answers

Maharajan CMaharajan C
Hi Anastasiya,

can you please elaborate your concern little bit more so we can help you much easily... Because am seeing you have already written some code...

Thanks,
Maharajan.C
Anastasiya Komar 8Anastasiya Komar 8
Trying to change any field of an opportunity triggers an error.  I need to select an active gift card and uncheck the active one, but if the gift card is not active anyway, I can't add it to the opportunity.  But after the card has become inactive, I should be able to change any opportunity field except the gift card
Maharajan CMaharajan C
Hi,

Please use the below code which is bulkified. If there is any syntax issue please fix it.
 
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 and Active__c = true];    
        
        for(Gift_Card__c giftCard: giftCardToUpdate ) {
            giftCard.Active__c = false;
        }
        
        update giftCardToUpdate;
    } 
    
    
    public void beforeInsert (List<Opportunity> newOpportunities){
        List<String> cardNames = new List<String>();
        Map<string,decimal> giftCardMap = new Map<string,decimal>();
        for(Opportunity oppItem : newOpportunities) {
            cardNames.add(oppItem.Gift_Card__c);
        }
        for( Gift_Card__c gc : [SELECT Active__c, Amount__c, Name FROM Gift_Card__c WHERE Name IN :cardNames and Active__c = true]){
            if(giftCardMap.containsKey(gc.name)){
                decimal amt = giftCardMap.get(gc.name) + gc.Amount__c;
                giftCardMap.put(gc.name,amt);
            }
            else{
                giftCardMap.put(gc.name, gc.Amount__c);
            }
        }
        for (Opportunity newOpp: newOpportunities) {
            
            if(giftCardMap.containsKey(newOpp.Gift_Card__c)){
                newOpp.Amount -= giftCardMap.get(gc.name);
            }
            else{
                newOpp.Gift_Card__c.addError('Gift Card is inactive');
            }
        }  
    }
}

1. Check did you have any validation rule or trigger whihc is blocking your opportunity edit because the above code will fire only in opportunity insert. ( I assumed this trigger will fires only on opp insert based on your naming conditions).

2. To lock the Gift_Card__c field use validation rule.

Please let me know if you need any help...

Thanks,
Maharajan.C
Anastasiya Komar 8Anastasiya Komar 8
Thanks a lot! But I have a validation error not working when I select an inactive card...
This was selected as the best answer
James MaryJames Mary
Wilko Customer Experience Survey is an online poll created by Wilko to determine their customer’s satisfaction.

Wilko Survey tends to get honest feedback, reviews, and suggestions from their recent customers and implements them to improve its services.

https://takesurveyz.onl/yourwinningthoughts-com/