• Manjunath reddy 6
  • NEWBIE
  • 25 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
Hi I am trying to do Integration between two different organizations in salesforce Using REST API and REST Web Service and Apex Web Service.For safety purpose I did'nt mention my credentials in the below code, actually in my org Iam using all the credentials perfectly, this code was working fine earlier, but now, Iam not abel to get response from the target org.I am getting the error message in the debug log "Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2]", I am not able to figure out what is the issue? can some one help me on this.

My debug logs are as follows.
13:38:28.26 (28575173)|USER_DEBUG|[17]|DEBUG|===reqbody== grant_type=password&client_id=3MVG9Y6d_Btp4xp5pS0Mwb345aVwqNmCpRF9rEs1QMG_WkvGPZtIKsyUgujSZPhIcCzzxFQqugJCA7fs7SkZB& client_secret=3594199692394096988&username=manjunath@ceptes.com&password=Surya@12338VnYqobYUKwN718uV5oYB9Hy



13:38:28.26 (162542537)|USER_DEBUG|[19]|DEBUG|===res=== System.HttpResponse[Status=Not Found, StatusCode=404]
 
public class SendAccountFromSource {
    private final String clientId = 'Clent Id from App';
    private final String clientSecret = 'clientSecretfrom connected app';
    private final String username = 'User name';
    private final String password = 'passwordwithsecuritytoken';
public class deserializeResponse{
    public String id;
    public String access_token;
}
public String ReturnAccessToken (SendAccountFromSource acount){
    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://login.salesforce.com/services/oauth2/callback');
    system.debug('===reqbody== '+reqbody);
    HttpResponse res = h.send(req);
    system.debug('===res=== '+res );
    deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
    system.debug('===resp1.access_token== '+resp1.access_token);
    return resp1.access_token;
}
@future(callout=true)
public static void createAccount(String accName, String accId){
    SendAccountFromSource acount = new SendAccountFromSource();
    system.debug('===acount == '+acount );
    String accessToken = acount.ReturnAccessToken (acount);
    system.debug('===accessToken == '+accessToken );
    if(accessToken != null){
    String endPoint = 'https://ap2.salesforce.com/services/data/v32.0/sobjects/Account/';
    String jsonstr = '{"Name" : "' + accName + '"}';
    system.debug('===jsonstr == '+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);
    system.debug('===res1 == '+res1 );
    deserializeResponse resp2 = (deserializeResponse)JSON.deserialize(res1.getbody(),deserializeResponse.class);
    Account a = [SELECT Id FROM Account WHERE Id = :accId];
    system.debug('===a==='+a);
    system.debug('===resp2== '+resp2 );
    system.debug('===jsonstr == '+jsonstr );
    update a;
    }
}
}



 
trigger ClosedOpportunityTrigger on Opportunity (after insert,after update) {
List<task> carry=New List<task>();

  for(opportunity opp:trigger.new){
   if(opp.stagename=='closed own'){
    task t=new task(whatid=opp.id);
    carry.add(t); 
    }
   }
     insert carry;

 }

 The trigger will add a task to any opportunity inserted or updated with the stage of 'Closed Won'. The task's subject must be 'Follow Up Test Task'.The Apex trigger must be called 'ClosedOpportunityTrigger'
With 'ClosedOpportunityTrigger' active, if an opportunity is inserted or updated with a stage of 'Closed Won', it will have a task created with the subject 'Follow Up Test Task'.
To associate the task with the opportunity, fill the 'WhatId' field with the opportunity ID.
This challenge specifically tests 200 records in one operation.

I tried with the above code, task is not created.
I have a  requirement to copy phone from contact to related account phone.but my trigger not works, can some one help me on this
trigger copy on account(after insert,after update) {

List<id> carry=New list<id>();
For(account acc:trigger.new){
carry.add(acc.id);

}
List<Contact> hold=[select id, name,phone from contact where id IN: carry];
List<account> take=[Select id,phone from account where id IN: carry];
for(account acc:take){
for(contact c:hold){
acc.phone=c.phone;
}
}
}

