0

I am trying to write a REST service which will send SF objects between Orgs. Right now, as a test, I am sending data to the same Org that it is coming from. I wrote a GET request which worked so I know the communcation part is working. But I am now trying to send an Account using POST. My JSON looks like this

{
"acct": {
    "attributes": {
        "type": "Account",
        "url": "/services/data/v34.0/sobjects/Account/0018A000002ryhSQAQ"
    },
    "Id": "0018A000002ryhSQAQ",
    "IsDeleted": false,
    "Name": "Test",
    "RecordTypeId": "012C0000000QakwIAC",
    "BillingAddress": null,
    "ShippingAddress": null,
    "OwnerId": "0058A000000I5WbQAK",
    "CreatedDate": "2015-08-25T16:07:17.000+0000",
    "CreatedById": "0058A000000I5WbQAK",
    "LastModifiedDate": "2015-08-25T16:07:17.000+0000",
    "LastModifiedById": "0058A000000I5WbQAK",
    "SystemModstamp": "2015-08-25T16:07:17.000+0000",
    "LastViewedDate": "2015-08-28T13:54:38.000+0000",
    "LastReferencedDate": "2015-08-28T21:54:40.000+0000",
    "IsPartner": false,
    "IsCustomerPortal": false,
    "Advocacy__c": false,
    "Background_check_required__c": "Criminal",
    "External_Content_Approval_Required__c": false,
    "Indicative_Data_Options__c": "Name",
    "Internal_Content_Approval_Required__c": false
}
}

This is the service

This is the code calling the service

public PageReference Migrate() {

        Account myAct = accountInfo ;

        system.debug('myAct = ' + myAct);

        RequestWrapper rw = new RequestWrapper();

        rw.acct = myAct;

        string myActJSON = JSON.serialize(rw);
        system.debug('myActJSON = ' + myActJSON );

        sendAccountDetails(myActJSON );
        return null;
    }

    private string sendAccountDetails(string actDetails) {
        HttpRequest req = new HttpRequest();

        req.setMethod('POST');

        string endpoint = oauth.instance_url +'/services/apexrest/ClientMigration';
        system.debug('endpoint = ' + endpoint);
        req.setEndpoint(endpoint);


        req.setBody(actDetails);
        req.setCompressed(true); 
        req.setHeader('Authorization', 'OAuth '+ oauth.access_token);
        req.setHeader('content-type', 'application/json');

        Http http = new Http();

        HTTPResponse res = http.send(req);

        System.debug('BODY: '+res.getBody());

        System.debug('STATUS:'+res.getStatus());

        System.debug('STATUS_CODE:'+res.getStatusCode());

        return res.getBody();

    }

    public class RequestWrapper{
        Account acct;
        public RequestWrapper(){}
    }     

This is the service

@RestResource(urlMapping='/ClientMigration')
global with sharing class ClientMigrationService {
static Account clientMigrated;
@HttpPost
global static string Migrate(Account Client) {
    //Account a = JSON.deserialize(client);
    /*Map<String, SObject> mapJsonObjects = (Map<String, SObject>)
        JSON.deserialize(Client, Map<String, SObject>.class);
    Account a= (Account) mapJsonObjects.get('Account');   

    insert a;
    clientMigrated = a;
    */
    system.debug (client);
    return 'Success';
}

 public class RequestWrapper{
    Account acct;
    public RequestWrapper(){}
}        

}

This is the error I keep seeing

Unexpected parameter encountered during deserialization: acct at [line:1, column:10]","errorCode":"JSON_PARSER_ERROR"}]

It seems that no matter what I do it complains at the location in the JSON where it get just past the very first object and gets into the inner objects. I tried doing this without the RequestWrapper and just serializing the account directly but had the same problem. I think I must be doing something wrong with the serialization process.

Does anyone have some suggestions? Thanks, Chris

  • 1
    The framework will deserialize for you in the HttpPost method so you don't need any explicit deserialize calls in there. The name and the type of the method parameter matter: your JSON needs to be {Client: {account stuff here}} with your current code. Essentially the parameters to the method have to be held in a JSON object where the names match the Apex code parameter names. – Keith C Aug 28 '15 at 22:21
  • Thank you that makes sense. I think I got it working now. – tchrisbaker Aug 29 '15 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.