function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Utpal MaityUtpal Maity 

Creating a salesorder in Salesforce by calling Rest API and data in XML Format

Hi ,

I am developing a custom integration application for creating sales orders in Salesforce. I am using xml for sending data in salesforce while calling the API. But I was unable to create the sales order in Sales force. The same data when I am sending using JSON is woking but with XNL it is not woking. I have converted the same JSON data to XML using the online tools but it is not working. 

My JSON eequest data is like this 

URL: https://ap2.salesforce.com/services/data/v30.0/commerce/sale/order 
 
 
HEADERS 
    Authorization: Bearer XXXXXXXXXXXXPANkBmIlOTVrgLcSJxArivqsXXX
    Content-Type : application/json 
 
 
REQUEST BODY 


   "order": [ 
   { 
      "attributes": { 
      "type": "Order" 
      }, 
      "EffectiveDate": "2015-04-11", 
      "Status": "Draft", 
      "billingCity": "SFO-Inside-OrderEntity-1", 
      "accountId": "00128000002l6q5AAA", 
      "Pricebook2Id": "01s28000000uD8IAAU", 
      "OrderItems": { 
         "records": [ 
            { 
            "attributes": { 
               "type": "OrderItem" 
            }, 
            "PricebookEntryId": "01u28000000eeirAAA", 
            "quantity": "1", 
            "UnitPrice": "15.99" 
            } 
         ] 
      } 
   } 
   ] 


and the XML request date is like this

REQUEST BODY


<order>
    <attributes>
        <type>order</type>
    </attributes>
    <EffectiveDate>2015-04-11</EffectiveDate>
    <Status>Draft</Status>
    <billingCity>SFO-Inside-OrderEntity-1</billingCity>
    <accountId>00128000002l6q5AAA</accountId>
    <Pricebook2Id>01s28000000uD8IAAU</Pricebook2Id>
    <OrderItems>
        <records>
            <attributes>
                <type>OrderItem</type>
            </attributes>
            <PricebookEntryId>01u28000000eeirAAA</PricebookEntryId>
            <quantity>1</quantity>
            <UnitPrice>15.99</UnitPrice>
        </records>
    </OrderItems>
</order>

But the xml is not working it is giving error 

An unexpected error occurred. Please include this ErrorId if you contact support: 697865704-94096 (-871941818)

Please suggest some wayout.

Thanks in advance
Anupam RastogiAnupam Rastogi
Hi Utpal,

I suggest that you use Workbench for testing the XML that you are sending to Salesforce. It will give you proper error message. That can help you correct it.
  1. Just go to: https://workbench.developerforce.com/login.php
  2. Log into it using your SFDC Org and then navigate to "Jump To -> REST Explorer"
  3. Select the method as POST
  4. Click Headers button and change the Content-Type and Accept to have XML instead of JSON
  5. Paste your input XML into the Request Body and update the URL to: /services/data/v30.0/commerce/sale/order
And you are done, just click the Execute button and see if you get any errors.

Thanks
AR

If you find the reply useful that helped solve the problem, then mark it as best answer.
Utpal MaityUtpal Maity
Hi Anupam,

I have tried the steps provided by you but it is not giving the detail error. It is returnning the error like this.

HTTP/1.1 500 Server Error Date: Wed, 08 Apr 2015 07:26:36 GMT Set-Cookie: BrowserId=nM5ljhJrQKmqSDf78pfl-A;Path=/;Domain=.salesforce.com;Expires=Sun, 07-Jun-2015 07:26:36 GMT Expires: Thu, 01 Jan 1970 00:00:00 GMT Sforce-Limit-Info: api-usage=53/15000 Content-Type: application/xml;charset=UTF-8 Transfer-Encoding: chunked
<?xml version="1.0" encoding="UTF-8"?> <Errors> <Error> <errorCode>UNKNOWN_EXCEPTION</errorCode> <message>An unexpected error occurred. Please include this ErrorId if you contact support: 1992864319-8481 (-871941818)</message> </Error> </Errors>
Anupam RastogiAnupam Rastogi
Set the body to the below and try...

