使用C#SalesForce中的Upsert函数绑定自定义对象中的链接?

时间:2011-07-15 02:05:06

标签: c# salesforce

我有以下代码在Salesforce中创建一个Task,然后跟踪用户的浏览历史记录并将其存储在SalesForce中。目前,它将用户浏览的每个页面显示为单个条目。我想在Browsing_History__c对象中将所有这些条目组合在一起,而不是每次用户访问页面时都创建任务。

任何帮助都会受到赞赏。我对SF不太熟悉。 :)

private void CreateTaskInSF(string id, string type, string details, string description)
    {
        // if there's a similar Event in the past 2 hours, don't add it
        QueryResult qr = null;
        try // get events from past 2 hours
        {
            qr = Binding.query("Select Details__c from Task WHERE WhoId='" + id + "' and Type__c='" + type + "' and CreatedDate > " + DateTime.UtcNow.AddHours(-2).ToString("s") + "Z");
        }
        catch (Exception e)
        {
            return;
        }
        bool logged = false;
        if (qr != null) // if there are Tasks in past 2 hours
        {
            sforce.sObject[] browsing = qr.records;
            if (browsing != null)
            {
                // iterate through events to make sure the new Task isn't logged
                for (int i = 0; i < browsing.Length; i++)
                {
                    Task currTask = (Task)browsing[i];
                    if (currTask.Details__c == details)
                    {
                        if (description != "") // is there a description to check for?
                        {
                            string oldTaskDescription = "";
                            if (currTask.Description != null)
                                oldTaskDescription = currTask.Description;
                            if (oldTaskDescription == description) // if there is a description match
                                logged = true;
                        }
                        else
                            logged = true; // there's no description, so check only on details field
                    }
                }
            }
        }
        if (logged == true)
        {
            return; // if Activity is already logged, don't log it again
        }

        else if (type == "Browsing")
        {
            QueryResult browsingQuery = null;
            try // get events from past 2 hours
            {
                browsingQuery = Binding.query("Select Web_Browsing__c from Task WHERE WhoId='" + id + "' and Subject='" + type + "' and Details__c='" + details + "' and CreatedDate > " + DateTime.UtcNow.AddHours(-2).ToString("s") + "Z");
            }
            catch
            {

            }

            Boolean createNewBrowsing = false;
            if (browsingQuery != null) // if there are Tasks in past 2 hours
            {
                sforce.sObject[] webBrowsing = browsingQuery.records;
                if (webBrowsing != null)
                {
                    //find correct object and update Browsing_History__c
                    //Binding.update

                }

                else
                {
                    createNewBrowsing = true;
                }

            }
            else
            {
                createNewBrowsing = true;
            }

            if (createNewBrowsing)
            {

                Web_Browsing__c newTask = new Web_Browsing__c();
                newTask.Lead__c = id;
                newTask.Browsing_History_255__c = details;
                newTask.Type__c = type;

                newTask.Browsing_History__c = details;
                newTask.CreatedDate = DateTime.Now;
                //if(type == "Browsing") newTask. = details;
                //SaveResult[] createResult = Binding.create(new sObject[] { newTask });

            try
            {
                SaveResult[] createResult = Binding.create(new sObject[] { newTask });
            }
            catch (Exception e)
            {
                return;
            }

            }

        }

        else
        {
            // if this new Activity isn't logged, then create a new Activity Task
            sforce.Task newTask = new sforce.Task();
            newTask.WhoId = id;
            newTask.Subject = type;
            newTask.Details__c = details;
            if (description != "") newTask.Description = description;
            newTask.Status = "Completed";
            newTask.Priority = "Normal";
            newTask.ActivityDate = DateTime.Now;
            newTask.ActivityDateSpecified = true;

            // insert it
            try
            {
                SaveResult[] createResult = Binding.create(new sforce.sObject[] { newTask });

            }
            catch (Exception e)
            {
                return;
            }
        }


    }

1 个答案:

答案 0 :(得分:1)

您需要更新查询以请求浏览历史记录对象并更新代码以创建浏览历史记录对象而不是任务。

如果您还没有,请查看Web Services API文档,它包含java / c#中queryingcreating的示例。