在c# SalesForce中使用Upsert函数在自定义对象中绑定链接

本文关键字:自定义 链接 对象 函数 绑定 SalesForce Upsert | 更新日期: 2023-09-27 18:02:14

我有以下代码,它在Salesforce中创建一个任务,然后跟踪用户的浏览历史并将其存储在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;
            }
        }

    }

在c# SalesForce中使用Upsert函数在自定义对象中绑定链接

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

如果你还没有,请查看Web服务API文档,其中有使用java/c#查询和创建的示例。