+ Start a Discussion
PProkesPProkes 

Editing Trigger

I need to deactivate the part of the Trigger below that creates an error message when the Opportunity Created Date is greater than the Credit Expiry Date. The other parts of the Trigger can remain. Please help me, as when I've deleted some of the Trigger it didn't calculate the Line of Credit Used etc anymore.

 

trigger oppMasterLeaseAgreementUpdate on Opportunity (after insert, after update, after delete, after undelete) {
    Set<String> RECORDTYPE_NAMES = new Set<String>{'AUS : CFS : Opportunity Record Type', 'AUS : DFS : Opportunity Record Type'};
    
    OppUtilities.isOppTrigger = true;
    
    Opportunity[] records;

    //Get the records
    if (Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate) 
        records = Trigger.new;
    else if (Trigger.isDelete) 
        records = Trigger.old;

    
    Map<String, Id> recordTypeMap = oppUtilities.getOppRecordTypeMap();
    
    Set<Id> recordTypeIds = new Set<Id>();
    
    for (String recType : RECORDTYPE_NAMES) {
        System.debug(recType + '::' + recordTypeMap.get(recType));
        recordTypeIds.add(recordTypeMap.get(recType));
    }

    List<Id> mlaIds = new List<Id>();
    
    Map<Id, Map<Id, Datetime>> mapValidateDate = new Map<Id, Map<Id, Datetime>>();
    
    //Loop through the opportunity records
    for (Opportunity record : records) {        
        if (recordTypeIds.contains(record.RecordTypeId)) {
            if (!(Trigger.isUpdate && Trigger.oldMap.get(record.Id).Amount == record.Amount 
                    && Trigger.oldMap.get(record.Id).Master_Lease_Agreement__c == record.Master_Lease_Agreement__c
                    && Trigger.oldMap.get(record.Id).ForecastCategory == record.ForecastCategory)) {
                
                if (record.Master_Lease_Agreement__c != null) {
                    System.debug('debug1:' + record.Master_Lease_Agreement__c);
                    mlaIds.add(record.Master_Lease_Agreement__c);
                    
                    //we need to store the created date because we are validating whether the it is greater than the MLA Credit_Expiry_Date__c
                    mapValidateDate.put(record.Id, new Map<Id, Datetime>{record.Master_Lease_Agreement__c => record.CreatedDate});
                }
                
                //In case the user changes the Master_Lease_Agreement__c field from one Master_Lease_Agreement__c Id to another, update the old one too
                if (Trigger.isUpdate && Trigger.oldMap.get(record.Id).Master_Lease_Agreement__c != record.Master_Lease_Agreement__c 
                        && Trigger.oldMap.get(record.Id).Master_Lease_Agreement__c != null) {
                    System.debug('debug2:' + Trigger.oldMap.get(record.Id).Master_Lease_Agreement__c);
                    mlaIds.add(Trigger.oldMap.get(record.Id).Master_Lease_Agreement__c);
                }
            }
        }
    }
    
    if (mlaIds.size() > 0) {
        OppUtilities oppUtil = new OppUtilities();
        
        Map<Id, Decimal> totalWonAmountMap = new Map<Id, Decimal>();
        Map<Id, Decimal> totalOpenAmountMap = new Map<Id, Decimal>();
        
        totalWonAmountMap = oppUtil.getTotalOpportunityAmountsByMLAIds(mlaIds, OppUtilities.ForecastType.WON);
        totalOpenAmountMap = oppUtil.getTotalOpportunityAmountsByMLAIds(mlaIds, OppUtilities.ForecastType.OPEN);
            
        Map<Id, Master_Lease_Agreement__c> mapMLAs = new Map<Id, Master_Lease_Agreement__c>([SELECT Id, Name, Credit_Expiry_Date__c FROM Master_Lease_Agreement__c WHERE Id IN :mlaIds]);
            
        //loop through opps and throw add error to records that are linked to Master_Lease_Agreement__c that has Credit_Expiry_Date__c in the past
        for (Id oppId : mapValidateDate.keySet()) {
            Map<Id, Datetime> mapValidateDateInner = mapValidateDate.get(oppId);
            
            Id mlaId;
            
            //This inner map will always have 1 key-value pair in it, the mla Id related to the opp and the opp Created Date
            for (Id id : mapValidateDateInner.keySet()) 
                mlaId = id;
            
            Datetime oppCreatedDate = mapValidateDateInner.get(mlaId);
            
            System.debug('MLA Credit_Expiry_Date__c:' + mapMLAs.get(mlaId).Credit_Expiry_Date__c);
            System.debug('Opp Created Date:' + oppCreatedDate);
            if (Date.newInstance(oppCreatedDate.year(), oppCreatedDate.month(), oppCreatedDate.day()) > mapMLAs.get(mlaId).Credit_Expiry_Date__c)
                Trigger.newMap.get(oppId).addError('This Opportunity with Id=' + oppId + ' cannot be linked with Master Lease Agreement ' + mapMLAs.get(mlaId).Name
                                                        + ' because the Created Date of the Opportunity is past the Credit Expiry Date of the Master Lease Agreement');
        } 
            
        //Update the Master_Lease_Agreement__c records' Line_of_Credit_Used__c and Line_of_Credit_in_Pipeline__c fields
        for (Master_Lease_Agreement__c mla : mapMLAs.values()) {
            mla.Line_of_Credit_Used__c = totalWonAmountMap.get(mla.Id);
            mla.Line_of_Credit_in_Pipeline__c = totalOpenAmountMap.get(mla.Id);
        }
        
        update mapMLAs.values();
    }
    
}


                                       

