You need to sign in to do that
Don't have an account?
Poorna Developer
Future method cannot call from future method or batch class
Helo Everyone,
I have written an apex code to get order object data from process builder by using invocable method.
And also I had performed some task like create json data and pass that json data in to webservice to get server resposnse.
Here my code,
public with sharing class OrderActivation
{
@InvocableMethod(label='getorderid' description='Get order record id from order')
public static void getdata(getcustomerid[] requests)
{
String recordId,bCity,bStreet,bState,bZip,bCountry,sCity,sStreet,sState,sZip,sCountry,orderAmount,cCity,cStreet,cState,cZip,cCountry,cName,cEmail;
string jsonString;
for(getcustomerid request : requests)
{
recordId= request.recId;
System.debug('order Id::::: '+recordId);
RootObject objJSONRoot = new RootObject();
Merchant merch = new Merchant();
merch.signature = '73242ibc7osfsdfb5|tuJEH3gNbeWJsdfsdffIHah||nbobljsdhgfjsdhjbnmdli0poys|doU3HJVosdfsfwefym7MQ44qf7cpn7pc';
merch.tariff = 10423004;
Customer cus = new Customer();
cus.first_name=request.cName;
cus.last_name=request.cName;
cus.city=request.cCity;
cus.sState=request.cState;
cus.country_code=request.cCountry;
cus.zip=request.cZip;
cus.email = request.cEmail;
cus.customer_ip = '182.156.208.74';
cus.customer_no = '9265';
cus.tel ='+49 089 123456';
cus.mobile = '+49 174 7781423';
cus.house_no='2';
Transactions tran = new Transactions();
tran.payment_type ='INVOICE';
tran.amount = '100';
tran.currencyX= 'EUR';
tran.test_mode='1';
tran.order_no = '12';
Custom cu = new Custom();
cu.lang='EN';
objJSONRoot.merchant = merch;
objJSONRoot.customer = cus;
objJSONRoot.transactions = tran;
objJSONRoot.cust = cu;
jsonString = JSON.Serialize(objJSONRoot);
System.debug('jsonString::: ' +jsonString);
}
OrderActivation.OrderGet(recordId,jsonString);
}
@future(callout=true)
public static void OrderGet(string recordId, string jsonString)
{
String BodyData = jsonString;
String OrderId = recordId;
System.debug(OrderId);
System.debug('Json Body::::'+BodyData);
try
{
Order od = new Order(id=OrderId);
String endpointurl = 'https://sample.webserver';
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(endpointurl);
//req.setBody(sBody);
req.setMethod('POST');
req.setHeader('Accept','application/json');
req.setHeader('Content-Type', 'application/json');
req.setHeader('charset','utf-8');
HttpResponse responseg = http.send(req);
String response = responseg.getBody();
System.debug(responseg.getBody());
System.debug(responseg);
System.debug(response);
od.Transaction_Data__c = response;
update od;
}
catch(Exception e){
System.debug(LoggingLevel.ERROR, e.getMessage());
}
}
public static string generateJSON(String jsonString, String request)
{
String first_name = 'Max';
String last_name ='cName';
//string email ='test@com';
string customer_ip = '182.156.208.74';
string customer_no = '9265';
string tel ='+49 089 123456';
string mobile = '+49 174 7781423';
string payment_type = 'INVOICE';
string amount = '1000';
string currencyX = 'EUR';
string test_mode = '1';
string due_date = '2021-08-05';
string invoice_ref = 'BNR-14-8410121284';
string order_no = '84112121084';
string lang ='EN';
String city = 'San Francisco';
String sState = 'CA';
String zip = '946556105';
String country_code = 'US';
RootObject objJSONRoot = new RootObject();
String jsonData = JSON.Serialize(objJSONRoot);
// jsonData = jsonData.replace('"currency":', '"currencyX":');
// System.debug(jsonData);
return jsonData;
}
public class RootObject
{
public Merchant merchant { get; set;}
public Customer customer { get; set;}
public Transactions transactions {get;set;}
public Custom cust {get;set;}
}
public class Merchant
{
public string signature { get; set; }
public integer tariff { get; set; }
}
public class Customer
{
public string first_name { get; set; }
public string last_name { get; set; }
public string email {get;set;}
public string house_no {get;set;}
public string street {get;set;}
public string city {get;set;}
public string zip {get;set;}
public string sState {get;set;}
public string country_code {get;set;}
public string customer_ip{get;set;}
public string customer_no{get;set;}
public string tel{get;set;}
public string mobile{get;set;}
}
public class Transactions
{
public string payment_type {get;set;}
public string amount {get;set;}
public string currencyX {get;set;}
public string test_mode {get;set;}
public string due_date {get;set;}
public string invoice_ref {get;set;}
public string order_no {get;set;}
}
public class Custom
{
public string lang {get;set;}
}
public class getcustomerid
{
//request billing address related invocable variables and make variables as required one
@InvocableVariable(required=true)
public String recId;//Get Order record id
@InvocableVariable(required=true)
public String bStreet;
@InvocableVariable(required=true)
public String bState;
@InvocableVariable(required=true)
public String bCity;
@InvocableVariable(required=true)
public String bZip;
@InvocableVariable(required=true)
public String bCountry;
//request shipping address related invocable varibales and makes variables a required one
@InvocableVariable(required=true)
public String sStreet;
@InvocableVariable(required=true)
public String sState;
@InvocableVariable(required=true)
public String sCity;
@InvocableVariable(required=true)
public String sZip;
@InvocableVariable(required=true)
public String sCountry;
//request order amount related invocable varibales and makes variables a required one
@InvocableVariable(required=true)
public String orderAmount;
// cCity,cStreet,cState,cZip,cCountry,cName;
@InvocableVariable(required=true)
public String cCity;
@InvocableVariable(required=true)
public String cStreet;
@InvocableVariable(required=true)
public String cState;
@InvocableVariable(required=true)
public String cZip;
@InvocableVariable(required=true)
public String cCountry;
@InvocableVariable(required=true)
public String cName;
@InvocableVariable(required=true)
public String cEmail;
}
}
I'm getting error like Future methode cannot call from future method or batch class.
Any help!!
Thanks in advance.
I have written an apex code to get order object data from process builder by using invocable method.
And also I had performed some task like create json data and pass that json data in to webservice to get server resposnse.
Here my code,
public with sharing class OrderActivation
{
@InvocableMethod(label='getorderid' description='Get order record id from order')
public static void getdata(getcustomerid[] requests)
{
String recordId,bCity,bStreet,bState,bZip,bCountry,sCity,sStreet,sState,sZip,sCountry,orderAmount,cCity,cStreet,cState,cZip,cCountry,cName,cEmail;
string jsonString;
for(getcustomerid request : requests)
{
recordId= request.recId;
System.debug('order Id::::: '+recordId);
RootObject objJSONRoot = new RootObject();
Merchant merch = new Merchant();
merch.signature = '73242ibc7osfsdfb5|tuJEH3gNbeWJsdfsdffIHah||nbobljsdhgfjsdhjbnmdli0poys|doU3HJVosdfsfwefym7MQ44qf7cpn7pc';
merch.tariff = 10423004;
Customer cus = new Customer();
cus.first_name=request.cName;
cus.last_name=request.cName;
cus.city=request.cCity;
cus.sState=request.cState;
cus.country_code=request.cCountry;
cus.zip=request.cZip;
cus.email = request.cEmail;
cus.customer_ip = '182.156.208.74';
cus.customer_no = '9265';
cus.tel ='+49 089 123456';
cus.mobile = '+49 174 7781423';
cus.house_no='2';
Transactions tran = new Transactions();
tran.payment_type ='INVOICE';
tran.amount = '100';
tran.currencyX= 'EUR';
tran.test_mode='1';
tran.order_no = '12';
Custom cu = new Custom();
cu.lang='EN';
objJSONRoot.merchant = merch;
objJSONRoot.customer = cus;
objJSONRoot.transactions = tran;
objJSONRoot.cust = cu;
jsonString = JSON.Serialize(objJSONRoot);
System.debug('jsonString::: ' +jsonString);
}
OrderActivation.OrderGet(recordId,jsonString);
}
@future(callout=true)
public static void OrderGet(string recordId, string jsonString)
{
String BodyData = jsonString;
String OrderId = recordId;
System.debug(OrderId);
System.debug('Json Body::::'+BodyData);
try
{
Order od = new Order(id=OrderId);
String endpointurl = 'https://sample.webserver';
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(endpointurl);
//req.setBody(sBody);
req.setMethod('POST');
req.setHeader('Accept','application/json');
req.setHeader('Content-Type', 'application/json');
req.setHeader('charset','utf-8');
HttpResponse responseg = http.send(req);
String response = responseg.getBody();
System.debug(responseg.getBody());
System.debug(responseg);
System.debug(response);
od.Transaction_Data__c = response;
update od;
}
catch(Exception e){
System.debug(LoggingLevel.ERROR, e.getMessage());
}
}
public static string generateJSON(String jsonString, String request)
{
String first_name = 'Max';
String last_name ='cName';
//string email ='test@com';
string customer_ip = '182.156.208.74';
string customer_no = '9265';
string tel ='+49 089 123456';
string mobile = '+49 174 7781423';
string payment_type = 'INVOICE';
string amount = '1000';
string currencyX = 'EUR';
string test_mode = '1';
string due_date = '2021-08-05';
string invoice_ref = 'BNR-14-8410121284';
string order_no = '84112121084';
string lang ='EN';
String city = 'San Francisco';
String sState = 'CA';
String zip = '946556105';
String country_code = 'US';
RootObject objJSONRoot = new RootObject();
String jsonData = JSON.Serialize(objJSONRoot);
// jsonData = jsonData.replace('"currency":', '"currencyX":');
// System.debug(jsonData);
return jsonData;
}
public class RootObject
{
public Merchant merchant { get; set;}
public Customer customer { get; set;}
public Transactions transactions {get;set;}
public Custom cust {get;set;}
}
public class Merchant
{
public string signature { get; set; }
public integer tariff { get; set; }
}
public class Customer
{
public string first_name { get; set; }
public string last_name { get; set; }
public string email {get;set;}
public string house_no {get;set;}
public string street {get;set;}
public string city {get;set;}
public string zip {get;set;}
public string sState {get;set;}
public string country_code {get;set;}
public string customer_ip{get;set;}
public string customer_no{get;set;}
public string tel{get;set;}
public string mobile{get;set;}
}
public class Transactions
{
public string payment_type {get;set;}
public string amount {get;set;}
public string currencyX {get;set;}
public string test_mode {get;set;}
public string due_date {get;set;}
public string invoice_ref {get;set;}
public string order_no {get;set;}
}
public class Custom
{
public string lang {get;set;}
}
public class getcustomerid
{
//request billing address related invocable variables and make variables as required one
@InvocableVariable(required=true)
public String recId;//Get Order record id
@InvocableVariable(required=true)
public String bStreet;
@InvocableVariable(required=true)
public String bState;
@InvocableVariable(required=true)
public String bCity;
@InvocableVariable(required=true)
public String bZip;
@InvocableVariable(required=true)
public String bCountry;
//request shipping address related invocable varibales and makes variables a required one
@InvocableVariable(required=true)
public String sStreet;
@InvocableVariable(required=true)
public String sState;
@InvocableVariable(required=true)
public String sCity;
@InvocableVariable(required=true)
public String sZip;
@InvocableVariable(required=true)
public String sCountry;
//request order amount related invocable varibales and makes variables a required one
@InvocableVariable(required=true)
public String orderAmount;
// cCity,cStreet,cState,cZip,cCountry,cName;
@InvocableVariable(required=true)
public String cCity;
@InvocableVariable(required=true)
public String cStreet;
@InvocableVariable(required=true)
public String cState;
@InvocableVariable(required=true)
public String cZip;
@InvocableVariable(required=true)
public String cCountry;
@InvocableVariable(required=true)
public String cName;
@InvocableVariable(required=true)
public String cEmail;
}
}
I'm getting error like Future methode cannot call from future method or batch class.
Any help!!
Thanks in advance.
Do you have a trigger on SObject Order that may be triggering this same code when you made update on the order under Future call.
Hi Poorna
Your problem fixed?