+ Start a Discussion
Rohan TelangRohan Telang 

Date submitted to REST API in POSTMAN is giving error

Hello,
We have created a form in our website to submit info that will create a Order in Salesforce.  Three of the Fields we have in Order object are 
"EffectiveDate","License_activation_date__c" and "License_Expiration_date__c". I am submitting the date in the correct format of yyyy-MM-dd and the API request is correct and the response back is successful. However, when I am submmiting the request in the body, it's giving error.
I am submitting the JSON in the body as follows-

[
{
    "accountId":"0015E00001eJboYQAS",
    "licenseActivationDate":"2020-08-03",
    "licenseExpirationDate":"2021-10-11",
    "orderStartDate":"2021-08-21",
    "status":"Draft",
    "contractNumber":"8005E000002VXzxQAG"
}
]


But, it's giving error as Expected EGS_Order_API.DateValue but found "2020-08-03" at [line:3, column:37]

My REST API Class is as follows-

@RestResource(urlMapping='/OrderForWebTeam/*')
global class EGS_OrderForWebTeam {
    
    public class OrderData{
        public Id accountId;
        public String orderName;
        public String customerId;
        public Decimal currencys;
        public String billingStreet;
        public String billingCity;
        public String billingState;
        public String billingPostalCode;
        public String billingCountry;
        public String status;
        public String orderAmount;
        public DateValue orderStartDate;
        public String orderType;
        public DateValue licenseActivationDate;
        public DateValue licenseExpirationDate;
        public Id productId;
        public String entitlementId;    
        public Id contractNumber;    
    }
    
    public class DateValue{
        public Integer day;
        public Integer month;
        public Integer year;
    }
    
    @httpPost
    global static void createOrderData(){
        try{
            RestRequest req = RestContext.request;
            String jsonString = req.requestBody.tostring();
            List<OrderData> receivedData = (List<OrderData>)System.JSON.deserialize(jsonString, List<OrderData>.class);
            
            List<Order> orderList = new List<Order>();
            for(OrderData oData:receivedData){
                Order orderObject= New Order();
                orderObject.AccountId=oData.accountId;
                orderObject.Name=oData.orderName;
                orderObject.Customer_ID__c=oData.customerId;
                orderObject.Currency__c=oData.currencys;
                orderObject.BillingStreet=oData.billingStreet;
                orderObject.BillingCity=oData.billingCity;
                orderObject.BillingState=oData.billingState;
                orderObject.BillingPostalCode=oData.billingPostalCode;
                orderObject.BillingCountry=oData.billingCountry;
                orderObject.Status=oData.status;
                orderObject.Type=oData.orderType;
                orderObject.License_activation_date__c= Date.newInstance(oData.licenseActivationDate.year,oData.licenseActivationDate.month,oData.licenseActivationDate.day);
                orderObject.License_Expiration_date__c= Date.newInstance(oData.licenseExpirationDate.year,oData.licenseExpirationDate.month,oData.licenseExpirationDate.day);
                orderObject.EffectiveDate= Date.newInstance(oData.orderStartDate.year,oData.orderStartDate.month,oData.orderStartDate.day);
                orderObject.Product_name__c=oData.productId;
                orderObject.Entitlement_ID__c=oData.entitlementId;
                orderObject.ContractId=oData.contractNumber;
                
                orderList.add(orderObject);
            }
            insert orderList;
            RestResponse res = RestContext.response;
            res.statusCode = 200;
            res.responseBody = Blob.valueOf('Order Inserted Successfully..');
        }catch(Exception e){
            RestResponse res = RestContext.response;
            res.statusCode = 404;
            res.responseBody = Blob.valueOf('Something went wrong! Error Message : '+e.getMessage());
        }
    }
    
}



Does anyone have any idea what could be the problem?

Thanks !!
Best Answer chosen by Rohan Telang
Maharajan CMaharajan C
Hi Roham,

I don't know why you have created the seperate wrapper class for Date. It's not needed and you can directly use the date value from JSON because your date format in JSON looks fine.

Please try the below changes in your apex class:
 
