+ Start a Discussion

Get URL Parameters in Apex Trigger

Hey all.  I am trying to write a custom trigger which requires information I have placed as parameters in the referring URL.  Here is the code:

trigger AddActivity on Case (before insert) {

    List<Case> cases = Trigger.new;
    Case c = cases[0];    
    AddActivity aa = new AddActivity();
    Map<String,Id> urlInfo = aa.getUrlInfo();
    system.debug('-------------------> urlInfo: '+urlInfo);

public class AddActivity{

    public Map<String,Id> getUrlInfo(){
        String origin = ApexPages.currentPage().getParameters().get('origin');
        Id accId = ApexPages.currentPage().getParameters().get('def_account_id');
        Map<String,Id> urlInfo = new Map<String,Id>{    
            'origin' => origin,
            'accId' => accId
        return urlInfo;

The result when saving a Case record is "Error: Invalid Data. Review all error messages below to correct your data. Apex trigger AddActivity caused an unexpected exception, contact your administrator: AddActivity: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.AddActivity.getUrlInfo: line 5, column 1".

With every debug I do and attempt to pull URL parameters using URL methods, Site methods, whatever...none of them produce what I need.  Here is an example of a URL I have:
I need to be able to pull the "query" portion of it and parse the data from there (everything after the ?).

Is there no way to do this with a Trigger?


Mathew Andresen 5Mathew Andresen 5
I'm not an expert by any means, but I don't think this will work.  Triggers can come from anywhere including bulk operations such as data uploading.  So I don't believe they pass in URL info, just object info.

So you would need to figure out another way to get that info.  For example, you could have a lookup field on that triggering object that pointed to the other object.  
Niket SFNiket SF
Hell Adrissel,

 You can not any VF page or any URL in trigger execution. you have to store this data or need to identify how you can create that URL string and may be then you can use it.