Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (13.1k points)
reopened by
I want to upload file using a mobile sdk. So is there a way to upload file?

1 Answer

0 votes
by (26.7k points)
edited by

At first, you need to create the parent object, and to do that using the following:

request = RestRequest.getRequestForCreate(apiVersion, objectType, fields);

client.sendAsync(restRequest, new RestClient.AsyncRequestCallback() {...

After that, you can use the below code:

private void postImageAsAttachment(String parentId, String title) {

    Map<String, Object> fields = new HashMap<String, Object>();

    fields.put("Name", title);

    fields.put("ParentId", parentId);

    fields.put("Body", ImageHelper.getBase64FromImage(mCurrentPhotoPath));

    RestRequest request = null;

    try {

        request = RestRequest.getRequestForCreate(apiVersion, "Attachment", fields);

    } catch (Exception ex) {

        Log.d(TAG, "sendRequest: ", ex);

        Toast.makeText(MainActivity.this, "The file upload failed: " + ex.toString(), Toast.LENGTH_LONG).show();

    }

    client.sendAsync(request, new RestClient.AsyncRequestCallback() {...

I hope this will help.

Want to become an Salesforce Expert? Join Salesforce admin course now!!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...