Пример #1
0
        public async void flush()
        {
            if (body.records.Count == 0)
            {
                return;
            }

            String jsonBody = JsonConvert.SerializeObject(body, Formatting.None, new RecordObjectConverter());

            body = new SalesforceBody();

            String Url = InstanceUrl + "/services/data/" + ApiVersion + "/composite/tree/" + ParentObject;

            RestRequest request = new RestRequest(Url, Method.POST);

            request.AddHeader("Authorization", Token);
            request.AddHeader("Content-Type", "application/json");

            request.AddJsonBody(jsonBody);

            Thread.Sleep(500);
            IRestResponse response = await Client.ExecutePostAsync(request);

            RestSharp.Serialization.Json.JsonDeserializer deserializer = new RestSharp.Serialization.Json.JsonDeserializer();

            if (HttpStatusCode.Created == response.StatusCode)
            {
                SuccessResponse results = deserializer.Deserialize <SuccessResponse>(response);
                foreach (ResultSuccess result in results.results)
                {
                    Logger.Info(String.Format("Object Reference: {0} added with id: {1}", result.referenceId, result.id));
                }
            }
            else if (HttpStatusCode.BadRequest == response.StatusCode)
            {
                ErrorResponse errors = deserializer.Deserialize <ErrorResponse>(response);

                foreach (ResultError result in errors.results)
                {
                    String message = "";
                    message = String.Format("Object Reference: {0} has errors: ", result.referenceId);
                    foreach (Error error in result.errors)
                    {
                        message = message + error.message + " for fields [";
                        foreach (String errorMessage in error.fields)
                        {
                            message = message + errorMessage + ",";
                        }
                    }

                    message = message.Substring(0, message.Length - 1) + "]";
                    Logger.Error(message);
                }
            }
        }
Пример #2
0
        public void PreparePayload(String[] data, int line)
        {
            Dictionary <string, Dictionary <string, object> > body = new Dictionary <string, Dictionary <string, object> >();
            Record parent = new Record();

            int i = 0;

            foreach (String value in data)
            {
                //only if field was mapped
                if (Mapping.ContainsKey(i))
                {
                    MappingPayload.Mapping map = Mapping[i];

                    Dictionary <string, object> dataset = new Dictionary <string, object>();
                    dataset.Add(map.toColumn, value);

                    if (body.ContainsKey(map.toObject))
                    {
                        body[map.toObject].Add(map.toColumn, value);
                    }
                    else
                    {
                        body.Add(map.toObject, dataset);
                    }
                }
                i++;
            }

            parent.attributes.Add("type", ParentObject);
            parent.attributes.Add("referenceId", ParentObject + line.ToString());

            RestSharp.Serialization.Json.JsonSerializer serializer = new RestSharp.Serialization.Json.JsonSerializer();

            parent.fields = body[ParentObject];

            List <Record>  records  = new List <Record>();
            SalesforceBody children = new SalesforceBody();

            foreach (KeyValuePair <string, Dictionary <string, object> > entry in body)
            {
                if (entry.Key == ParentObject)
                {
                    continue;
                }

                Record child = new Record();

                child.attributes.Add("type", entry.Key);
                child.attributes.Add("referenceId", entry.Key + line.ToString());
                child.fields = entry.Value;

                children.records = records;
                parent.children.Add(Meta[entry.Key].labelPlural, new SalesforceBody(child));
            }

            this.body.records.Add(parent);

            if (this.body.records.Count >= BatchSize)
            {
                flush();
            }
        }