0

I got two objects Business_Unit__c and Project_Portfolio__c having Master detail relationship and the junction object is, Business_Unit_Project_Portfolio_Lookup__c.

I want to display an error message if Project_Portfolio__c is associated with a Business_Unit__c(which means if those two object records have a junction object record)on deletion of Project_Portfolio__c record to prevent user to delete associated object records on the Project_Portfolio__c side.

If Project_Portfolio__c doesn't have a junction object record, Project_Portfolio__c should be deleted.

I got the following error when I try to delete a Project_Portfolio__c record which has a association with Business_Unit__c. Also I get the same error when I try to delete a Project_Portfolio__c which does not have a junction record.

Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger ProjectPortfolioTrigger caused an unexpected exception, contact your administrator: ProjectPortfolioTrigger: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: External entry point". 

Click here to return to the previous page.

Any help is appreciated!

Here is my Trigger:-

trigger ProjectPortfolioTrigger on Project_Portfolio__c (before delete) {

    if(trigger.isBefore){
        if(trigger.isDelete){
            ProjectPortfolioTriggerHelper.deleteProjectPortfolios(trigger.new);
        }
    }
}

Here is my Helper class:-

public with sharing class ProjectPortfolioTriggerHelper {

    public static void deleteProjectPortfolios(List<Project_Portfolio__c> newRecords){

            List<Project_Portfolio__c> portfolios = new List<Project_Portfolio__c> ();

            portfolios = [SELECT Id, (SELECT Id FROM Business_Unit_Project_Portfolio_Lookups__r)
                                FROM Project_Portfolio__c
                                WHERE Id =:newRecords];

                for(Project_Portfolio__c portf : portfolios){
                    if(!portf.Business_Unit_Project_Portfolio_Lookups__r.isempty()){
                        portf.Name.addError('You cannot delete the record');

                    }else{
                        delete portfolios;
                    }
                }
        }
    }
1

Do nothing

You need to pass trigger.old from trigger ProjectPortfolioTriggerHelper.deleteProjectPortfolios(trigger.old);

   public with sharing class ProjectPortfolioTriggerHelper {

    public static void deleteProjectPortfolios(List<Project_Portfolio__c> newRecords){

            set<Id> setIds = new set<Id>();
            for(Project_Portfolio__c portf : newRecords){
                setIds.add(portf.Id);
            }
            map<Id, Integer> mapProject_Portfolio = new map<Id, Integer>();
            for(Business_Unit_Project_Portfolio_Lookup__c obj : [SELECT Id,Project_Portfolio__c FROM Business_Unit_Project_Portfolio_Lookup__c WHERE Project_Portfolio__c IN: setIds])
            {
                if(mapProject_Portfolio.containsKey(obj.Project_Portfolio__c))
                    mapProject_Portfolio.put(obj.Project_Portfolio__c, mapProject_Portfolio.get(obj.Project_Portfolio__c)+1);
                else
                    mapProject_Portfolio.put(obj.Project_Portfolio__c, 1);
            }

            for(Project_Portfolio__c portf : newRecords){
                if(mapProject_Portfolio.containsKey(portf.Id)){
                        portf.Name.addError('You cannot delete the record');
                    }
                }
        }
}
  • Removed the line but still i'm getting the same error ! Can't understand the reason – Tinkerbell Dec 7 '15 at 11:35
  • how can I access obj.Project_Portfolio__c in the if statement before the last line (2nd for loop). Because it is outside the first for loop ? – Tinkerbell Dec 7 '15 at 12:02
  • i'm getting an error 'Variable does not exist: obj.Project_Portfolio__c' on line, if(mapProject_Portfolio.containsKey(obj.Project_Portfolio__c)){ – Tinkerbell Dec 7 '15 at 12:08
  • Let us continue this discussion in chat. – Ratan Paul Dec 7 '15 at 12:09
1

In a before delete trigger context, you don't have trigger.new list. Have a look at the context variable documentation.

Pass the list, trigger.old into the helper class and you don't need that else part to forcefully delete the record. Also not sure if it works an sObject list with your query filter. Try to collect Ids and put in the filter

Your Answer

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

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