I tried to create a trigger on contact also as below but not works.
trigger abc on Contact (after insert,after update) {

List<id> carry=New list<id>();
For(contact c:trigger.new){
carry.add(c.id);

}
List<Contact> hold=[select id, name,phone from contact ];
List<account> take=[Select id,phone from account where id IN: carry];
for(account acc:take){
for(contact c:hold){
acc.phone=c.phone;
}
}

}
Hi I am trying to make a call out using rest services.the scenario is when I create an account in source org I should create an account in the target org.I have created a trigger SendAccount which calls the class SendAccountFromSource. Iam able to get access token as a response, but I am not able to get response when I use end point URL my debuglogs are given below, please anyone help me on this
trigger SendAccount on Account(after insert){
    for(Account a : Trigger.new){
            SendAccountFromSource.createAccount(a.Name, a.Id);        
     }
}
 
public class SendAccountFromSource {
    private final String clientId = '3MVG9Y6d_Btp4xp5pS0Mwb345aVwqNmCpRF9rEs1QMG_WkvGPZtIKsyUgujSZPhIcCzzxFQqugJCA7fs7SkZB';
    private final String clientSecret = '3594199692394096988';
    private final String username = 'manjunath@ceptes.com';
    private final String password = 'Surya@123SKIjxR3KtHFY6wv3uhLfbga0E';
public class deserializeResponse{
    public String id;
    public String access_token;
}
public String ReturnAccessToken (SendAccountFromSource acount){
    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);
system.debug('====================reqbody============'+reqbody);
req.setMethod('POST');
req.setEndpoint('https://ap1.salesforce.com/services/oauth2/token');
system.debug('====================req============'+req);
HttpResponse res = h.send(req);
system.debug('====================res============'+res);
    deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
system.debug('====================resp1============'+resp1);
system.debug('====================resp1.access_token============'+resp1.access_token);
String accessToken = resp1.access_token;
String accName = 'Shiva8';
return accessToken;
}
@future(callout=true)
public static void createAccount(String accName, String accId){
    system.debug('====================accId============'+accId);
    SendAccountFromSource acount = new SendAccountFromSource();
    system.debug('===acount == '+acount );
    String accessToken = acount.ReturnAccessToken (acount);
    if(accessToken != null){
    system.debug('====================accId============'+accId);
    String endPoint = 'https://ap1.salesforce.com/services/apexrest/createAccount/';
                       //https://ap1.salesforce.com/services/apexrest/v1/getaccountids/
    //String endPoint = 'https://ap1.salesforce.com/services/apexrest/v1/getaccountids/';
    //String jsonstr = '{"AccName" : "' + accName + '"}';
    String jsonstr = '{"Name" : "' + accName + '"}';
    
    Http h2 = new Http();
    HttpRequest req1 = new HttpRequest();
    req1.setHeader('Authorization','Bearer ' + accessToken); 
    req1.setHeader('Content-Type','application/json;charset=UTF-8');
    req1.setHeader('accept','application/json');
    req1.setBody('jsonstr');
    req1.setMethod('POST');
    req1.setEndpoint(endPoint);
    HttpResponse res1 = h2.send(req1);
       system.debug('====================res1============'+res1.getBody());
    String trimmedResponse = res1.getBody().unescapeCsv().remove('\\');
    system.debug('====================trimmedResponse ============'+trimmedResponse );
    deserializeResponse resp2 = (deserializeResponse)JSON.deserialize(trimmedResponse, deserializeResponse.class);
    system.debug('====================resp2 ============'+resp2);

    
    
    Account a = [SELECT Id FROM Account WHERE Id = :accId];
    //a.externalId__c= resp2.id;
    update a; 

       
  } 
}
}
16:55:38.17 (17241361)|CODE_UNIT_STARTED|[EXTERNAL]|01p280000054SO5|SendAccountFromSource.createAccount 16:55:38.17 (18294404)|USER_DEBUG|[30]|DEBUG|====================accId============0012800000peYgoAAE 16:55:38.17 (18506002)|USER_DEBUG|[32]|DEBUG|===acount == SendAccountFromSource:[clientId=3MVG9Y6d_Btp4xp5pS0Mwb345aVwqNmCpRF9rEs1QMG_WkvGPZtIKsyUgujSZPhIcCzzxFQqugJCA7fs7SkZB, clientSecret=3594199692394096988, password=Surya@123SKIjxR3KtHFY6wv3uhLfbga0E, username=manjunath@ceptes.com] 16:55:38.17 (18681163)|USER_DEBUG|[15]|DEBUG|====================reqbody============grant_type=password&client_id=3MVG9Y6d_Btp4xp5pS0Mwb345aVwqNmCpRF9rEs1QMG_WkvGPZtIKsyUgujSZPhIcCzzxFQqugJCA7fs7SkZB&client_secret=3594199692394096988&username=manjunath@ceptes.com&password=Surya@123SKIjxR3KtHFY6wv3uhLfbga0E 16:55:38.17 (18780827)|USER_DEBUG|[18]|DEBUG|====================req============System.HttpRequest[Endpoint=https://ap1.salesforce.com/services/oauth2/token, Method=POST] 16:55:38.17 (18859108)|CALLOUT_REQUEST|[19]|System.HttpRequest[Endpoint=https://ap1.salesforce.com/services/oauth2/token, Method=POST] 16:55:38.17 (488682227)|CALLOUT_RESPONSE|[19]|System.HttpResponse[Status=OK, StatusCode=200] 16:55:38.17 (488841703)|USER_DEBUG|[20]|DEBUG|====================res============System.HttpResponse[Status=OK, StatusCode=200] 16:55:38.17 (489959480)|USER_DEBUG|[22]|DEBUG|====================resp1============deserializeResponse:[access_token=00D90000000gZ83!AQEAQKB7bYRQZObCuq7fvGfN8p9xc9UjvF2hvRmsc5xFc778E6IPDYfqeFCJxIAjet8vdhk8rzk2Rgnw5VngAWBcv.JypYsv, id=https://login.salesforce.com/id/00D90000000gZ83EAE/005900000014zatAAA] 16:55:38.17 (490013080)|USER_DEBUG|[23]|DEBUG|====================resp1.access_token============00D90000000gZ83!AQEAQKB7bYRQZObCuq7fvGfN8p9xc9UjvF2hvRmsc5xFc778E6IPDYfqeFCJxIAjet8vdhk8rzk2Rgnw5VngAWBcv.JypYsv 16:55:38.17 (490065182)|USER_DEBUG|[35]|DEBUG|====================accId============0012800000peYgoAAE 16:55:38.17 (492632717)|CALLOUT_REQUEST|[50]|System.HttpRequest[Endpoint=https://ap1.salesforce.com/services/apexrest/createAccount/, Method=POST] 16:55:38.17 (688726311)|CALLOUT_RESPONSE|[50]|System.HttpResponse[Status=Bad Request, StatusCode=400] 16:55:38.17 (688871235)|USER_DEBUG|[51]|DEBUG|====================res1============[{"message":"Unexpected character ('j' (code 106)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [line:1, column:2]","errorCode":"JSON_PARSER_ERROR"}] 16:55:38.17 (689407452)|USER_DEBUG|[53]|DEBUG|====================trimmedResponse ============[{"message":"Unexpected character ('j' (code 106)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [line:1, column:2]","errorCode":"JSON_PARSER_ERROR"}] 16:55:38.17 (689816529)|FATAL_ERROR|System.JSONException: Malformed JSON: Expected '{' at the beginning of object Class.System.JSON.deserialize: line 15, column 1 Class.SendAccountFromSource.createAccount: line 54, column 1 16:55:38.17 (689839357)|FATAL_ERROR|System.JSONException: Malformed JSON: Expected '{' at the beginning of object Class.System.JSON.deserialize: line 15, column 1 Class.SendAccountFromSource.createAccount: line 54, column 1
trigger ClosedOpportunityTrigger on Opportunity (after insert,after update) {
List<task> carry=New List<task>();

  for(opportunity opp:trigger.new){
   if(opp.stagename=='closed own'){
    task t=new task(whatid=opp.id);
    carry.add(t); 
    }
   }
     insert carry;

 }

 The trigger will add a task to any opportunity inserted or updated with the stage of 'Closed Won'. The task's subject must be 'Follow Up Test Task'.The Apex trigger must be called 'ClosedOpportunityTrigger'
With 'ClosedOpportunityTrigger' active, if an opportunity is inserted or updated with a stage of 'Closed Won', it will have a task created with the subject 'Follow Up Test Task'.
To associate the task with the opportunity, fill the 'WhatId' field with the opportunity ID.
This challenge specifically tests 200 records in one operation.

I tried with the above code, task is not created.