Integration of Google Forms with the 1C:Enterprise Platform to Accept Customer Orders

Alexander Biryukov

11.05.2023 14 min

1C-Google.png 

One of the most common tasks for 1C developers is the integration of an application written in the 1C:Enterprise language with a company's online store. That is, a company has an online store where they receive orders, and they want to process these orders using a product built on the 1C:Enterprise platform. 

What if for some reason we do not have a website to sell our goods through? And we still want to take orders via the Internet and process them in our 1C:Enterprise-based application. Are you saying that this is an impossible task? And I say not at all! You can easily accomplish it with the 1C:Enterpise platform-based application and a mechanism called Google Forms. 

If you have not come across Google Forms before, there is some useful information for you at https://www.google.com/forms/about/ 

In short, Google Forms is a mechanism that allows you to quickly conduct a questionnaire, survey, or some similar event. Also, among the Google Forms templates, there is a template for customer orders. Still, should we start using Google Forms out of the box, with no modifications, then all the information entered by users will be accumulated directly in Google Forms and only there. And we want to be able to retrieve this information that, in this case, is a customer order. So, we want to get this information directly in an application written in the 1C:Enterprise language. 

I want to clarify right away that using the mechanism described herein, you can implement not only the processing of orders through Google Forms but also conduct surveys of your customers, or place a job offer, and then receive candidate CVs via Google Forms and process all the data in an application built on the 1C:Enterprise platform. 

Well, let's go. Open Google Forms and create an order form there: 

Pic_1.png

 

Pic_2.png

Then open a new empty 1C configuration in Designer and create document Order with the following attributes:

 

Pic_3.png

To simplify the task, we will not use catalogs to store the lists of customers and goods. With this assumption in mind, we set the type of all attributes in document Order to String. We want to use Answer-ID from Google Forms as a document number. Thus, for document Order, we set the following numbering parameters:

Pic_4.png

Now we return to the previously created Google form and open the script editor:

Pic_5.png

The following window will open:

Pic_6.png

Here we replace the automatically generated code (function myFinction()) with the following:

var POST_URL = "";

function onSubmit(e) {

   var form = FormApp.getActiveForm();

   var allResponses = form.getResponses();

   var latestResponse = allResponses[allResponses.length - 1];

   var aID = latestResponse.getId();

   var response = latestResponse.getItemResponses();

   var payload = {};

   for (var i = 0; i < response.length; i++) {

       var question = response[i].getItem().getTitle();

       var answer = response[i].getResponse();

       payload[question] = answer;

   }

   var options = {

       "method": "post",

       "contentType": "application/json",

       "payload": JSON.stringify(payload),

       "headers" : {

         "x-form-answer-id": aID,

       }

   };

UrlFetchApp.fetch(POST_URL, options);

};

The idea behind the code is really simple: we retrieve the active form, then we retrieve the last entered response in this form. Additionally, we pick the response ID, which becomes the number of our document Order in our 1C application. Then we collect the rest of the data in the payload array, convert this array into a JSON structure, and pass it in the body of the POST query to the server. We also pass the response ID as the POST query header (x-form-answer-id).

As you can see, there is nothing complicated here.

You might have noticed that in the code above, there is one variable that is not initialized, namely, POST_URL. This variable should store the path to the server that will process our POST query. Now is the time to return to the 1C:Enterprise platform and create a code to process the data received from Google Forms. 

We go back to Designer and create an HTTP service and give it a name. Let’s call it google_forms: 

Pic_7.png

Let me remind you that we have already covered the subject of creating and using HTTP services in the 1C:Enterprise platform. Check part #1 https://1c-dn.com/blog/work-with-http-services-in-1c-part-1-get-method/ and part #2: https://1c-dn.com/blog/work-with-http-services-in-1c-part-2-post-method/ 

Therefore, we will save some time by skipping a detailed guide on how to implement an HTTP service in your application built on the 1C:Enterprise platform. 

So, we have created the google_forms HTTP service, then for this service we create an url_template called order with the following parameters: 

Pic_8.png

Then we add method POST:

Pic_9.png

