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.

Chandu
  • 163
  • 3
  • 16
  • 2
    Your question is not clear. What is it that you need assistance with? Are you getting any error? – Rajiv Bhatt Apr 14 '15 at 08: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 "http://localhost:8080/dftly?app_key=app_key, – Chandu Apr 14 '15 at 09:50
  • So whats the issue that you are facing? I see that you are invoking sendRequest() in the trigger. Is that failing? – Rajiv Bhatt Apr 15 '15 at 08:52
  • @RajivBhatt Yes, its not invoking that method sendRequest(). – Chandu Apr 15 '15 at 10:01

1 Answers1

4

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

sfdcfox
  • 489,769
  • 21
  • 458
  • 806