1

I have to create Account/Contact in a single HTTPPOST call using REST API.

I am trying to create below class but it does not allow me to save.

@RestResource(urlMapping='/CreateCustomer/*')
global with sharing class CreateCustomerRestAPI {

    @HttpPost
    global static string doPost(){
       System.debug('Start');
        RestRequest  req     = RestContext.request;   
         //Getting JSON String from request body
         String jsonInput  = req.requestBody.toString();

         System.debug('jsonInput::::'+jsonInput);

        AccountWrapper d = (AccountWrapper)JSON.deserialize(jsonInput, AccountWrapper.class);
        //System.debug('accInstancae::::'+d);

    return null;

}   

    public class AccountWrapper{
    public String name; //AccountTest
    public cls_contact contact;
    public class cls_contact {
        public String firstName;    //contactFirstName
        public String secondName;   //contactSecondName
    }
  }

}

Please look into it and let me know what is wrong with able code.

Many Thanks Arjun

  • 1
    where's the DML?; simply deserializing the JSON into apex objects won't cause any DML to occur – cropredy Jan 25 '16 at 5:57
1

Salesforce allow one level deep inner classes.

The following is a list of the major differences between Apex classes and Java classes:

  • Inner classes and interfaces can only be declared one level deep inside an outer class.
  • An inner class behaves like a static Java inner class, but doesn’t require the static keyword. An inner class can have instance member variables like an outer class, but there is no implicit pointer to an instance of the outer class (using the this keyword).

    so your code will be like this

    @RestResource(urlMapping='/CreateCustomer/*')
    global with sharing class CreateCustomerRestAPI {
    
    @HttpPost
    global static string doPost(){
    System.debug('Start');
    RestRequest  req     = RestContext.request;   
     //Getting JSON String from request body
     String jsonInput  = req.requestBody.toString();
    
     System.debug('jsonInput::::'+jsonInput);
    
    AccountWrapper d = (AccountWrapper)JSON.deserialize(jsonInput, AccountWrapper.class);
    //System.debug('accInstancae::::'+d);
    
    return null;
    
    }   
    
    public class AccountWrapper{
    public String name; //AccountTest
    public cls_contact contact;
    
    
    }
    
    public class cls_contact {
        public String firstName;    //contactFirstName
        public String secondName;   //contactSecondName
    }
    
    }
    
| improve this answer | |
1

You can only have inner classes one level deep. For example:

public class myOuterClass {
   // Additional myOuterClass code here
   class myInnerClass {
     // myInnerClass code here
   }
}

You are trying to create 2 level deep inner class which is not allowed. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_defining.htm

There are 2 ways(AFAIK) to process the nested JSON structures:

1) Create objects in inner class of other inner class like this.

// Inner classes used for serialization by readValuesAs(). 

public class Invoice {
    public Double totalPrice;
    public DateTime statementDate;
    public Long invoiceNumber;
    List<LineItem> lineItems;

    public Invoice(Double price, DateTime dt, Long invNumber, List<LineItem> liList) {
        totalPrice = price;
        statementDate = dt;
        invoiceNumber = invNumber;
        lineItems = liList.clone();
    }
}  

public class LineItem {
    public Double unitPrice;
    public Double quantity;
    public String productName;
}

2) Use Untyped Parsing of JSON Objects:

String s = '{"totalResults": 2, "startIndex":0, "pricing":[{"price":10.80,"cost":9.22,"gp":12},{"price":5.50,"cost":4.00,"gp":24}]}';
Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(s);
List<Object> pricing = (List<Object>) m.get('pricing');
for (Object o : pricing) {
    Map<String, Object> p = (Map<String, Object>) o;
    System.debug('>>> ' + p);
}
| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.