<?xml version="1.0" encoding="UTF-8"?>
<order>
    <attributes>
        <type>order</type>
    </attributes>
    <EffectiveDate>2015-04-11</EffectiveDate>
    <Status>Draft</Status>
    <billingCity>SFO-Inside-OrderEntity-1</billingCity>
    <accountId>00128000002l6q5AAA</accountId>
    <Pricebook2Id>01s28000000uD8IAAU</Pricebook2Id>
    <OrderItems>
        <records>
            <attributes>
                <type>OrderItem</type>
            </attributes>
            <PricebookEntryId>01u28000000eeirAAA</PricebookEntryId>
            <quantity>1</quantity>
            <UnitPrice>15.99</UnitPrice>
        </records>
    </OrderItems>
</order>

Thanks

 
Anupam RastogiAnupam Rastogi
I hope that you are sending the Access Token along with the request.
Utpal MaityUtpal Maity
Hi Anupam,

I am sending the below headers with the request.

Content-Type: application/xml; charset=UTF-8
Accept: application/xml
Authorization: Bearer 00D28000000JOQU!AQYAQCzLLUQHVktJoBRwhbNu1WvrVxrLKNuDMC33a65S5dFMRTo7kCfy.qQevS3Opu7QtMD51C7qpL1xBkZQg

Thanks and Regards
Utpal Maity
Anupam RastogiAnupam Rastogi
See this access token might not work because you must have taken it from some other login call.

So what I was asking was whether in custom application code, from where you are calling salesforce REST API, you have extracted the access token first using the login call and then used it to call the REST API resource for creating sales order.

Can you please paste the code here from where you are calling salesforce REST API.
Utpal MaityUtpal Maity
Hi Anupam ,

The code is like this

string sfdcConsumerKey = "3MVG9ZL0ppGP5UrB7LcMsVM21rMIyJwaCTeWpe._4T5k.lWlJTt56kcJrdOmGUETdwhUnRN.jK";

        string sfdcConsumerSecret = "8760078wwww71430600114";

        string redirectURL = "https://login.salesforce.com/services/oauth2/callback";
        string sfdcToken = "BBrUsrwl2srrr0kNhrUrrrrr9DHjir";
        string sfdcUserName = "jdddd@gmail.com";
        string sfdcPassword = "xxxxxxx";

        string loginPassword = sfdcPassword + sfdcToken;
        string URI = "https://login.salesforce.com/services/oauth2/token";

        StringBuilder body = new StringBuilder();
        body.Append("grant_type=password&");
        body.Append("client_id=" + sfdcConsumerKey + "&");
        body.Append("client_secret=" + sfdcConsumerSecret + "&");
        body.Append("username=" + sfdcUserName + "&");
        body.Append("password = " + loginPassword);

        HttpClient authClient = new HttpClient();

        HttpContent content = new FormUrlEncodedContent(new Dictionary<string, string>
  {
     {"grant_type","password"},
     {"client_id",sfdcConsumerKey},
     {"client_secret",sfdcConsumerSecret},
     {"username",sfdcUserName},
     {"password",loginPassword}
   }
        );

        HttpResponseMessage message = authClient.PostAsync("https://login.salesforce.com/services/oauth2/token", content).Result;

        string responseString = message.Content.ReadAsStringAsync().Result;

        JObject obj = JObject.Parse(responseString);
        string oauthToken = (string)obj["access_token"];
        string serviceUrl = (string)obj["instance_url"];


        /// posting the order data
        /// 

        string xmldata = @"<order><attributes><type>Order</type></attributes><EffectiveDate>2015-04-11</EffectiveDate><Status>Draft</Status><billingCity>SFO-Inside-OrderEntity-1</billingCity><accountId>00128000002l6q5AAA</accountId><Pricebook2Id>01s28000000uD8IAAU</Pricebook2Id><OrderItems><records><attributes><type>OrderItem</type></attributes><PricebookEntryId>01u28000000eeirAAA</PricebookEntryId><quantity>1</quantity><UnitPrice>15.99</UnitPrice></records></OrderItems></order>";

        var elements = XElement.Parse(xmldata);


        xmldata = elements.ToString();
        HttpClient createClient = new HttpClient();
        content = new StringContent(xmldata, Encoding.UTF8, "application/xml");
        string uri = serviceUrl + "/services/data/v30.0/commerce/sale/order";
        HttpRequestMessage postrequest = null;
        postrequest = new HttpRequestMessage(HttpMethod.Post, uri);


        postrequest.Headers.Add("Authorization", "Bearer " + oauthToken);
        postrequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
        postrequest.Content = content;
        HttpResponseMessage postresponse = createClient.SendAsync(postrequest).Result;
        string responsedata = postresponse.Content.ReadAsStringAsync().Result;

