4

I am developing an android app to create a new Quote in vTiger(ver 5.4) CRM server.

I was able to generate the new quote but the product_id and quantity that I sent for addition in quote details were not added in it. The other details are being shown in the new quote except the list of products, their quantities and pricing.

I have also studied the vTiger webservices tutorial but it was not helpful in this case.

I found an accepted answer of similar question but it is in php not in Android/JAVA.

This is how I am sending the details required to create a new quote in vTiger server.:-

            try {
                objectJson.put("subject", subject);
                objectJson.put("account_id", accountId);
                objectJson.put("bill_street", address); 
                objectJson.put("assigned_user_id", "19x1");
                objectJson.put("conversion_rate", "1.000"); 
                objectJson.put("currency_id", "21x1");  
                objectJson.put("hdnTaxType", "group");          
                objectJson.put("productid", productId); 
                objectJson.put("quantity", quantity);
            } 
            catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            String data = null;

            try {               
                data = URLEncoder.encode("sessionName", "UTF-8")
                        + "=" + URLEncoder.encode(sessionId, "UTF-8");
                data += "&" + URLEncoder.encode("element", "UTF-8") + "="
                        + URLEncoder.encode(objectJson.toString(), "ISO-8859-1");
                data += "&" + URLEncoder.encode("elementType", "UTF-8") + "="
                        + URLEncoder.encode(moduleName, "UTF-8");  //moduleName='Quotes'
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

             String text = "";
             BufferedReader reader=null;

             // Send data
                try
                {                 
                    // Defined URL  where to send data
                    URL url = new URL("http://vtiger_url/webservice.php?operation=create");

                    // Send POST data request
                    URLConnection conn = url.openConnection();
                    conn.setDoOutput(true);
                    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                    wr.write( data );
                    wr.flush();   
                 } 
                    catch(Exception ex)
                        {

                        }

The above code helps me to generate the quote without product details.

After studying the above mentioned php answer, I changed the URL that I was using in my code to this:- http://vtiger_url/webservice.php?total=23000&operation=create. This helped me to add the total amount to the newly created quote but I wasn't successful to add rest of the details using this method.

Community
  • 1
  • 1
Amit Anand
  • 1,225
  • 1
  • 16
  • 40

4 Answers4

1

The answer you have found seems to be suitable, even if it's a php solution. The problem you descirbed:

I have tried this too but unfortunately I am not able to access Inventoryproductrel table.

maybe already indicates a lack of proper authentication or missing privileges. So my suggestion is to

  1. Find out why you can't access the table Inventoryproductrel. Try to access that table using your authentication privileges (sessionID), examine the response for any hints and try to solve the table isn't accessible issue.
  2. Finally follow the suggestions from this answer.

Just another hint. If you can successfully access that table via you web browser, than I would sniff the request and have a look at the http parameters and their values. Based on those findings you can modify your request. Always consider, the web browser can only do the same as you android application.

Community
  • 1
  • 1
My-Name-Is
  • 4,814
  • 10
  • 44
  • 84
  • But I don't know `php` so I can't decode that answer for my use. – Amit Anand Aug 11 '14 at 07:56
  • @help Have done the first step of my suggestion? This isn't related to the php souce. – My-Name-Is Aug 11 '14 at 10:05
  • How it can be possible that the same table is accessible with same credentials by using web browser but not by android? – Amit Anand Aug 11 '14 at 10:10
  • @help You need to set an appropriate request to the destination service. If the table isn't accessible you will get an error message or a hint along with the response from the service. That the table isn't accessible via via your android application just indicates that there is something wrong with your request. If you want us to provide further support you need to post a workable sample that shows us the response messages or post the response details itself. – My-Name-Is Aug 11 '14 at 10:15
  • @help Please send the link which allows you to access the `Inventoryproductrel` table and insert records there. – My-Name-Is Aug 11 '14 at 11:12
1

These lines were added in existing code:-

JSONArray pdoInformation = new JSONArray();

try{

// Added these lines in try block to send product details

  for(int i=0; i<productIds.size(); i++) {
     JSONObject obj = new JSONObject();
     obj.put("productid", productIds.get(i) );
     obj.put("qty", quantities.get(i));
     obj.put("listprice", listprices.get(i));
     pdoInformation.put(obj);
       }                
            objectJson.put("pdoInformation", pdoInformation);     
}

Here product details were needed to be sent in a JSONArray with name as "pdoInformation".

for loop is used to send multiple product details.

Here productIds, quantities and listprices are three mandatory product details stored as ArrayList.

Amit Anand
  • 1,225
  • 1
  • 16
  • 40
0

Why don't you use the web service to create products as well? That should be supported as per the documentation. Once you create the products and get their ids, you can create a quote object that includes these ids. The objects and their fields are not very well documented for the rest APIs, so you could use the query/describe APIs to get as much information as possible about what data needs to be supplied for creating the different objects. From the description of the Quotes module, you would need to include item_details which will contain the product and quantity information. The exact field name and format can be obtained by the describe API as described in the web service documentation

To get a description of the vTiger objects

String modeleName = "Quotes"; //Use Products if you want a description of the Products module
String data = null;

try {               
    data = URLEncoder.encode("sessionName", "UTF-8")
                + "=" + URLEncoder.encode(sessionId, "UTF-8");
    data += "&" + URLEncoder.encode("elementType", "UTF-8") + "="
                + URLEncoder.encode(moduleName, "UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} 

String text = "";
BufferedReader reader=null;
System.out.println(data);
// Send data
try
{                 
    // Defined URL  where to send data
    URL url = new URL("http://vtiger_url/webservice.php?operation=describeobject");

    // Send GET data request
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write( data );
    wr.flush();   
} catch(Exception ex) {
}

Note that it's pretty much the same as what you are doing, only the url & parameters are different, and the request type is GET instead of POST To create a product, you would follow the same procedure as you did for the quote, only the url and parameters would be different

Sasikanth Bharadwaj
  • 1,457
  • 1
  • 11
  • 11
  • @help, added sample code to earlier answer, please check it out – Sasikanth Bharadwaj Aug 07 '14 at 09:58
  • This code is only for describing a module. I can do all the operations in vtiger except addition of product details in newly created quote :( Help me with that. – Amit Anand Aug 07 '14 at 10:01
  • If you want to update an existing quote object, you get the object and use the update operation. If you want to create a quote with the product information populated, you would need to create a product before hand and include it's id in the quote object you create. To know what are the parameter names and data types you should supply, you can use the describe operation, get all the information about the quotes object and then fill in the fields accordingly. – Sasikanth Bharadwaj Aug 07 '14 at 10:12
  • I have full details of products that I am sending to the quote generation process, I even checked what is required to create quote by using DESCRIBE but it also didn't worked and I also tried to update the generated quote with product details but it also didn't worked. – Amit Anand Aug 07 '14 at 10:16
0

@help as far i understand your question is how to send JsonObject with data to specified Server is that correct ? if it is the correct then i suggest you to use Volley library for networking and here are many examples that may useful to you .http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/ and http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

Just go through it. It provides easiest way to perform networking operations and also cancellation of request is also possible with this library.

Sample Code:

final String URL = "SERVER_URL";
// Post params to be sent to the server
HashMap<String, String> params = new HashMap<String, String>();
params.put("token", "AbCdEfGh123456");

JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
   new Response.Listener<JSONObject>() {
       @Override
       public void onResponse(JSONObject response) {
           try {
               VolleyLog.v("Response:%n %s", response.toString(4));
           } catch (JSONException e) {
               e.printStackTrace();
           }
       }
   }, new Response.ErrorListener() {
       @Override
       public void onErrorResponse(VolleyError error) {
           VolleyLog.e("Error: ", error.getMessage());
       }
   });

// add the request object to the queue to be executed
ApplicationController.getInstance().addToRequestQueue(req);

Using this code you can send your data to your server and at server side you can use php code to receive this jsondata and parse it.

Thanks.

Deep Shah
  • 1,334
  • 3
  • 20
  • 41
  • What is `ApplicationController` in last line? – Amit Anand Aug 11 '14 at 06:07
  • It is Singleton class which extends Application for obtaining Volley requestQueue . So , you can create RequestQueue instance whenever necessary throughout the application. This entire code is provided in http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/ . first just go through it. – Deep Shah Aug 11 '14 at 06:15
  • `java.lang.NoClassDefFoundError: com.android.volley.toolbox.JsonObjectRequest` – Amit Anand Aug 11 '14 at 07:59
  • have you import Volley as library project ? if not then follow this tutorial to work with volley : http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ – Deep Shah Aug 11 '14 at 08:01
  • I have added volley.jar to my project – Amit Anand Aug 11 '14 at 08:02
  • there must be problem with your Build Path of your project. have you check volley.jar in Order and Export tab? you can refer http://blog.lemberg.co.uk/volley-part-1-quickstart this. – Deep Shah Aug 11 '14 at 09:10
  • ooh yes I missed to check that option. Its running now but is showing NullPointerException at `ApplicationController.getInstance().addToRequestQueue(req);`. – Amit Anand Aug 11 '14 at 09:23
  • that code is perfectly working ... anyway you can create RequestQueue using following code : RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); – Deep Shah Aug 11 '14 at 09:26