To create a handler, we click the respective button. The HTTP service module will open with the handler already created: 

Pic_10.png

As you can see, by default, our service always returns 200 as a response. Let us replace the code with the following:

Function orderpost(Request)

           

      stringBodyRequest = Request.GetBodyAsString();

           

      mapOrder = JSONToStructure(stringBodyRequest);

           

      newOrder = Documents.Order.CreateDocument();

           

      newOrder.Date   = CurrentDate();

      newOrder.Number = Request.Headers.Get("x-form-answer-id");

           

      newOrder.DeliveryTo       = mapOrder.Get("Delivery to");

      newOrder.CustomerName        = mapOrder.Get("Your name");

      newOrder.CustomerEmail         = mapOrder.Get("E-mail");

      newOrder.Comments                = mapOrder.Get("Comments");

           

      arrayProducts = mapOrder.Get("What is the item you would like to order?");

           

      For Each curProduct In arrayProducts Do

                             

               newProduct = newOrder.Products.Add();

               newProduct.Product = curProduct;

                             

       EndDo;

           

       Response = New HTTPServiceResponse(200);

           

       Try

              newOrder.Write();

                             

              Response.SetBodyFromString("OK");

       Except

              Response.StatusCode = 500;

              Response.SetBodyFromString(ErrorDescription());

       EndTry;

           

       Return Response;

           

EndFunction

In brief, this code operates as follows: when receiving a POST query from an external source, we try to convert the data we have received in JSON format into a structure that contains the customer order data. Then, we want to use this structure to create document Order. If no error occurs, then the function returns code 200. If there is an error, then code 500 and the error details are returned. 

Ok, we have created the program code for the POST query handler. Now it is time to publish our 1C application on the web server. Here I will also save some time by directing you to the existing article: https://1c-dn.com/blog/work-with-http-services-in-1c-part-1-get-method/ 

So, we publish our 1C application on the web server:

Pic_11.png

Basically, it is all we had to do in the 1C:Enterprise platform. Now we return to the Google Forms’ script editor, and modify the script to initialize the POST_URL variable:

Pic_12.png

I remind you that the POST_URL variable must direct to the address on the web server where your 1C application is published. 

It seems everything is ready to operate. Just one finishing touch. We need to set up a trigger so that when you click the Submit button on Google Forms, the data gets saved not only in the form itself but also sent for processing to the 1C:Enterprise platform.

To do this, select the Triggers menu:

Pic_13.png

And create a new trigger with the following parameters:

Pic_14.png

Now everything is ready for testing!
Open our previously created Google form, fill it out, and click Submit:

Pic_15.png

If we set everything up correctly, then a new document Order gets added to the platform: 

Pic_16.png

My congratulations! We have just set up a feature to accept orders in the 1C:Enterprise platform via the web and did not use any website for this.
Obviously, the given example is very simple, and, I dare say, primitive. Still, it demonstrates a basic concept of how it is possible to get the desired result by combining different services in a non-standard way. Just like in our example about receiving orders via the web. 

Is there anything in this example we can improve? Definitely. There is certainly a huge potential for improvement! First, it is necessary to place in catalogs all data like clients, goods, and similar. Second, in this example there is no check that such an order already exists, that is, every time you edit a previously entered Google form, a new order is created. In fact, there are a lot of things you can do. The only limit is your imagination! 

So, let us sum it up. We implemented the feature of accepting orders inside a 1C:Enterprise platform-based application via the Google Forms mechanism of creating orders. The main advantage of this method is the ease of deployment. This method allows you to start accepting orders in 1C applications without building a website and seeking web developers’ assistance. Additionally, we have seen that the capabilities of the 1C:Enterprise platform allow it to interact with various services, including even Google Forms, that for some developers can be a great surprise.

But trust me, we have barely scratched the surface! Stay with us as there is plenty of fantastic stuff ahead! 

As always, the source code of the 1C application and the script for the Google Forms can be downloaded at:

https://1c-dn.com/developer_tools/integration_of_google_forms_with_the_1c/

Be the first to know tips & tricks on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.