Thanks and Regards
Utpal Maity
Anupam RastogiAnupam Rastogi
Hi Utpal,

I observe that the URL being used by you is 
     URL: https://ap2.salesforce.com/services/data/v30.0/commerce/sale/order

I do not think this is correct for using REST API. the URL for creating a new record for an object should be - 
     URL: https://ap2.salesforce.com/services/data/v30.0/sobjects/[ObjectName]/

So in your case it should be 
     URL: https://ap2.salesforce.com/services/data/v30.0/sobjects/Order/

And use xmldata with the following value and execute the code.

string xmldata = "<?xml version="1.0" encoding="UTF-8"?><order><attributes><type>Order</type></attributes><EffectiveDate>2015-04-11</EffectiveDate><Status>Draft</Status><billingCity>SFO-Inside-OrderEntity-1</billingCity><accountId>00128000002l6q5AAA</accountId><Pricebook2Id>01s28000000uD8IAAU</Pricebook2Id><OrderItems><records><attributes><type>OrderItem</type></attributes><PricebookEntryId>01u28000000eeirAAA</PricebookEntryId><quantity>1</quantity><UnitPrice>15.99</UnitPrice></records></OrderItems></order>";

Thanks
AR
 
Utpal MaityUtpal Maity
Hi Anupam,

When I am tried the latest solution given by you it is giving error like this

<?xml version="1.0" encoding="UTF-8"?> <Errors> <Error> <errorCode>INVALID_FIELD</errorCode> <message>No such column &apos;attributes&apos; on entity &apos;Order&apos;. If you are attempting to use a custom field, be sure to append the &apos;__c&apos; after the custom field name. Please reference your WSDL or the describe call for the appropriate names.</message> </Error> </Errors>

Thanks and regards
Utpal Maity
Anupam RastogiAnupam Rastogi
First of all, this error means that earlier we have been hitting the incorrect URL.

What fields are mandatory on an Order in your app? Just use them within the XML that you are sending.
Anupam RastogiAnupam Rastogi
Try using the following XML value.

string xmldata = "<?xml version="1.0" encoding="UTF-8"?><order><EffectiveDate>2015-04-11</EffectiveDate><Status>Draft</Status><billingCity>SFO-Inside-OrderEntity-1</billingCity><accountId>00128000002l6q5AAA</accountId><Pricebook2Id>01s28000000uD8IAAU</Pricebook2Id></order>";
 
Utpal MaityUtpal Maity
Hi Anupam,

I have tried previously with this and it is working but the problem is that when I am trying to send the order data with product it is not working. Then I have to send the product in seperate api call.

Thanks
Utpal
Anupam RastogiAnupam Rastogi
Is there a Master Detail relation between Product and Order?

What is the relationship name for Pricebook2 on Order?
 
Utpal MaityUtpal Maity
Hi Anupam,

I have no idea about a order and item relationship from salesforce API point of view . But why I want to upload order and items in one single API call is that suppose my order which I want to create in salesforce have 10 products so for creating that order i have to call webservices for 11 times. If we can send the products associated with the orders in one go the we donot have to call the api so many times.

Thanks and Regards
Utpal Maity
Anupam RastogiAnupam Rastogi
Try this


string xmldata = "<?xml version="1.0" encoding="UTF-8"?><order><EffectiveDate>2015-04-11</EffectiveDate><Status>Draft</Status><billingCity>SFO-Inside-OrderEntity-1</billingCity><accountId>00128000002l6q5AAA</accountId><Pricebook2Id>01s28000000uD8IAAU</Pricebook2Id><OrderItems><PricebookEntryId>01u28000000eeirAAA</PricebookEntryId><quantity>1</quantity><UnitPrice>15.99</UnitPrice></OrderItems></order>";
 
Utpal MaityUtpal Maity
Hi Anupam,

Order with item is not working . Previously I have tried this and now with the xml given by you I have tried but it is not working.

Thanks and Regards
Utpal Maity
Anupam RastogiAnupam Rastogi
Try this


string xmldata = "<?xml version="1.0" encoding="UTF-8"?><order><EffectiveDate>2015-04-11</EffectiveDate><Status>Draft</Status><billingCity>SFO-Inside-OrderEntity-1</billingCity><accountId>00128000002l6q5AAA</accountId><Pricebook2Id>01s28000000uD8IAAU</Pricebook2Id><OrderItems><PricebookEntryId>01u28000000eeirAAA</PricebookEntryId><quantity>1</quantity><UnitPrice>15.99</UnitPrice></OrderItems></order>";
 
