You need to sign in to do that
Don't have an account?
Ben 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
This is the class:
Please help!!
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!!
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.
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.
and I changed the class:
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
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