• Olga Shyliuk
  • NEWBIE
  • 0 Points
  • Member since 2021

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

Hi, everyone!

Here I need to create trigger that support gift cards which allows users to reduce Opportunity price on fixed amount. Opportunity and Gift Card has no relationship. User has write name of gift card in opportunity then its reduce amount opportunity for the amount of gift card.

public with sharing class GiftCardTriggerHandler {
    public void afterUpdate(List<Gift_Card__c> newGiftCards) {
        Set<String> opportunityNames = new Set<String>();
        for (Gift_Card__c giftCardItem : newGiftCards) {
            opportunityNames.add(giftCardItem.Name);
        }
        Map<ID, Opportunity> opportunitiesToUpdate = new Map<ID, Opportunity>([SELECT Id, Gift_Card__c, Amount FROM Opportunity WHERE Gift_Card__c IN: opportunityNames]);
        List<Gift_Card__c> giftCardToInsert = [SELECT Id, Name, Amount__c, Active__c FROM Gift_Card__c WHERE Name IN: opportunityNames];    

        for(String opportunityName : opportunityNames) {
            Decimal Amount = 0;
            Opportunity currentOpportunity = opportunitiesToUpdate.get(opportunityName);
            System.debug('Null' + currentOpportunity.Amount);
            for(Gift_Card__c giftCardItem : giftCardToInsert) {            
                if(giftCardItem.Name == opportunityName) {
                    if(giftCardItem.Active__c == true) {
                        Amount = currentOpportunity.Amount - giftCardItem.Amount__c;
                        giftCardItem.Active__c = false;
                    } 
                    else if(giftCardItem.Active__c == false) {
                           opportunityName.addError('Please, choose active Gift Card!'); 
                    }            
                } 
            }
            if (currentOpportunity.Amount < Amount) {
                currentOpportunity.Amount = Amount;
                opportunitiesToUpdate.put(opportunityName, currentOpportunity);
            }
        } 
        update opportunitiesToUpdate.values();
    }
}

Hello everyone, can you help me?
I need to add Author name wich is located in Author Id object into Book object and after that i need to send email. Both fields are in junction object AuthorToBook. Explain me whats wrong with my code?

public class BatchUpdateBookAuthors implements Database.Batchable<sObject>, Database.Stateful {
    public Integer recordsProcessed = 0;
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT ID, Author__c FROM Book__c WHERE Author__c = null' +
            'SELECT ID, Author__c, Book__c (SELECT Id FROM Author__c) FROM AuthorToBook__c'
        );
    }
    public void execute(Database.BatchableContext bc, List<AuthorToBook__c> scope){
        // process each batch of records
        List<Book__c> books = new List<Book__c>();
        for (AuthorToBook__c authors : scope) {
            for (Book__c book : books) {
                book.Author__c = authors.Author__c;
                books.add(book);
                recordsProcessed = recordsProcessed + 1;
            }
        }
        update books;
    }
    public void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + ' records processed!');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
            JobItemsProcessed,
            TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob
            WHERE Id = :bc.getJobId()];
        
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.toAddresses = new String[] { 'olga.luckina@gmail.com' };
        message.optOutPolicy = 'FILTER';
        message.subject = 'Books';
        message.plainTextBody = 'Books are updated';
        Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
        Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);

        if (results[0].success) {
            System.debug('The email was sent successfully.');
        } else {
            System.debug('The email failed to send: ' + results[0].errors[0].message);
        }
    }
}