Trigger:
trigger Intglead on Lead (After Insert) {
List<string> lsstr= new List<string>();
for(Lead xt:Trigger.new)
{
lsstr.add(String.valueOf(xt.LastName));
lsstr.add(String.valueOf(xt.Company));
lsstr.add(String.valueOf(xt.Status));
}
SendLeadUsingRESTAPI.callcreateAcc(lsstr);
}
Apex class:
public class SendLeadUsingRESTAPI {
private final String clientId = 'xxxxxxxxxxxxxxxxx';
private final String clientSecret = 'xxxxxx';
private final String username = 'xxxxxxxx';
private final String password = 'xxxxxxxxx';
public class deserializeResponse
{
public String id;
public String access_token;
}
public String ReturnAccessToken (SendLeadUsingRESTAPI leada)
{
String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndpoint('https://na30.salesforce.com/services/oauth2/token');
HttpResponse res = h.send(req);
deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
return resp1.access_token;
}
@future(callout=true)
public static void callcreateAcc (List<String> l)
{
List<string> js = new List<string>();
SendLeadUsingRESTAPI leadb = new SendLeadUsingRESTAPI();
String accessToken;
accessToken = leadb.ReturnAccessToken(leadb);
for(string s : l)
{
js.add(s);
}
if(accessToken != null){
String endPoint = 'https://na30.salesforce.com/services/apexrest/Lead/doPost';
String jsonstr = (String)JSON.serialize(js);
System.debug('hello'+jsonstr);
Http h2 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization','Bearer ' + accessToken);
req1.setHeader('Content-Type','application/json');
req1.setHeader('accept','application/json');
req1.setBody(jsonstr);
req1.setMethod('POST');
req1.setEndpoint(endPoint);
HttpResponse res1 = h2.send(req1);
}
}
}
Target Org Webservice Class:
@RestResource(urlMapping='/Lead/doPost')
global with sharing class MyRestResource {
@HttpPost
global static String doPost(String name,
String Company, String Status) {
Lead ld = new Lead();
ld.LastName = name;
ld.Company = Company;
ld.Status = Status;
insert ld;
return ld.Id;
}
}
I am trying to integrate if lead created in one org it will automatically created in another org using apex rest.
I am getting error:
12:44:02.19 (413305091)|VARIABLE_ASSIGNMENT|[35]|endPoint|"https://na30.salesfo (37 more) ..."
12:44:02.19 (413307102)|STATEMENT_EXECUTE|[36]
12:44:02.19 (413317598)|METHOD_ENTRY|[36]||System.JSON.serialize(Object)
12:44:02.19 (413643659)|METHOD_EXIT|[36]||System.JSON.serialize(Object)
12:44:02.19 (413650492)|VARIABLE_SCOPE_BEGIN|[36]|jsonstr|String|false|false
12:44:02.19 (413662132)|VARIABLE_ASSIGNMENT|[36]|jsonstr|"[\"youtest7\",\"rt7\",\"N (4 more) ..."
12:44:02.19 (413665227)|STATEMENT_EXECUTE|[37]
12:44:02.19 (413668691)|HEAP_ALLOCATE|[37]|Bytes:5
12:44:02.19 (413685019)|HEAP_ALLOCATE|[37]|Bytes:29
12:44:02.19 (413723066)|USER_DEBUG|[37]|DEBUG|hello["youtest7","rt7","New"]
12:44:02.19 (413730586)|STATEMENT_EXECUTE|[38]
12:44:02.19 (413751628)|VARIABLE_SCOPE_BEGIN|[38]|h2|System.Http|true|false
12:44:02.19 (413791469)|VARIABLE_ASSIGNMENT|[38]|h2|"System.Http[]"|0x76c7719a
12:44:02.19 (413795785)|STATEMENT_EXECUTE|[39]
12:44:02.19 (413810854)|VARIABLE_SCOPE_BEGIN|[39]|req1|System.HttpRequest|true|false
12:44:02.19 (413849167)|VARIABLE_ASSIGNMENT|[39]|req1|"System.HttpRequest[Endpoint=null, Method=null]"|0x42bcd6d0
12:44:02.19 (413853947)|STATEMENT_EXECUTE|[40]
12:44:02.19 (413858158)|HEAP_ALLOCATE|[40]|Bytes:13
12:44:02.19 (413861861)|HEAP_ALLOCATE|[40]|Bytes:7
12:44:02.19 (413868267)|HEAP_ALLOCATE|[40]|Bytes:119
12:44:02.19 (413926757)|STATEMENT_EXECUTE|[41]
12:44:02.19 (413931817)|HEAP_ALLOCATE|[41]|Bytes:12
12:44:02.19 (413935344)|HEAP_ALLOCATE|[41]|Bytes:16
12:44:02.19 (413965572)|STATEMENT_EXECUTE|[42]
12:44:02.19 (413969978)|HEAP_ALLOCATE|[42]|Bytes:6
12:44:02.19 (413997079)|STATEMENT_EXECUTE|[43]
12:44:02.19 (414026520)|STATEMENT_EXECUTE|[44]
12:44:02.19 (414051825)|STATEMENT_EXECUTE|[45]
12:44:02.19 (414072712)|STATEMENT_EXECUTE|[46]
12:44:02.19 (416325080)|CALLOUT_REQUEST|[46]|System.HttpRequest[Endpoint=https://na30.salesforce.com/services/apexrest/Lead/doPost, Method=POST]
12:44:02.19 (500457659)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:389
12:44:02.19 (500534441)|CALLOUT_RESPONSE|[46]|System.HttpResponse[Status=Bad Request, StatusCode=400]
12:44:02.19 (500557728)|HEAP_ALLOCATE|[46]|Bytes:120
12:44:02.19 (500589704)|VARIABLE_SCOPE_BEGIN|[46]|res1|System.HttpResponse|true|false
12:44:02.19 (500666784)|VARIABLE_ASSIGNMENT|[46]|res1|"System.HttpResponse[Status=Bad Request, StatusCode=400]"|0x68aa2c88
12:44:02.500 (500691680)|CUMULATIVE_LIMIT_USAGE
12:44:02.500 (500691680)|LIMIT_USAGE_FOR_NS|(default)|