Paste this code in the target org
============================
@RestResource(urlMapping='/v1/CustomResturl/*')
global with sharing class getContact {
@Httpget
global static list<contact> mycontacts(){
RestRequest req = RestContext.request;
RestResponse res = Restcontext.response;
list<contact> lstcontact =[Select id , name,Email from contact];
system.debug('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'+lstcontact);
return lstcontact ;
}
}
==================================
Execute below code in the Source org
==========================
public class DeserializeJsonResp
{
public String id;
public String access_token;
}
String reqbody = 'grant_type=password&client_id='+'your clientid which you got from your third party connected app'+'&client_secret='+'your secret key which you got from your third party connected app'+'&username='+'jagadeesh777@chitti.com'+'&password='+'yourpasswordsecuritytoken';
System.debug('%%%%%%%%%%%%%%%%%'+reqbody);
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndpoint('https://jagadeesh777-dev-ed.my.salesforce.com/services/oauth2/token');
HttpResponse res = h.send(req);
System.debug('&&&&&&&&&&&&&&&&'+res.getbody());
DeserializeJsonResp responsewithtoken = (DeserializeJsonResp)JSON.deserialize(res.getbody(),DeserializeJsonResp.class);
system.debug('@@@@access_token@@'+responsewithtoken );
Http h2 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization','Bearer ' + responsewithtoken.access_token);
req1.setHeader('Content-Type','application/json');
req1.setHeader('accept','application/json');
req1.setMethod('GET');
req1.setEndpoint('https://jagadeesh777-dev-ed.my.salesforce.com/services/apexrest/v1/CustomResturl/');
HttpResponse res1 = h2.send(req1);
String trimmedResponse = res1.getBody().unescapeCsv().remove('\\');
JSONParser parser = JSON.createParser(res1.getBody());
List<Contact> insertcontacts=new List<Contact>();
while (parser.nextToken() != null) {
//Id
if((parser.getCurrentToken() == JSONToken.FIELD_NAME) ){
Contact cont;
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Id')) {
// Get the value.
parser.nextToken();
// Compute the grand total price for all invoices.
string sId= parser.getText();
cont=new Contact();
cont.Id=sId;
system.debug('Id@@@' + sId);
parser.nextToken();
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'Name')) {
parser.nextToken();
string sName= parser.getText();
cont.LastName=sName;
}
//Email
parser.nextToken();
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'Email')) {
parser.nextToken();
string sEmail= parser.getText();
cont.Email=sEmail;
}
insertcontacts.add(cont);
}
}
}
Insert contList;
============================
@RestResource(urlMapping='/v1/CustomResturl/*')
global with sharing class getContact {
@Httpget
global static list<contact> mycontacts(){
RestRequest req = RestContext.request;
RestResponse res = Restcontext.response;
list<contact> lstcontact =[Select id , name,Email from contact];
system.debug('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'+lstcontact);
return lstcontact ;
}
}
==================================
Execute below code in the Source org
==========================
public class DeserializeJsonResp
{
public String id;
public String access_token;
}
String reqbody = 'grant_type=password&client_id='+'your clientid which you got from your third party connected app'+'&client_secret='+'your secret key which you got from your third party connected app'+'&username='+'jagadeesh777@chitti.com'+'&password='+'yourpasswordsecuritytoken';
System.debug('%%%%%%%%%%%%%%%%%'+reqbody);
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndpoint('https://jagadeesh777-dev-ed.my.salesforce.com/services/oauth2/token');
HttpResponse res = h.send(req);
System.debug('&&&&&&&&&&&&&&&&'+res.getbody());
DeserializeJsonResp responsewithtoken = (DeserializeJsonResp)JSON.deserialize(res.getbody(),DeserializeJsonResp.class);
system.debug('@@@@access_token@@'+responsewithtoken );
Http h2 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization','Bearer ' + responsewithtoken.access_token);
req1.setHeader('Content-Type','application/json');
req1.setHeader('accept','application/json');
req1.setMethod('GET');
req1.setEndpoint('https://jagadeesh777-dev-ed.my.salesforce.com/services/apexrest/v1/CustomResturl/');
HttpResponse res1 = h2.send(req1);
String trimmedResponse = res1.getBody().unescapeCsv().remove('\\');
JSONParser parser = JSON.createParser(res1.getBody());
List<Contact> insertcontacts=new List<Contact>();
while (parser.nextToken() != null) {
//Id
if((parser.getCurrentToken() == JSONToken.FIELD_NAME) ){
Contact cont;
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Id')) {
// Get the value.
parser.nextToken();
// Compute the grand total price for all invoices.
string sId= parser.getText();
cont=new Contact();
cont.Id=sId;
system.debug('Id@@@' + sId);
parser.nextToken();
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'Name')) {
parser.nextToken();
string sName= parser.getText();
cont.LastName=sName;
}
parser.nextToken();
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'Email')) {
parser.nextToken();
string sEmail= parser.getText();
cont.Email=sEmail;
}
insertcontacts.add(cont);
}
}
}
Insert contList;
No comments:
Post a Comment