Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have this error that says "System.AsyncException: No enclosing class for method: methodName".

The code structure :

trigger trgSendRecords on DataSetup__c (after insert) {

    /* SOME BUSINESS LOGIC
    */
    sendRequest('hello' , 'world');

    @future
    public static void sendRequest(string A , string B){

     /* HTTP CALL OUT TO A REST SERVICE
     */

    }

}

When the record is inserted on the custom object I always get an error :

System.AsyncException: No enclosing class for method: sendRequest

I have tried to put this method in a GlobalClass and then call it in the trigger but the error still remains the same.

Request you guys to shed some light on the cause and remedy for this.

Thanks!!

share|improve this question

1 Answer 1

up vote 4 down vote accepted

It is unfortunate that the trigger compiler allows methods to be declared in triggers. We asked in a case about this some time ago and were told that this is undocumented and unsupported.

The normal pattern for what you are doing is this trigger:

trigger trgSendRecords on DataSetup__c (after insert) {
    Sender.sendRequest('hello' , 'world');
}

and this class:

public class Sender {
    @future
    public static void sendRequest(string A , string B) {
        // ...
    }
}

where the static method invoked is qualified by its class name in the trigger. Did you try exactly this pattern?

share|improve this answer
    
Yes I did try this which seems to work just fine. What is strange is that it seemed to work for the first execution without any errors and then subsequently started throwing errors (or may be I'm fatigued). So apparently this is not supported, am I right? –  Shaikh Mohd Nadeem Jun 17 at 12:03
    
If you mean adding methods in triggers, then I can't say what will happen and making the method @future adds further uncertainly. Bottom line is to use the documented and supported approach. –  Keith C Jun 17 at 12:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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