0

我正在编写一个控制台应用程序来将文件 .csv 上传到 CRM 中。这是我的代码:

 var hundredData = File.ReadAllLines(@"..\..\Data\ImportList100Data.csv");

        var dataList = from detailData in hundredData
                        let data = detailData.Split(';')
                        select new
                        {
                            GroupName = data[0],
                            ProjectNumber = data[1],
                            Name = data[2],
                            Members = data[3],
                            Customer = data[4],
                            ProjectStatus = data[5],
                            StartDate = data[6],
                            EndDate = data[7]
                        };
        var list = dataList.ToList();

        int totalData = list.Count();
        int successUpd = 0;
        int successCreate = 0;
        log.Info("There are " + (totalData - 1) + " items to be processed");

        var systemUser = getSystemUser(crmSvc);
        for (int i = 1; i < list.Count(); i++)
        {

            var data = list[i];
            DateTime startDate = DateTime.Parse(data.StartDate);
            DateTime endDate = DateTime.Parse(data.EndDate);
            int optSet;
            switch (data.ProjectStatus.ToString())
            {
                case "New":
                    optSet = 283630000;
                    break;
                case "Planned":
                    optSet = 283630001;
                    break;
                case "In Progress":
                    optSet = 283630002;
                    break;
                case "Finished":
                    optSet = 283630003;
                    break;
                default:
                    throw new Exception("Invalid choice");
            }
            var visa = addMem(crmSvc, data.Members);

            var proj = getProjID(context, data.ProjectNumber.ToString());
            elca_Project newProject = new elca_Project();

            newProject.new_ProjectGroup = QueryGroup(context, data.GroupName);
            newProject.elca_name = data.Name.ToString();
            newProject.elca_Customer = data.Customer.ToString();
            newProject.elca_StartDate = startDate;
            newProject.elca_EndDate = endDate;
            newProject.elca_ProjectStatus = new OptionSetValue(optSet);
            newProject.elca_Members = visa;
            newProject.OwnerId = systemUser;

            try
            {
                if (proj != null)
                {
                    newProject.Id = proj.Id;

                    crmSvc.Update(newProject);
                    successUpd++;
                    log.Info("Item " + i + "/" + (totalData - 1) + ": Project was updated");
                }
                else
                {
                    newProject.elca_ProjectNumber = data.ProjectNumber.ToString();

                    crmSvc.Create(newProject);
                    successCreate++;
                    log.Info("Item " + i + "/" + (totalData - 1) + ": Project was created");
                }
            }
            catch(Exception e)
            {
                log.Error("Item " + i + "/" + (totalData - 1) + ": Failed with error " + e.Message, e);
            }
        }

并获取系统用户:

    static EntityReference getSystemUser(CrmServiceClient crmSvc)
    {
        WhoAmIRequest systemUserRequest = new WhoAmIRequest();
        WhoAmIResponse systemUserResponse = (WhoAmIResponse)crmSvc.Execute(systemUserRequest);
        Guid userId = systemUserResponse.UserId;

        return new EntityReference("systemuser", userId);
    }

但即使我没有拨打任何帐户,我也会收到这样的错误。如何解决? 漏洞

非常感谢

4

0 回答 0