Android: Salesforce -cannot upsert data into object

504 Views Asked by At

Hi Iam facing a hard time inserting data into salesforce objects , from my android app i want to insert data into a Sobject, implementing as below:

private void sendOrderRequest() throws UnsupportedEncodingException {
        final String externalIdField = "OrderExtID__c";
        final String externalId = "123";
        final String accountType = SalesforceSDKManager.getInstance().getAccountType();
        final LoginOptions loginOptions = SalesforceSDKManager.getInstance().getLoginOptions();

        new ClientManager(getActivity(), accountType, loginOptions, SalesforceSDKManager.getInstance().shouldLogoutWhenTokenRevoked()).getRestClient(getActivity(), new RestClientCallback() {

            @Override
            public void authenticatedRestClient(RestClient client) {
        if (client == null) {
        SalesforceSDKManager.getInstance().logout(getActivity());
        return;
            }


            // 'client' is a valid rest client.
            RestRequest restRequest = null;
           try {
               HashMap<String, Object> fields = new HashMap<String, Object>();
               fields.put(CREATEDBY, createdby.getText().toString());
               fields.put(CREATEDDATE, createddate.getText().toString());
               fields.put(ORDERID, orderid.getText().toString());
               fields.put(PRODUCTID, productid.getText().toString());
               fields.put(QUANTITY, quantity.getText().toString());
               fields.put(PRICE, price.getText().toString());
               fields.put(DESCRIPTION, description.getText().toString());

            restRequest = RestRequest.getRequestForUpsert(getString(R.string.api_version), getString(R.string.objecttype),externalIdField,externalId, fields);
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
         // restRequest = RestRequest.getRequestForDescribeGlobal(getString(R.string.api_version));



            client.sendAsync(restRequest, new AsyncRequestCallback() {
                @Override
                public void onSuccess(RestRequest request, RestResponse result) {
                    try {
                        System.out.println("successsss");

                        System.out.println("Result..printinf ===="+result.asJSONObject().getJSONArray("sobjects"));


                        EventsObservable.get().notifyEvent(EventType.RenditionComplete);

                    } catch (Exception e) {
                        onError(e);
                    }
                }

i am getting error:404, can someone point me in right direction, the getRequestForUpsert() asks for externalIdfeild and external id, am i passing the request coreect way?..ant help is appreciated

1

There are 1 best solutions below

0
On

If the problem is as stated, that your error message is suggesting that you have not provided an identified externalIdField nor an externalId, it is likely because you are attempting to perform an UPSERT operation via the REST request. An UPSERT is a Force.com Data Manipulation Language (DML) operation that will attempt to determine if the record (salesforce object) you are submitting is or is not already in the database by performing a lookup on its ID first. It allows such a lookup on either a salesforce ID (the record's PK) or an alternate candidate key field flagged as an ExternalId (typically from an external data table of record,) but also requires that you identify which field should be used as the key; the Id or an ExternalId field. If you only need to do an insert, try an alternate call to the getRequestForCreate() method instead. Below is a link to the REST documentation page for the RestRequest:

http://docs.developer.salesforce.com/docs/atlas.en-us.186.0.mobile_sdk.meta/mobile_sdk/android_native_classes_restrequest.htm