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
Asel Vazir 1Asel Vazir 1 

Create record on related object and update fields on it if Opportunity custom checkbox field is true

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


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

if (Trigger.isInsert || Trigger.isUpdate){
    PrimaryEmailOnExchange.createGLTransaction(Trigger.new);
}

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Asel,

You can certainly use the approach of code but have you considered the OOB solutions for your implementation??

Regards,
Anutej