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
Vijay Zutshi 2Vijay Zutshi 2 

Apex Trigger not updating as required

The trigger senario is as follows:-

Create “Top X Designation” custom object which is the related list to Opportunity (Look up Relationship). The Top X Designation object, has fields Type (Picklist), Document Attached (Checkbox). The opportunity object has  one field Handoff Attached with pick list type with values are Yes, No. The logic that the trigger has to follow is 
//If Type (Top X Designation) = "Contract Flow Down/Handoff"
// and "Document Attached” = True then "Handoff Attached" = True, otherwise false.

My trigger is as follows. After insert is working fine but after update trigger is no performing as required
trigger topDesignation on Top_X_Designation__c (After insert, After Update, after delete) {
    LIST<Top_X_Designation__c> newTopDesignation = new LIST<Top_X_Designation__c>();
    LIST<Opportunity> oppList = new LIST<Opportunity>();
    LIST<Opportunity> totalOpp = new LIST<Opportunity>();
    SET<ID> newId = new SET<ID>();
       if(Trigger.IsAfter) {
        if(Trigger.IsInsert) {
            for(Top_X_Designation__c top : Trigger.new) {
                newTopDesignation.add(top);
                newId.add(top.OwnerId); 
                system.debug('top' + top.OwnerId);
                system.debug(top.Id);
                Opportunity newOpp = new Opportunity();
                newOpp.OwnerId = top.Id;
                system.debug('opportunity' + newOpp.OwnerId);
                if(top.Type__c == 'Contract Flow Down/Handoff' && top.Document_Attached__c == True) {
                newOpp.Handoff_Attached__c = 'Yes';
                totalOpp.add(newOpp);
                newId.add(newOpp.Id);
                } 
                else {
                newOpp.Handoff_Attached__c = 'No';
                totalOpp.add(newOpp);
                newId.add(newOpp.Id);    
                }
              }
        }
        //system.debug('opp' + totalOpp);
        if(Trigger.IsUpdate) {
            LIST<Top_X_Designation__c> updateTopDesg = new LIST<Top_X_Designation__c>();
            LIST<Opportunity> upOpp = new LIST<Opportunity>();
            SET<ID> topId = new SET<ID>();            
            for(Top_X_Designation__c updateDesg : Trigger.new) {
                Id oldId = Trigger.oldMap.get(updateDesg.Id).OwnerId;
                Id newId = updateDesg.Id;
                system.debug('old' + oldId);
                system.debug('new' + newId);
                if(oldId != newId) {
                    topId.add(updateDesg.Id);
                    updateTopDesg.add(updateDesg);
                }
            }
            system.debug('set' +topId);
            LIST<Opportunity> listOpp = [SELECT OwnerId, Name, Handoff_Attached__c    FROM Opportunity
                                         WHERE Id IN:topId];
            system.debug('opp' + listOpp);
            for(Top_X_Designation__c dc : Trigger.new) {
                for(Opportunity oppUpdate :listOpp) {
                    if(dc.Id  == oppUpdate.Id) {
                        oppUpdate.Handoff_Attached__c = 'Yes';
                        upOpp.add(oppUpdate);
                    }
                        else 
                        {
                            oppUpdate.Handoff_Attached__c = 'No';
                            upOpp.add(oppUpdate);
                        }                    
                    }
                }
            if(upOpp != NULL && upOpp.size()>0) {
                update upOpp;
            }               
        }
 }
}

I am new to this field and would appreciate help to solve my problem.
Thanks
Vijay Zutshi
SwethaSwetha (Salesforce Developers) 
HI Vijay,
Do you see any error message or any information in the debug logs on where the functionality is failing in case of after update?Thanks
Vijay Zutshi 2Vijay Zutshi 2
Hi Swetha, Thanks for replying back. As such there is no error. But my concern is related to system.debug for listOpp. This is the list on opportunities. So when I see the system.debug it is coming blank which means the SOQL query is not picking anything. Please advise. Thanks Vijay Zutshi