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.