Exemplo n.º 1
0
        /* ---------------------------------------------------------Generic SForce Routines - need to clean up and merge with above */
        //Add Data to the Dataset - need to call recursilvely
        private void AddData(SForceEdit.SObjectDef sobj, DataRow rw, System.Xml.XmlElement x, string ParentName)
        {
            if (ParentName != "") ParentName += "_";

            if (x.HasAttributes && x.Attributes["xsi:type"] != null && x.Attributes["xsi:type"].Value == "sf:sObject")
            {
                string tempname = ParentName + x.LocalName;
                for (int k = 0; k < x.ChildNodes.Count; k++)
                {
                    System.Xml.XmlElement xchild = ((System.Xml.XmlElement)x.ChildNodes[k]);
                    if (xchild.HasAttributes && xchild.Attributes["xsi:type"] != null && xchild.Attributes["xsi:type"].Value == "sf:sObject")
                    {
                        AddData(sobj, rw, xchild, tempname);
                    }
                    else
                    {
                        //Check if the table contains the field if it does add it in
                        string fldname = tempname + "_" + xchild.LocalName;
                        if (rw.Table.Columns.Contains(fldname))
                        {
                            rw[fldname] = GetDataColumnData(fldname, xchild.InnerText);
                        }
                        else
                        {
                            //if the column is not there add it in if its in the columns objects
                            if (sobj.FieldExists(fldname))
                            {
                                string fldtype = sobj.GetField(fldname).DataType;
                                System.Data.DataColumn c = new DataColumn(fldname, GetDataColumnType(fldtype));
                                rw.Table.Columns.Add(c);
                                rw[fldname] = GetDataColumnData(fldname, xchild.InnerText);
                            }
                        }
                    }
                }
            }
            else
            {
                if (rw.Table.Columns.Contains(x.LocalName))
                {
                    string fldtype = sobj.GetField(x.LocalName).DataType;
                    rw[x.LocalName] = GetDataColumnData(fldtype, x.InnerText);
                }
                else
                {
                    //if the column is not there add it in if its in the columns objects
                    if (sobj.FieldExists(x.LocalName))
                    {
                        string fldtype = sobj.GetField(x.LocalName).DataType;
                        System.Data.DataColumn c = new DataColumn(x.LocalName, GetDataColumnType(fldtype));
                        rw.Table.Columns.Add(c);
                        rw[x.LocalName] = GetDataColumnData(fldtype, x.InnerText);
                    }
                }
            }
        }