@RestResource(urlMapping='/OrderForWebTeam/*')
global class EGS_OrderForWebTeam {
    
    public class OrderData{
        public Id accountId;
        public String orderName;
        public String customerId;
        public Decimal currencys;
        public String billingStreet;
        public String billingCity;
        public String billingState;
        public String billingPostalCode;
        public String billingCountry;
        public String status;
        public String orderAmount;
        public Date orderStartDate;
        public String orderType;
        public Date licenseActivationDate;
        public Date licenseExpirationDate;
        public Id productId;
        public String entitlementId;    
        public Id contractNumber;    
    }
    
    @httpPost
    global static void createOrderData(){
        try{
            RestRequest req = RestContext.request;
            String jsonString = req.requestBody.tostring();
            List<OrderData> receivedData = (List<OrderData>)System.JSON.deserialize(jsonString, List<OrderData>.class);
            
            List<Order> orderList = new List<Order>();
            for(OrderData oData:receivedData){
                Order orderObject= New Order();
                orderObject.AccountId=oData.accountId;
                orderObject.Name=oData.orderName;
                orderObject.Customer_ID__c=oData.customerId;
                orderObject.Currency__c=oData.currencys;
                orderObject.BillingStreet=oData.billingStreet;
                orderObject.BillingCity=oData.billingCity;
                orderObject.BillingState=oData.billingState;
                orderObject.BillingPostalCode=oData.billingPostalCode;
                orderObject.BillingCountry=oData.billingCountry;
                orderObject.Status=oData.status;
                orderObject.Type=oData.orderType;
                orderObject.License_activation_date__c= oData.licenseActivationDate;
                orderObject.License_Expiration_date__c= oData.licenseExpirationDate;
                orderObject.EffectiveDate= oData.orderStartDate;
                orderObject.Product_name__c=oData.productId;
                orderObject.Entitlement_ID__c=oData.entitlementId;
                orderObject.ContractId=oData.contractNumber;
                
                orderList.add(orderObject);
            }
            insert orderList;
            RestResponse res = RestContext.response;
            res.statusCode = 200;
            res.responseBody = Blob.valueOf('Order Inserted Successfully..');
        }catch(Exception e){
            RestResponse res = RestContext.response;
            res.statusCode = 404;
            res.responseBody = Blob.valueOf('Something went wrong! Error Message : '+e.getMessage());
        }
    }
    
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Roham,

I don't know why you have created the seperate wrapper class for Date. It's not needed and you can directly use the date value from JSON because your date format in JSON looks fine.

Please try the below changes in your apex class:
 
@RestResource(urlMapping='/OrderForWebTeam/*')
global class EGS_OrderForWebTeam {
    
    public class OrderData{
        public Id accountId;
        public String orderName;
        public String customerId;
        public Decimal currencys;
        public String billingStreet;
        public String billingCity;
        public String billingState;
        public String billingPostalCode;
        public String billingCountry;
        public String status;
        public String orderAmount;
        public Date orderStartDate;
        public String orderType;
        public Date licenseActivationDate;
        public Date licenseExpirationDate;
        public Id productId;
        public String entitlementId;    
        public Id contractNumber;    
    }
    
    @httpPost
    global static void createOrderData(){
        try{
            RestRequest req = RestContext.request;
            String jsonString = req.requestBody.tostring();
            List<OrderData> receivedData = (List<OrderData>)System.JSON.deserialize(jsonString, List<OrderData>.class);
            
            List<Order> orderList = new List<Order>();
            for(OrderData oData:receivedData){
                Order orderObject= New Order();
                orderObject.AccountId=oData.accountId;
                orderObject.Name=oData.orderName;
                orderObject.Customer_ID__c=oData.customerId;
                orderObject.Currency__c=oData.currencys;
                orderObject.BillingStreet=oData.billingStreet;
                orderObject.BillingCity=oData.billingCity;
                orderObject.BillingState=oData.billingState;
                orderObject.BillingPostalCode=oData.billingPostalCode;
                orderObject.BillingCountry=oData.billingCountry;
                orderObject.Status=oData.status;
                orderObject.Type=oData.orderType;
                orderObject.License_activation_date__c= oData.licenseActivationDate;
                orderObject.License_Expiration_date__c= oData.licenseExpirationDate;
                orderObject.EffectiveDate= oData.orderStartDate;
                orderObject.Product_name__c=oData.productId;
                orderObject.Entitlement_ID__c=oData.entitlementId;
                orderObject.ContractId=oData.contractNumber;
                
                orderList.add(orderObject);
            }
            insert orderList;
            RestResponse res = RestContext.response;
            res.statusCode = 200;
            res.responseBody = Blob.valueOf('Order Inserted Successfully..');
        }catch(Exception e){
            RestResponse res = RestContext.response;
            res.statusCode = 404;
            res.responseBody = Blob.valueOf('Something went wrong! Error Message : '+e.getMessage());
        }
    }
    
}

Thanks,
Maharajan.C
This was selected as the best answer
Rohan TelangRohan Telang
@Maharajan.C - Thank You so much Sir !!! I did the changes as per given by you. It Worked.