0

This is my Apex Trigger

trigger OpportunityUpdateTrigger on Opportunity (after Update,After Delete,after Undelete,before Update) 
{     
    if((Trigger.isAfter) && (Trigger.isDelete))
   {
       system.debug('inside is delete');
       OpportunityUpdateTriggerHandler.getTotalDealFee(trigger.old);
       system.debug('trigger.old *****' +trigger.old);
   }   
    if((Trigger.isBefore) && (Trigger.isUpdate))
    {
        if(!Trigger.new.isEmpty()){
            OpportunityUpdateTriggerHandler.SaveAndNextPrevention();
        }   
    }
}

This is my Apex Class

public without sharing class OpportunityUpdateTriggerHandler 
{
public static void SaveAndNextPrevention(){
        Map<String,String> rejectedMap = new Map<String,String>();
        Map<String,String> missedMap = new Map<String,String>();
        //List<Id> oppIds = new List<Id>();
        system.debug('In CloseActivityCopy trigger');
        List<Opportunity> verOpps = [SELECT Id, Name,stageName,Loan_Number__c, (SELECT id, Name, Document_Name__c, docType__c, Document_Master__c,Document_Master__r.Stages__c,
                                                                                Document_Master__r.Document_Name__c, Document_Master__r.DocumentType__c, Reviewed__c, Approved__c,
                                                                                AttachmentId__c, Comment__c FROM Verification_Documents__r WHERE Opportunity__r.Id IN :Trigger.new) 
                                     FROM Opportunity WHERE Id IN :Trigger.new ];
        /*for(Opportunity opp : verOpps ){
            oppIds.add(opp.Id); 
        }*/
        system.debug('verOpps'+verOpps);

        system.debug('verOpps::: '+ verOpps);
        User LoggedinUsr= [SELECT Id,Name,Partner_User_Type__c FROM User WHERE id=:UserInfo.getUserID()];
        Map<id,Integer> oppVdMap = new Map<id,Integer>();
        List<TypeWiseDocument__c> fileListMandate = new List<TypeWiseDocument__c>();
        List<String> fileList = new List<String>();
        for(TypeWiseDocument__c documentDesc: [Select Document_Name__c, Stages__c, User__c, DocumentType__c 
                                               FROM TypeWiseDocument__c 
                                               WHERE Mandatory__c=true AND DocumentType__c = 'Predefined'])
        {
            fileListMandate.add(documentDesc);
        }
        for (Opportunity o : verOpps) 
        {
            System.debug('opp id for record type is ====  '+o.Id);
            String opporId = o.Id;
            Map<String,Verification_Documents__c> MapAttchmentByType=new Map<String,Verification_Documents__c>();
            for(Verification_Documents__c attch : o.Verification_Documents__r)
            {
                system.debug('attch.Document_Master__r.Stages__c****'+attch.Document_Master__r.Stages__c+'o.StageName***'+o.StageName);
                MapAttchmentByType.put(attch.docType__c, attch);
                if(attch.Document_Master__r.Stages__c == o.StageName){
                    if(attch.Reviewed__c){
                        system.debug('attch reviewed****'+attch);
                        if(!attch.Approved__c){
                            rejectedMap.put(o.id,attch.docType__c);
                            system.debug('rejectedMap ' + rejectedMap);
                            break;
                        }
                    }else{
                        rejectedMap.put(o.id,attch.docType__c);
                        system.debug('rejectedMap ' + rejectedMap);
                        break;
                    }
                }
            }
            //system.debug('rejectedMap****'+rejectedMap);
            for(TypeWiseDocument__c t : fileListMandate)
            {
                if(o.StageName == t.Stages__c)
                {
                    fileList.add(t.Document_Name__c);
                }

            }
            Boolean flag=false;
            system.debug('fileList:: -->'+ fileList);
            system.debug('MapAttchmentByType:: -->'+ MapAttchmentByType);
            for(String docuType:fileList){
                if(MapAttchmentByType.get(docuType)==null){
                    oppVdMap.put(o.Id, 0);    
                    break;
                }
            }
        }
        for(Opportunity oppt : Trigger.new)
        {
            **if((oppt.StageName == 'Verification'  && Trigger.oldMap.get(oppt.Id).StageName!= 'Verification') ||
               (oppt.StageName == 'Construction'  && Trigger.oldMap.get(oppt.Id).StageName!= 'Construction' ) ||
               (oppt.StageName == 'Project Completion'  && Trigger.oldMap.get(oppt.Id).StageName!= 'Project Completion' ) ||
               (oppt.StageName == 'In Service'  && Trigger.oldMap.get(oppt.Id).StageName!= 'In Service' ) ||
               (oppt.StageName == 'Closed Won'  && Trigger.oldMap.get(oppt.Id).StageName!= 'Closed Won' )
              )**
            {
                if(oppVdMap.containskey(oppt.id)){
                    oppt.addError('Please attach all the required documents before moving to next stage');
                }
                if(rejectedMap.containskey(oppt.id)){
                    oppt.addError('Please check if all the mandatory documents uploaded by Customer/Contractor  are approved.');
                }
            }
        }
    }
}
3
  • 2
    Can you highlight in your code, where exactly are you receiving this error?
    – Jayant Das
    Mar 29, 2019 at 18:05
  • also, remove WHERE Opportunity__r.Id IN :Trigger.new from your sub query. It is not needed at all. Mar 29, 2019 at 18:09
  • @JayantDas Highlighted code
    – mahesh
    Mar 29, 2019 at 18:14

1 Answer 1

5

Trigger.new is a type of List<sObject> outside of a trigger context. You need to "cast" it back in to a List<Opportunity> for it to work:

for(Opportunity oppt : (List<Opportunity>)Trigger.new)

In addition, Trigger.oldMap is also a Map<Id, sObject> outside of a trigger context. You would have to cast the sobject in to an opportunity as well:

((Opportunity)Trigger.oldMap.get(oppt.Id)).StageName

For this reason, trigger handlers are typically written to pass in the correct type to avoid casting:

public static void SaveAndNextPrevention(Opportunity[] newRecs, Map<Id, Opportunity> oldRecs) {

...

OpportunityUpdateTriggerHandler.SaveAndNextPrevention(Trigger.new, Trigger.oldMap);
1
  • Thanks @sfdcfox
    – mahesh
    Mar 29, 2019 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.