1

I am getting this error below when i tried testing my apex rest services using work bench

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Company_Make__c, Engine_Displacement__c, Power__c, Torque__c, Transmission__c, Fuel_Tank_Capacity__c]: [Company_Make__c, Engine_Displacement__c, Power__c, Torque__c, Transmission__c, Fuel_Tank_Capacity__c] Class.BikeManagement.doPost: line 40, column 1

but iam passing all the required fields(cross checked with only layout i have).

my post method

@RestResource(urlMapping='/v1/BikeManagement/')
global class BikeManagement {
@httpPost
    global static bike__c doPost(){
        
        Map<String,String> urlParamsMap = RestContext.request.params;
        String bikename = urlParamsMap.get('Name');
        String cc= urlParamsMap.get('Engine_Displacement__c');
        String hp = urlParamsMap.get('Power__c');
        String Nm = urlParamsMap.get('Torque__c');
        String company = urlParamsMap.get('Company_Make__c');  
        String Gear = urlParamsMap.get('Transmission__c');
        String tank = urlParamsMap.get('Fuel_Tank_Capacity__c');
        bike__c postedbike =  new bike__c(Name=bikename,
                                          Engine_Displacement__c=cc,
                                          Power__c=hp,
                                          Torque__c=Nm,
                                          Company_Make__c=company,
                                          Transmission__c=Gear,
                                          Fuel_Tank_Capacity__c=tank);
        insert postedbike;                
        return postedbike;
    }
}

I have tried using below request data in rest explorer

Method -Post
URL - /services/apexrest/v1/BikeManagement/
body -
{
"Name":"Continental GT 650"
"Engine_Displacement__c":"648.9"
"Power__c":"47"
"Torque__c":"53"
"Company_Make__c":"Royal Enfield"
"Transmission__c":"6 Speed"
"Fuel_Tank_Capacity__c":"13"
}
  • Are you sure you are not receiving null/blank values? – d_k Jul 15 at 13:07
  • I have checked it using debug statements and get to know that myurlParamsMap is having null. So yes. i am getting null values, But i didnt understand why. @d_k Can you please help me? – Rajesh Banglore Jul 15 at 13:14
  • I have changed the post method and input values in request body @httpPost global static bike__c doPost(String bikename,String cc,String hp,String Nm,String company, String Gear,String tank){ bike__c postedbike = new bike__c(Name=bikename, Engine_Displacement__c=cc, Power__c=hp, Torque__c=Nm, Company_Make__c=company, Transmission__c=Gear, Fuel_Tank_Capacity__c=tank); system.debug('postedbike'+ postedbike); } – Rajesh Banglore Jul 15 at 13:36
2

RestContext.request.params are for Query String parameters (e.g. /services/apexrest/v1/BikeManagement?Name=Continental+GT+650&Engine_Displacement__c=648.9&...).

If you want to use your current design, you must set the Content-Type header to application/x-www-form-urlencoded and change the payload as follows:

Name=Continental+GT+650&Engine_Displacement__c=648.9&Power__c=47&Torque__c=53&Company_Make__c=Royal+Enfield&Transmission__c=6+Speed&Fuel_Tank_Capacity__c=13

Otherwise, if you want JSON, change your code to the following:

Map<String, String> urlParamsMap = (Map<String, Object>)JSON.deserializeUntyped(RestContext.request.getBody());

Note that you need to fix your JSON in that case (you're missing commas):

{
"Name":"Continental GT 650",
"Engine_Displacement__c":"648.9",
"Power__c":"47",
"Torque__c":"53",
"Company_Make__c":"Royal Enfield",
"Transmission__c":"6 Speed",
"Fuel_Tank_Capacity__c":"13"
}

Other alternatives are also possible, such as using:

{
    "request": {
        "bike": {
            "Name": "Continental GT 650",
            "Engine_Displacement__c": "648.9",
            "Power__c": "47",
            "Torque__c": "53",
            "Company_Make__c": "Royal Enfield",
            "Transmission__c": "6 Speed",
            "Fuel_Tank_Capacity__c": "13"
        }
    }
}

And changing your method signature to:

@httpPost
global static bike__c doPost(Bike__c bike){

This provides automatic deserialization for you, and supports XML and JSON without any modification to your code (I'd recommend this, personally).

| improve this answer | |
  • Hi there! I've had the issue where I get the required field missing error on a specific field on a custom object. This field was deployed as a required field but was changes to not being required after a day. However, we're still getting this error once in a while on new records being inserted. I haven't been able to find out why this is happening. Please help if you have ideas as to what may be causing this. Thanks! – Bahman.A Sep 17 at 20:07

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.