Anupam RastogiAnupam Rastogi
What is the error?
Utpal MaityUtpal Maity
Hi ,

The error is 


HTTP/1.1 500 Server Error Date: Wed, 08 Apr 2015 10:19:54 GMT Set-Cookie: BrowserId=1yPMxn7xTqCaErErkv8QjA;Path=/;Domain=.salesforce.com;Expires=Sun, 07-Jun-2015 10:19:54 GMT Expires: Thu, 01 Jan 1970 00:00:00 GMT Sforce-Limit-Info: api-usage=91/15000 Content-Type: application/xml;charset=UTF-8 Transfer-Encoding: chunked
<?xml version="1.0" encoding="UTF-8"?> <Errors> <Error> <errorCode>UNKNOWN_EXCEPTION</errorCode> <message>An unexpected error occurred. Please include this ErrorId if you contact support: 510642957-38936 (-387431154)</message> </Error> </Errors>

Thanks and Regards
Utpal Maity
Anupam RastogiAnupam Rastogi
Hi Utpal,

As the REST API URL has the object for which we wish to create record, therefore it is not possible to create two different object's records. Like in your case you are trying to create records for both Order and Order Line Items. This is not possible using REST Api.

Instead you can use SOAP API to create Parent-Child relationship within the same call. So if you have 1 Order with 10 line items then within the same call you can create all 11 records having relationship.

So basically what you need to do is - 
1. Call the SOAP Api Login call with your username and password to retrieve the server URL and the sessionId
2. Use the sessionId to call the server URL with the XML having Order and Order Line Items.

So the syntax of the XML for say 1 Order having 2 Line Items should be like - 

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com" 
xmlns:urn1="urn:sobject.enterprise.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Header>
      <urn:SessionHeader>
                <urn:sessionId>[sessionId retrieved from the login() call]</urn:sessionId>         
      </urn:SessionHeader>
   </soapenv:Header>
   <soapenv:Body>
      <urn:create>
         <urn:sObjects xsi:type="urn1:Order">
  <EffectiveDate>2015-04-11</EffectiveDate> 
  <Status>Draft</Status> 
  <billingCity>SFO-Inside-OrderEntity-1</billingCity> 
  <accountId>00128000002l6q5AAA</accountId> 
  <Pricebook2Id>01s28000000uD8IAAU</Pricebook2Id> 
            <Order_Ext_Id__c>ABC-123</Order_Ext_Id__c>
         </urn:sObjects>
         <urn:sObjects xsi:type="urn1:OrderItems">
  <PricebookEntryId>01u28000000eeirAAA</PricebookEntryId> 
  <quantity>1</quantity> 
  <UnitPrice>15.99</UnitPrice> 
            <urn1:Order>
               <Order_Ext_Id__c>ABC-123</Order_Ext_Id__c>
            </urn1:Order>
         </urn:sObjects>
         <urn:sObjects xsi:type="urn1:OrderItems">
  <PricebookEntryId>01u28000000eeirBBB</PricebookEntryId> 
  <quantity>1</quantity> 
  <UnitPrice>20.99</UnitPrice> 
            <urn1:Order>
               <Order_Ext_Id__c>ABC-123</Order_Ext_Id__c>
            </urn1:Order>
         </urn:sObjects>
      </urn:create>
   </soapenv:Body>
</soapenv:Envelope>

`Here Order_Ext_Id__c is a new field on Order that needs to be created on Order which will help relate Orders with Line Items. Usually this field contains the row Id of Order from the Source system.

Thanks
AR
 
Utpal MaityUtpal Maity
Hi Anupam,

Thanks for your suggestion . So for this reason I need to go with seperate api call option for order and order products.

Thanks and Regards
Utpal Maity
Anupam RastogiAnupam Rastogi
No, you can use SOAP API's login and create calls.

A single create call will be able to create both Order and Order Products. Use the format of the sample request message I shared for creating the message containing all the Order Products for the Order.

Thanks
AR
Hari M 22Hari M 22
Hi Anupam,

I am trying to integrate SalesForce with my ERP system to sync data in both ways for Customers, Accounts, Orders, and Products.

I have gone through the SOAP AI document but was not much helpful.

Can you let me know where can I get the API document for SOAP API where I can have the details of URL and headers and XML Body which are needed to create records in SalesForce and also to get the list of records data?

Thanks,
Hari