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
Ben Merton 15Ben Merton 15 

Trying to create a trigger that calls a class

This trigger is returning an error saying:  Apex trigger unifize.POContacts caused an unexpected exception, contact your administrator: unifize.POContacts: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Class.unifize.POContactsController.: line 9, column 1
 
trigger POContacts on unifize__Purchase_Order__c (before insert, before update) {

POContactsController newpocs= new POContactsController();

}

This is the class:
 
public class POContactsController{
    string poid;
    string vendorId;
    string contactid;
    

    public POContactsController()
    {
        poId = ApexPages.currentPage().getParameters().get('id');
        vendorId = ApexPages.currentPage().getParameters().get('VendorId');
        
        List<Contact> contactidlist = [select Id FROM Contact WHERE unifize__Purchase_Orders__c=TRUE AND Accountid = :VendorId];
        contactid=contactidlist[0].id;
     
        System.debug('PO ID 1' + poId);
        System.debug('Vendor ID 1' + vendorId);

    }
    public pageReference POContacts()
    {
       //Ensures that there is a Vendor on the PO or returns an error
       
        if (string.IsBlank(vendorid))
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, 'No Vendor selected on the Work Order'));
            return null;
        }
        
        //Deletes any items if they already exist
        
          List<unifize__PO_Contact__c> existingpocontacts = [select ID from unifize__PO_Contact__c where unifize__Purchase_Order__c = :poid];
        if(existingpocontacts.size() != 0)
        {
        delete existingpocontacts;
        }
        
        //Checks that there are Contacts assigned to POs on the related Contacts and returns error if not
        
        List<Contact> Contactlist = [select ID, Email, MobilePhone, Phone, Name FROM Contact where unifize__Purchase_Orders__c=TRUE AND Accountid = :VendorId];
        if (Contactlist.size() == 0)
        {
        ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, 'No Contacts assigned to receive POs for this Vendor'));
        System.debug('Contact List Size' + Contactlist.size());
        return null;
        }
         
        List<unifize__PO_Contact__c> POContactList = new List<unifize__PO_Contact__c>();
        
        for (Contact POContactItem : ContactList)
        {
        
        unifize__PO_Contact__c newpocontact = new unifize__PO_Contact__c();
        newpocontact.unifize__Contact__c=contactid;
     
            POContactList.add(newpocontact);
            
        }
        if (POContactList.size() > 0)
        {
            insert POContactList;
        }
        return new PageReference('/' + poId);
    }
}

Please help!!
Abhishek BansalAbhishek Bansal
Hi Ben,

The class which you are trying to call from your trigger is a controller class for one of your VF page.
In this controller class at line no. 9 you are trying to get the parameter from one of your VF page and in this case there does not exist ant VF page so it is throwing error.

Please create a separate class or use the same code in your trigger which you want to execute.
Let me know if i can help you more on this.

Thanks,
Abhishek Bansal.
Amit Chaudhary 8Amit Chaudhary 8
Issue you are geeting because class which are you calling is Controller class of any other VF page.

Please check below post. I hope that will help you
http://amitsalesforce.blogspot.com/2015/06/trigger-best-practices-sample-trigger.html

Please check above blog and check framework. According to that you can create the Action class for all common method same method you can call from Trigger as well as VF page controller.
 
Ben Merton 15Ben Merton 15
Okay but this hasn't solved the problem, Abhishek.  As you suggested, I used the same code in my trigger:
 
trigger POContacts on unifize__Purchase_Order__c (before insert, before update) {

string poId = ApexPages.currentPage().getParameters().get('id');
string vendorId = ApexPages.currentPage().getParameters().get('VendorId');
List<Contact> contactidlist = [select Id FROM Contact WHERE unifize__Purchase_Orders__c=TRUE AND Accountid = :VendorId];
string contactid=contactidlist[0].id;

POContacts newpocs= new POContacts();


}

and I changed the class:
 
public class POContactsController{
    string poid;
    string vendorId;
    string contactid;
    

    public POContactsController()
    {

        System.debug('PO ID 1' + poId);
        System.debug('Vendor ID 1' + vendorId);

    }
    public pageReference POContacts()
    {
       //Ensures that there is a Vendor on the PO or returns an error
       
        if (string.IsBlank(vendorid))
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, 'No Vendor selected on the Work Order'));
            return null;
        }
        
        //Deletes any items if they already exist
        
          List<unifize__PO_Contact__c> existingpocontacts = [select ID from unifize__PO_Contact__c where unifize__Purchase_Order__c = :poid];
        if(existingpocontacts.size() != 0)
        {
        delete existingpocontacts;
        }
        
        //Checks that there are Contacts assigned to POs on the related Contacts and returns error if not
        
        List<Contact> Contactlist = [select ID, Email, MobilePhone, Phone, Name FROM Contact where unifize__Purchase_Orders__c=TRUE AND Accountid = :VendorId];
        if (Contactlist.size() == 0)
        {
        ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, 'No Contacts assigned to receive POs for this Vendor'));
        System.debug('Contact List Size' + Contactlist.size());
        return null;
        }
         
        List<unifize__PO_Contact__c> POContactList = new List<unifize__PO_Contact__c>();
        
        for (Contact POContactItem : ContactList)
        {
        
        unifize__PO_Contact__c newpocontact = new unifize__PO_Contact__c();
        newpocontact.unifize__Contact__c=contactid;
     
            POContactList.add(newpocontact);
            
        }
        if (POContactList.size() > 0)
        {
            insert POContactList;
        }
        return new PageReference('/' + poId);
    }
}

and I am getting this error:

Apex trigger unifize.POContacts caused an unexpected exception, contact your administrator: unifize.POContacts: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.unifize.POContacts: line 3, column 1
Abhishek BansalAbhishek Bansal
Hi Ben,

Since in your trigger there is no VF page that exists than how will you get the page parameters
ApexPages.currentPage().getParameters().get('id');
This line of code is accessing the VF page which does not exist in trigger.

What i was saying is Just use the code in your trigger that you want to use as per your requirement.
Dont copy and paste all the code instead use the required one.

Thanks,
Abhishek bansal
Ben Merton 15Ben Merton 15
Abhishek - I am sorry I just don't understand this.  Can you be more specific?  I am really not clear by what you mean when you say 'Don't copy and paste all the code instead use the required one'?