0

Need help to resolve the issue. The trigger doesn't create a new record with updated fields when checkbox on Opp object is true. Also, the new record on custom object should show the Amount resulted due to subtraction on Opp fields.

  public static void createGLTransaction(List<Opportunity> oppList2) {

    List<Id> GLIds = new List<Id>();

    for (Opportunity opp : oppList2) {
        if (opp.Fee_Applied__c == true) {
            GLIds.add(opp.Id);
        }

        Map<Id, GL_Transaction__c> mapGl = new Map<Id, GL_Transaction__c>
                ([SELECT Id, Transaction__c, Amount__c
                  FROM GL_Transaction__c
                  WHERE Id IN : GLIds]);

        List<GL_Transaction__c> glTransactions = new List<GL_Transaction__c>();

        for (Opportunity oppt : oppList2){
            if(oppt.Fee_Applied__c == true && mapGl.containsKey(oppt.Id)){
                GL_Transaction__c gl = mapGl.get(oppt.Id);
                gl.Amount__c = oppt.Amount_Received__c -= oppt.Exchange_Fee__c;
                gl.Transaction__c = 'Phase One Exchange Fee';
                glTransactions.add(gl);
            }
           insert glTransactions;
            update glTransactions;
        }
    }
}

Here is the trigger:

 trigger ExchangeTriggers on Opportunity (before insert, before update, after update) {

if (Trigger.isInsert || Trigger.isUpdate){
    PrimaryEmailOnExchange.createGLTransaction(Trigger.new);
}
1
  • 1
    Your code has multiple issues, including querying the wrong field/ID values, attempting to insert on an existing record, and the map never containing a value to begin with. You may want to consider just using a Flow to do this, as this is a simple enough task to use a Flow with. – sfdcfox May 20 '20 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.