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
adrisseladrissel 

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
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);
}

Class
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:
https://cs24.salesforce.com/500/e?retURL=%2F500%2Fo&RecordType=01230000000n2BN&def_account_id=00130000003zgQYAAY&def_contact_id=003a000001mAJxpAAG&ent=Case&origin=Five9
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?

Thanks!!

 
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.

Regards,
Niket