Exemplo n.º 2
0
        //Given a DataRow, update or Create the SalesForce Object
        //Assuming that we have just one row, easy to change to handle multiples
        public DataReturn Save(SForceEdit.SObjectDef sObj, DataRow dRow)
        {
            DataReturn dr = new DataReturn();

            sObject s = new sObject();
            s.type = sObj.Name;
            string id = "";

            if (dRow["Id"] == null || dRow["Id"].ToString() == "")
            {
                //new
                int fldCount = dRow.Table.Columns.Count - 1;
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                System.Xml.XmlElement[] o = new System.Xml.XmlElement[fldCount];

                fldCount = 0;

                List<string> fieldsToNull = new List<string>();

                foreach (DataColumn dc in dRow.Table.Columns)
                {

                    //Get the field definition
                    SForceEdit.SObjectDef.FieldGridCol f = sObj.GetField(dc.ColumnName);

                    // this is a new record so do it even if it says its readonly but exclud any _Name or _Type
                    if (!f.Create)
                    {
                        //nada ...
                    }
                    else if (dc.ColumnName == "Id")
                    {
                        //Nothing!
                    }
                    else if (dc.ColumnName == "type")
                    {
                        //don't do anything - this happens when we have the type field from a join
                    }
                    else
                    {

                        object val = dRow[dc.ColumnName];
                        if (dRow[dc.ColumnName] == DBNull.Value)
                        {
                            fieldsToNull.Add(dc.ColumnName);
                        }
                        else
                        {
                            o[fldCount] = doc.CreateElement(dc.ColumnName);

                            string sval = "";
                            if (f.DataType == "datetime")
                            {
                                sval = ((DateTime)val).ToString("o");
                            }
                            else if (f.DataType == "date")
                            {
                                sval = ((DateTime)val).ToString("yyyy-MM-dd");
                            }
                            else
                            {
                                sval = CleanUpXML(val.ToString());
                            }

                            o[fldCount].InnerText = sval;
                            fldCount++;
                        }

                    }
                }

                try
                {
                    // dont need to set the values to Null! this is a create so just don't tell them
                    // s.fieldsToNull = fieldsToNull.ToArray();
                    s.Any = Utility.SubArray<System.Xml.XmlElement>(o, 0, fldCount);
                    sfPartner.SaveResult[] sr = _binding.create(new sObject[] { s });
                    Globals.Ribbons.Ribbon1.SFDebug("Save>" + s.type);

                    for (int j = 0; j < sr.Length; j++)
                    {
                        if (sr[j].success)
                        {
                            dr.id = sr[j].id;
                        }
                        else
                        {
                            dr.success = false;
                            for (int i = 0; i < sr[j].errors.Length; i++)
                            {
                                dr.errormessage += (dr.errormessage == "" ? "" : ",") + sr[j].errors[i].message;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    dr.success = false;
                    dr.errormessage = ex.Message;
                }
            }
            else
            {
                //update
                int fldCount = dRow.Table.Columns.Count;
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

                System.Xml.XmlElement[] o = new System.Xml.XmlElement[fldCount];

                fldCount = 0;

                List<string> fieldsToNull = new List<string>();

                foreach (DataColumn dc in dRow.Table.Columns)
                {
                    //Get the field definition
                    SForceEdit.SObjectDef.FieldGridCol f = sObj.GetField(dc.ColumnName);

                    if (dc.ColumnName == "Id")
                    {
                        s.Id = dRow[dc.ColumnName].ToString();
                    }
                    else if (!f.Update)
                    {
                        //not on the list ...
                    }
                    else if (dc.ColumnName == "type")
                    {
                        //don't do anything - this happens when we have the type field from a join
                    }
                    else
                    {

                        object val = dRow[dc.ColumnName];
                        if (dRow[dc.ColumnName] == DBNull.Value ||
                            ((f.DataType != "string") && dRow[dc.ColumnName].ToString() == ""))
                        {
                            fieldsToNull.Add(dc.ColumnName);
                        }
                        else
                        {
                            o[fldCount] = doc.CreateElement(dc.ColumnName);

                            string sval = "";
                            if (f.DataType == "datetime")
                            {
                                sval = ((DateTime)val).ToString("o");
                            }
                            else if (f.DataType == "date")
                            {
                                sval = ((DateTime)val).ToString("yyyy-MM-dd");
                            }
                            else
                            {
                                sval = CleanUpXML(val.ToString());
                            }

                            o[fldCount].InnerText = sval;
                            fldCount++;
                        }
                    }
                }

                try
                {
                    s.fieldsToNull = fieldsToNull.ToArray();
                    s.Any = Utility.SubArray<System.Xml.XmlElement>(o, 0, fldCount);
                    sfPartner.SaveResult[] sr = _binding.update(new sObject[] { s });
                    Globals.Ribbons.Ribbon1.SFDebug("Update>" + s.type);
                    for (int j = 0; j < sr.Length; j++)
                    {
                        Console.WriteLine("\nItem: " + j);
                        if (sr[j].success)
                        {
                            dr.id = sr[j].id;
                        }
                        else
                        {
                            dr.success = false;
                            for (int i = 0; i < sr[j].errors.Length; i++)
                            {
                                dr.errormessage += (dr.errormessage == "" ? "" : ",") + sr[j].errors[i].message;
                            }
                        }
                    }

                }
                catch (Exception ex)
                {
                    dr.success = false;
                    dr.errormessage = ex.Message;
                }

            }
            return dr;
        }