/**
   * Called when "create" button is clicked.
   *
   * @param v
   */
  public void onCreateClick(View v) {
    String objectType =
        ((EditText) findViewById(R.id.create_object_type_text)).getText().toString();
    Map<String, Object> fields = parseFieldMap(R.id.create_fields_text);

    RestRequest request = null;
    try {
      request = RestRequest.getRequestForCreate(apiVersion, objectType, fields);
    } catch (Exception e) {
      printHeader("Could not build create request");
      printException(e);
      return;
    }
    sendRequest(request);
  }
  /**
   * Helper methods to create "count" of test records
   *
   * @param count
   * @return map of id to name for the created accounts
   * @throws Exception
   */
  protected Map<String, String> createRecordsOnServer(int count, String objectType)
      throws Exception {
    Map<String, String> idToValues = new HashMap<String, String>();
    for (int i = 0; i < count; i++) {

      // Request.
      String fieldValue = createRecordName(objectType);
      Map<String, Object> fields = new HashMap<String, Object>();
      // add more object type if need to support to use this API
      // to create a new record on server
      switch (objectType) {
        case Constants.ACCOUNT:
          fields.put(Constants.NAME, fieldValue);
          break;
        case Constants.OPPORTUNITY:
          fields.put(Constants.NAME, fieldValue);
          fields.put("StageName", "Prospecting");
          DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
          fields.put("CloseDate", formatter.format(new Date()));
          break;
        default:
          break;
      }

      RestRequest request =
          RestRequest.getRequestForCreate(
              ApiVersionStrings.getVersionNumber(targetContext), objectType, fields);

      // Response.
      RestResponse response = restClient.sendSync(request);
      assertNotNull("Response should not be null", response);
      assertTrue("Response status should be success", response.isSuccess());
      String id = response.asJSONObject().getString(LID);
      idToValues.put(id, fieldValue);
    }
    return idToValues;
  }
  private void getCase() {

    // Creating Case and save id in the running Activity

    if (activity.geteServiceAdministration() != null) {
      activity.setCaseFields(new HashMap<String, Object>());
      Map<String, Object> caseFields = activity.getCaseFields();
      caseFields.put("Service_Requested__c", activity.geteServiceAdministration().getID());
      caseFields.put("AccountId", activity.getUser().get_contact().get_account().getID());
      caseFields.put("RecordTypeId", activity.getCaseRecordTypeId());
      caseFields.put("Status", "Draft");
      caseFields.put("Type", "License Services");
      caseFields.put("Origin", "Mobile");
      caseFields.put(
          "License_Ref__c",
          activity.getUser().get_contact().get_account().get_currentLicenseNumber().getId());
      caseFields.put("isCourierRequired__c", true);
      activity.setCaseFields(caseFields);
      if (activity.getInsertedCaseId() != null && !activity.getInsertedCaseId().equals("")) {
        try {
          restRequest =
              RestRequest.getRequestForUpdate(
                  getActivity().getString(R.string.api_version),
                  "Case",
                  activity.getInsertedCaseId(),
                  activity.getCaseFields());
        } catch (IOException e) {
          e.printStackTrace();
        }
      } else {
        try {
          restRequest =
              RestRequest.getRequestForCreate(
                  getActivity().getString(R.string.api_version), "Case", activity.getCaseFields());
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      new ClientManager(
              getActivity(),
              SalesforceSDKManager.getInstance().getAccountType(),
              SalesforceSDKManager.getInstance().getLoginOptions(),
              SalesforceSDKManager.getInstance().shouldLogoutWhenTokenRevoked())
          .getRestClient(
              getActivity(),
              new ClientManager.RestClientCallback() {
                @Override
                public void authenticatedRestClient(final RestClient client) {
                  if (client == null) {
                    SalesforceSDKManager.getInstance().logout(getActivity());
                    return;
                  } else {
                    client.sendAsync(
                        restRequest,
                        new RestClient.AsyncRequestCallback() {
                          @Override
                          public void onSuccess(RestRequest request, RestResponse response) {
                            try {
                              JSONObject jsonObject = new JSONObject(response.toString());
                              activity.setInsertedCaseId(jsonObject.getString("id"));
                              // Getting other information Related to Created Case
                              try {
                                restRequest =
                                    RestRequest.getRequestForQuery(
                                        getString(R.string.api_version),
                                        SoqlStatements.getCaseNumberQuery(
                                            activity.getInsertedCaseId()));
                              } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                              }
                              client.sendAsync(
                                  restRequest,
                                  new RestClient.AsyncRequestCallback() {
                                    @Override
                                    public void onSuccess(
                                        RestRequest request, RestResponse response) {
                                      JSONObject jsonObject = null;
                                      Utilities.dismissLoadingDialog();
                                      try {
                                        jsonObject = new JSONObject(response.toString());
                                        JSONArray jsonArray =
                                            jsonObject.getJSONArray(JSONConstants.RECORDS);
                                        JSONObject jsonRecord = jsonArray.getJSONObject(0);
                                        Log.d("resultcase", response.toString());
                                        activity.setCaseNumber(jsonRecord.getString("CaseNumber"));
                                      } catch (JSONException e) {
                                        e.printStackTrace();
                                      }
                                    }

                                    @Override
                                    public void onError(Exception exception) {}
                                  });
                              Log.d("", response.toString());

                            } catch (JSONException e) {
                              e.printStackTrace();
                            }
                          }

                          @Override
                          public void onError(Exception exception) {
                            VolleyError volleyError = (VolleyError) exception;
                            NetworkResponse response = volleyError.networkResponse;
                            String json = new String(response.data);
                            Log.d("", json);
                            Utilities.dismissLoadingDialog();
                            getActivity().finish();
                          }
                        });
                  }
                }
              });
    }
  }