I'm writing a console app to upload file .csv into CRM. Here is my code:
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);
}
}
And get systemuser:
static EntityReference getSystemUser(CrmServiceClient crmSvc)
{
WhoAmIRequest systemUserRequest = new WhoAmIRequest();
WhoAmIResponse systemUserResponse = (WhoAmIResponse)crmSvc.Execute(systemUserRequest);
Guid userId = systemUserResponse.UserId;
return new EntityReference("systemuser", userId);
}
But I get the error like this even I don't call any account. How to fix it? Bug
Thanks so much