如何将来自数据库源的 GUID 与来自 Queryexpression 的 GUID 匹配

How to Matching GUID from Database Sources with GUID from Queryexpression

我要求每 30 分钟创建一次调度程序。此调度程序包含一个程序,该程序将获取今天创建的记录(包括 GUID),然后使用 Web 服务将其插入 Dynamics CRM。要从 SQL 服务器数据库中获取记录,我使用此查询:

SELECT * FROM new_crmtable WHERE DATEDIFF(day, CreatedOn, GetDate()) = 0

因为这个 shceduler,是 运行 每 30 分钟。每次执行此程序时,都会查看今天是否创建了新记录和旧记录。 例如,一条名为 A 的记录创建于今天中午 12 点 25 分。该程序第一次执行是在中午 12 点 30 分。然后在中午 12 点 45 分。创建了一个名为 B 的新记录。当程序在下午 13 点再次执行时。该程序同时获取记录 A 和 B。在 Dynamics CRM 中创建记录期间,它会给出一条错误消息。因为记录 A 已经创建。错误表明您不能使用重复的 GUID 创建记录(请记住,我还从 SQL 服务器数据库中获取 GUID,并使用其 GUID 插入 Dynamics CRM)

为避免错误,我需要检查是否已在 CRM 上创建记录。要从 Dynamics CRM 获取 GUID,我使用 QueryExpression。这是我的代码:

try
            {
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        try
                        {
                            Entity rs = new Entity("new_troubleticket");


                            OptionSetValue option = new OptionSetValue(100000001);
                            rs.Attributes.Add("new_systemsource", option);

                            Guid ticketid = new Guid(dt.Rows[i][0].ToString());
                            rs.Id = ticketid;


                            //FIRSTNAME
                            if (!string.IsNullOrEmpty(dt.Rows[i][1].ToString()))
                            {
                                rs["new_subject"] = dt.Rows[i][1].ToString();


            }
                                else
                                {
                                    rs["new_subject"] = null;
                                }

Queryexpression query = new QueryExpression { Entityname = "new_customentity", 
Columnset = new Columnset("new_customentityid")};
query.Criteria.AddCondition("CreatedOn", ConditionalOperator.Equal, Datetime.Now);

Entity Collection result = service.RetrieveMultiple(query)

你能告诉我如何匹配来自 SQL 服务器数据库的 GUID 和来自 Queryexpression 的 GUID 吗?

只需添加条件以按 id 过滤:

var g = Guid.NewGuid() //Replace this with your actual Id from the query
Queryexpression query = new QueryExpression { Entityname = "new_customentity", 
Columnset = new Columnset("new_customentityid")};
query.Criteria.AddCondition("new_customentityid", ConditionalOperator.Equal, g);

Entity Collection result = service.RetrieveMultiple(query)