Ejemplo n.º 1
0
        //New PES
        public DataReturn CloneAttachmentFile(string ParentId, string AttachmentName, string Xml)
        {
            DataReturn dr = new DataReturn();

            string id = "";

            String soqlQuery = "SELECT Id FROM Attachment where ParentId='" + ParentId + "' and Name='" + AttachmentName + "' order by LastModifiedDate desc limit 1";
            try
            {
                QueryResult qr = _binding.query(soqlQuery);

                if (qr.size > 0)
                {
                    sObject[] records = qr.records;
                    for (int i = 0; i < qr.records.Length; i++)
                    {
                        id = records[i].Any[0].InnerText;
                    }
                }

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

            sObject attach = new sObject();
            attach.type = "Attachment";
            System.Xml.XmlElement[] o;
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            SaveResult[] sr;

            if (id == "")
            {
                // Create the attacchments fields
                o = new System.Xml.XmlElement[4];
                doc = new System.Xml.XmlDocument();
                o[0] = doc.CreateElement("Name");
                o[0].InnerText = AttachmentName;

                o[1] = doc.CreateElement("isPrivate");
                o[1].InnerText = "false";

                o[2] = doc.CreateElement("ParentId");
                o[2].InnerText = ParentId;

                o[3] = doc.CreateElement("Body");
                byte[] data = Convert.FromBase64String(Xml);
                o[3].InnerText = Convert.ToBase64String(data);

                attach.Any = o;
                sr = _binding.create(new sObject[] { attach });

                for (int j = 0; j < sr.Length; j++)
                {
                    if (sr[j].success)
                    {
                        id = sr[j].id;
                    }
                    else
                    {
                        for (int i = 0; i < sr[j].errors.Length; i++)
                        {
                            dr.errormessage += (dr.errormessage == "" ? "" : ",") + sr[j].errors[i];
                        }
                    }
                }
            }
            else
            {
                // Update the attacchments fields
                doc = new System.Xml.XmlDocument();
                o = new System.Xml.XmlElement[1];
                o[0] = doc.CreateElement("Body");
                //  o[0].InnerText = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(Xml));
                byte[] data = Convert.FromBase64String(Xml);
                o[0].InnerText = Convert.ToBase64String(data);

                attach.Any = o;
                attach.Id = id;
                sr = _binding.update(new sObject[] { attach });

                for (int j = 0; j < sr.Length; j++)
                {
                    if (sr[j].success)
                    {
                        id = sr[j].id;
                    }
                    else
                    {
                        for (int i = 0; i < sr[j].errors.Length; i++)
                        {
                            dr.errormessage += (dr.errormessage == "" ? "" : ",") + sr[j].errors[i];
                        }
                    }
                }
            }

            dr.id = id;

            return dr;
        }
Ejemplo 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(string sObjectName, DataRow dRow)
        {
            DataReturn dr = new DataReturn();

            sObject s = new sObject();
            s.type = sObjectName;
            string id = "";
            List<string> fieldsToNull = new List<string>();

            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;
                foreach (DataColumn dc in dRow.Table.Columns)
                {
                    if (dc.ColumnName == "Id")
                    {
                        //Nothing!
                    }
                    else if (dc.ColumnName == "type" || dc.ColumnName == "LastModifiedDate" || dc.ColumnName == "CreatedDate")
                    {
                        //don't do anything - this happens when we have the type field from a join
                    }
                    else
                    {
                        if (dc.ColumnName.Contains("__r_"))
                        {
                            if (dc.ColumnName.EndsWith("Id"))
                            {

                                string tn = dc.ColumnName;
                                tn = tn.Substring(0, tn.IndexOf("__r_"));
                                //Concept__r_Id becomes Concept__c
                                tn += "__c";

                                o[fldCount] = doc.CreateElement(tn);
                                o[fldCount].InnerText = CleanUpXML(dRow[dc.ColumnName].ToString());
                                fldCount++;
                            }
                            //Otherwise do nothing
                        }
                        else
                        {
                            o[fldCount] = doc.CreateElement(dc.ColumnName);
                            o[fldCount].InnerText = CleanUpXML(dRow[dc.ColumnName].ToString());
                            fldCount++;
                        }
                    }
                }

                try
                {

                    s.Any = Utility.SubArray<System.Xml.XmlElement>(o, 0, fldCount);
                    SaveResult[] sr = _binding.create(new sObject[] { s });

                    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;
                foreach (DataColumn dc in dRow.Table.Columns)
                {
                    if (dc.ColumnName == "Id")
                    {
                        s.Id = dRow[dc.ColumnName].ToString();
                    }
                    else if (dc.ColumnName == "type" || dc.ColumnName == "LastModifiedDate" || dc.ColumnName == "CreatedDate")
                    {
                        //don't do anything - this happens when we have the type field from a join
                    }
                    else
                    {
                        //For relations - ignore all the other fields except the _Id one
                        //e.g. "Concept__r_Name" "Concept__r_Id" - ignore all but Id

                        //TODO: won't work Nested! need to try this out with a realation of a relation

                        if (dc.ColumnName.Contains("__r_"))
                        {
                            if (dc.ColumnName.EndsWith("Id"))
                            {

                                string tn = dc.ColumnName;
                                tn = tn.Substring(0, tn.IndexOf("__r_"));
                                //Concept__r_Id becomes Concept__c
                                tn += "__c";

                                string val = CleanUpXML(dRow[dc.ColumnName].ToString());
                                if (val == "")
                                {
                                    fieldsToNull.Add(dc.ColumnName);
                                }
                                else
                                {
                                    o[fldCount] = doc.CreateElement(tn);
                                    o[fldCount].InnerText = val;
                                    fldCount++;
                                }

                            }
                            //Otherwise do nothing
                        }
                        else
                        {
                            string val = CleanUpXML(dRow[dc.ColumnName].ToString());
                            if(val==""){
                                fieldsToNull.Add(dc.ColumnName);
                            } else{
                                o[fldCount] = doc.CreateElement(dc.ColumnName);
                                o[fldCount].InnerText = val;
                                fldCount++;
                            }
                        }

                    }
                }

                try
                {
                    s.fieldsToNull = fieldsToNull.ToArray();
                    s.Any = Utility.SubArray<System.Xml.XmlElement>(o, 0, fldCount);
                    SaveResult[] sr = _binding.update(new sObject[] { s });

                    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;
        }
Ejemplo n.º 3
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;
        }
Ejemplo n.º 4
0
        public DataReturn UpdateAttachmentFile(string Id,string AttachmentName,string FileName)
        {
            DataReturn dr = new DataReturn();

            byte[] b;

            try
            {
                b = System.IO.File.ReadAllBytes(FileName);
            }
            catch (Exception e)
            {
                dr.errormessage = e.Message;
                dr.success = false;
                return dr;
            }

            sObject attach = new sObject();
            attach.type = "Attachment";
            System.Xml.XmlElement[] o;
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            SaveResult[] sr;

                // Update the attacchments fields
                doc = new System.Xml.XmlDocument();
                o = new System.Xml.XmlElement[AttachmentName==""?1:2];

                o[0] = doc.CreateElement("Body");
                o[0].InnerText = Convert.ToBase64String(b);

                if (AttachmentName != "")
                {
                    o[1] = doc.CreateElement("Name");
                    o[1].InnerText = AttachmentName;
                }

                attach.Any = o;
                attach.Id = Id;
                try
                {
                    sr = _binding.update(new sObject[] { attach });
                    Globals.Ribbons.Ribbon1.SFDebug("Update Attachment");

                    for (int j = 0; j < sr.Length; j++)
                    {
                        if (sr[j].success)
                        {
                            Id = sr[j].id;
                        }
                        else
                        {
                            for (int i = 0; i < sr[j].errors.Length; i++)
                            {
                                dr.errormessage += (dr.errormessage == "" ? "" : ",") + sr[j].errors[i];
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    dr.errormessage = e.Message;
                    dr.success = false;
                    return dr;
                }

            dr.id = Id;

            return dr;
        }
Ejemplo n.º 5
0
 /// <remarks/>
 public void upsertAsync(string externalIDFieldName, sObject[] sObjects, object userState) {
     if ((this.upsertOperationCompleted == null)) {
         this.upsertOperationCompleted = new System.Threading.SendOrPostCallback(this.OnupsertOperationCompleted);
     }
     this.InvokeAsync("upsert", new object[] {
                 externalIDFieldName,
                 sObjects}, this.upsertOperationCompleted, userState);
 }
Ejemplo n.º 6
0
 /// <remarks/>
 public void upsertAsync(string externalIDFieldName, sObject[] sObjects) {
     this.upsertAsync(externalIDFieldName, sObjects, null);
 }
Ejemplo n.º 7
0
 /// <remarks/>
 public void updateAsync(sObject[] sObjects, object userState) {
     if ((this.updateOperationCompleted == null)) {
         this.updateOperationCompleted = new System.Threading.SendOrPostCallback(this.OnupdateOperationCompleted);
     }
     this.InvokeAsync("update", new object[] {
                 sObjects}, this.updateOperationCompleted, userState);
 }
Ejemplo n.º 8
0
 /// <remarks/>
 public void updateAsync(sObject[] sObjects) {
     this.updateAsync(sObjects, null);
 }
Ejemplo n.º 9
0
 /// <remarks/>
 public void createAsync(sObject[] sObjects) {
     this.createAsync(sObjects, null);
 }