0

trigger which generate's random string:

trigger setAppKey on dftly_app__c (before insert) {
    if (Trigger.isBefore){
        For (dftly_app__c dac: Trigger.new){
            Blob b = Crypto.GenerateAESKey(128);
            String h = EncodingUtil.ConvertTohex(b);
            String appkey = h.SubString(0,8)+ '-' + h.SubString(8,12) + '-' + h.SubString(12,16) + '-' + h.SubString(16,20) + '-' + h.substring(20);
            system.debug(appkey);
            dac.App_Key__c = appkey;
            App_keygen postappkey = new App_keygen();
             postappkey.sendRequest(dac.App_Key__c,dac.Name);
    }
}
}

And Apex class which try to connect's the External URL.

public with sharing class App_keygen {
     public void sendRequest(String appkey,String appname){
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http(); 
        req.setMethod('POST' ); // Method Type
        req.setEndpoint('https:example.com'); // Server Url
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); // Content Type
        req.setBody('app_key=' + EncodingUtil.urlEncode(appkey, 'UTF-8') + '&app_name=' + EncodingUtil.urlEncode(appname, 'UTF-8')); // Request Parameters
        try {
            res = http.send(req);
            if(res.getBody() != null){
                // Parse Response
            }
        } catch(Exception e) {
            System.debug('error: '+ e);
        }
    } 
}

Can any one please help me how to solve this one.

4
  • 2
    Your question is not clear. What is it that you need assistance with? Are you getting any error? Commented Apr 14, 2015 at 8:31
  • @RajivBhatt thing is on trigger ill generate an random string(app_key) and i need to pass that random string as parameter for the below URL, and the URL should like this "localhost:8080/dftly?app_key=app_key,
    – Chandu
    Commented Apr 14, 2015 at 9:50
  • So whats the issue that you are facing? I see that you are invoking sendRequest() in the trigger. Is that failing? Commented Apr 15, 2015 at 8:52
  • @RajivBhatt Yes, its not invoking that method sendRequest().
    – Chandu
    Commented Apr 15, 2015 at 10:01

1 Answer 1

4

You can't callout during a trigger. Use the Queueable interface, or a @future method, to perform the callout later.

1

Not the answer you're looking for? Browse other questions tagged .