in the appended class, when i am trying to insert a record in json format through workbench, it throws an error.
Apex Class :
@RestResource(urlmapping='/createnewrecord/*')
Global class CreateAccount {
@HttpPost
global static GenericResponse createNewAccount(CustomReq rqst)
{
// do your ifs and elses and check whether the JSON structure is right
Savepoint savePoint = Database.setSavepoint();
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
res.addHeader('Content-Type','application/json');
GenericResponse response= new GenericResponse();
try
{
Account accountRecord = new Account ();
accountRecord.Name = rqst.account.name;
insert accountRecord;
Contact contactRecord = new Contact ();
contactRecord.AccountId = accountRecord.Id;
contactRecord.FirstName = rqst.account.contact.firstname;
contactRecord.LastName = rqst.account.contact.lastname;
insert contactRecord;
Opportunity Opp = new Opportunity();
opp.AccountId = accountRecord.Id;
Opp.Name = rqst.account.opp.opportunityname;
insert opp;
Quote Q = new Quote();
Q.OpportunityId = opp.Id;
Q.Name = rqst.account.opp.Quote.QuoteName;
insert Q;
QuoteLineItem QLI = New QuoteLineitem();
QLI.QuoteId = Q.Id;
Product2 P = new Product2();
P.Id = QLI.QuoteId;
p.Name = rqst.account.opp.Quote.QLI.Product.productname;
response.message='Pass :)';
}
catch (Exception e)
{
Database.rollback(savePoint);
response.message='Fail';
}
return response;
}
//Post method ends here.
//Post method wrapper starts here.
global class GenericResponse
{
public string CustomReq;
public String status;
public String Message;
}
global class CustomReq
{
public CustomAccount account;
}
global class CustomAccount
{
public String name;
public CustomContact contact;
public customopp opp;
}
global class CustomContact
{
public String firstname;
public String lastname;
}
Global Class CustomOpp
{
public string OpportunityName;
public CustomQuote quote;
}
Global Class CustomQuote{
public string QuoteName;
public CustomQuoteLineitem QLI;
}
Global Class CustomQuoteLineItem{
public CustomProduct product;
}
Global Class CustomProduct{
public string ProductName;
}
}
Json Response from Workbench :
{
"Account" :
{"Name" : "test account name 21",},
"Contact" : {"firstname" : "John 21", "lastname" : "Smith 21"}
}
I shall be grateful to you for your kind cooperation.
2 réponses
in Json response, i am just inserting account and contact a simple one. therefore, there is no question of Quotelineitem and product.