+ Start a Discussion
vijendahr kvijendahr k 

Json string to apex class covert and insert record in SFDC



i have written below code for post method , an trying to insert response in salesforce, but i am enable to create response in proper format, could you help me why is the issue in below code

@RestResource(urlMapping='/Wrp/WrapCaseManager/*')
global with sharing class WrapCaseManager {

    @HttpPost

    global static WrapCaseManager.Caseinsert doPost(WrapCaseManager.Caseinsert  ACCREQ) {

        account acc=(account)ACCREQ.Ac;
        return ACCREQ;
    /*

    }


    global class outerinsert
    {
        public Caseinsert  cin;
    }
    global class Caseinsert {


        public Account Ac;


    }
}

below is response :

{
    "Ac" :
    {
        "AccountNumber" : "12345",
        "BillingCity" : "Billing City",
        "BillingPostalCode" : "123456",
        "BillingState" : "Billing State/Provinc",
        "BillingStreet" : "street",
        "Industry" : "Agriculture",
        "Name" : "sample"
    }
}

----- issue in valid AccountNumber, can any one help me to fix it.

 
GauravGargGauravGarg

Hi,

In JSON everything is returned as string, so you need to parse JSON into Account object and then need to insert record.

To parse JSON, you can use "JSON.deserialize" method it is explained in below link. 

https://salesforce.stackexchange.com/questions/9328/how-to-parse-the-json-object-in-salesforce

Hope this helps!!!

Let me know if you need more assistance on this. 


Thanks,

Gaurav
Skype: gaurav62990

vijendahr kvijendahr k
Finnaly my issue is solved , Garg,  for my issue posting code below .. if its helps ot any one just like it:

Request:

{
  "req" :
{
  "Acc" : {
    "AccountNumber" : "12345",
        "BillingCity" : "Billing City",
        "BillingPostalCode" : "123456",
        "BillingState" : "Billing State/Provinc",
        "BillingStreet" : "street",
        "Industry" : "Agriculture",
        "Name" : "sample"
  }
}
}

---- code--

@RestResource(urlMapping='/Wrp/WrapCaseManager/*')
global with sharing class WrapCaseManager {

@HttpPost

  global static WrapCaseManager.Accountinsert doPost(WrapCaseManager.Accountinsert req)
  {
   
   list<WrapCaseManager.Accountinsert> AccReqs= new list<WrapCaseManager.Accountinsert>();
  AccReqs.add(req);
  WrapCaseManager.Accountinsert accReq= new WrapCaseManager.Accountinsert();
      accReq.Acc=(Account)req.acc;
   // Account Request=(Account).req;
    system.debug(json.serializePretty('accReq==> '+accReq));
   return req;
  }
 
 global class Accountinsert {
 
   Public Account Acc;
 
 }
}