Shashikant SharmaShashikant Sharma

Try this one I hope will help you, commented the part that compares the dates and shows error message in your code.

 

trigger oppMasterLeaseAgreementUpdate on Opportunity (after insert, after update, after delete, after undelete) {
    Set<String> RECORDTYPE_NAMES = new Set<String>{'AUS : CFS : Opportunity Record Type', 'AUS : DFS : Opportunity Record Type'};
    
    OppUtilities.isOppTrigger = true;
    
    Opportunity[] records;

    //Get the records
    if (Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate) 
        records = Trigger.new;
    else if (Trigger.isDelete) 
        records = Trigger.old;

    
    Map<String, Id> recordTypeMap = oppUtilities.getOppRecordTypeMap();
    
    Set<Id> recordTypeIds = new Set<Id>();
    
    for (String recType : RECORDTYPE_NAMES) {
        System.debug(recType + '::' + recordTypeMap.get(recType));
        recordTypeIds.add(recordTypeMap.get(recType));
    }

    List<Id> mlaIds = new List<Id>();
    
    Map<Id, Map<Id, Datetime>> mapValidateDate = new Map<Id, Map<Id, Datetime>>();
    
    //Loop through the opportunity records
    for (Opportunity record : records) {        
        if (recordTypeIds.contains(record.RecordTypeId)) {
            if (!(Trigger.isUpdate && Trigger.oldMap.get(record.Id).Amount == record.Amount 
                    && Trigger.oldMap.get(record.Id).Master_Lease_Agreement__c == record.Master_Lease_Agreement__c
                    && Trigger.oldMap.get(record.Id).ForecastCategory == record.ForecastCategory)) {
                
                if (record.Master_Lease_Agreement__c != null) {
                    System.debug('debug1:' + record.Master_Lease_Agreement__c);
                    mlaIds.add(record.Master_Lease_Agreement__c);
                    
                    //we need to store the created date because we are validating whether the it is greater than the MLA Credit_Expiry_Date__c
                    mapValidateDate.put(record.Id, new Map<Id, Datetime>{record.Master_Lease_Agreement__c => record.CreatedDate});
                }
                
                //In case the user changes the Master_Lease_Agreement__c field from one Master_Lease_Agreement__c Id to another, update the old one too
                if (Trigger.isUpdate && Trigger.oldMap.get(record.Id).Master_Lease_Agreement__c != record.Master_Lease_Agreement__c 
                        && Trigger.oldMap.get(record.Id).Master_Lease_Agreement__c != null) {
                    System.debug('debug2:' + Trigger.oldMap.get(record.Id).Master_Lease_Agreement__c);
                    mlaIds.add(Trigger.oldMap.get(record.Id).Master_Lease_Agreement__c);
                }
            }
        }
    }
    
    if (mlaIds.size() > 0) {
        OppUtilities oppUtil = new OppUtilities();
        
        Map<Id, Decimal> totalWonAmountMap = new Map<Id, Decimal>();
        Map<Id, Decimal> totalOpenAmountMap = new Map<Id, Decimal>();
        
        totalWonAmountMap = oppUtil.getTotalOpportunityAmountsByMLAIds(mlaIds, OppUtilities.ForecastType.WON);
        totalOpenAmountMap = oppUtil.getTotalOpportunityAmountsByMLAIds(mlaIds, OppUtilities.ForecastType.OPEN);
            
        Map<Id, Master_Lease_Agreement__c> mapMLAs = new Map<Id, Master_Lease_Agreement__c>([SELECT Id, Name, Credit_Expiry_Date__c FROM Master_Lease_Agreement__c WHERE Id IN :mlaIds]);
            
        //loop through opps and throw add error to records that are linked to Master_Lease_Agreement__c that has Credit_Expiry_Date__c in the past
        for (Id oppId : mapValidateDate.keySet()) {
            Map<Id, Datetime> mapValidateDateInner = mapValidateDate.get(oppId);
            
            Id mlaId;
            
            //This inner map will always have 1 key-value pair in it, the mla Id related to the opp and the opp Created Date
            for (Id id : mapValidateDateInner.keySet()) 
                mlaId = id;
            
            Datetime oppCreatedDate = mapValidateDateInner.get(mlaId);
            
            System.debug('MLA Credit_Expiry_Date__c:' + mapMLAs.get(mlaId).Credit_Expiry_Date__c);
            System.debug('Opp Created Date:' + oppCreatedDate);
            /*if (Date.newInstance(oppCreatedDate.year(), oppCreatedDate.month(), oppCreatedDate.day()) > mapMLAs.get(mlaId).Credit_Expiry_Date__c)
                Trigger.newMap.get(oppId).addError('This Opportunity with Id=' + oppId + ' cannot be linked with Master Lease Agreement ' + mapMLAs.get(mlaId).Name
                                                        + ' because the Created Date of the Opportunity is past the Credit Expiry Date of the Master Lease Agreement');*/
        } 
            
        //Update the Master_Lease_Agreement__c records' Line_of_Credit_Used__c and Line_of_Credit_in_Pipeline__c fields
        for (Master_Lease_Agreement__c mla : mapMLAs.values()) {
            mla.Line_of_Credit_Used__c = totalWonAmountMap.get(mla.Id);
            mla.Line_of_Credit_in_Pipeline__c = totalOpenAmountMap.get(mla.Id);
        }
        
        update mapMLAs.values();
    }
    
}