Добрый день! Workflow генерирует для опроса токены, всё работает, но вот никак не могу сделать проверку, чтобы если уже есть такой токен в списке, то не нужно его опять генерировать, а то получается что дубликат делается. Код ниже:
X++:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace Bum.Survey.CRM.WorkflowActivity
{
public class GenerateTokens : CodeActivity
{
protected override void Execute(CodeActivityContext activityContext)
{
IExecutionContext context = activityContext.GetExtension<IExecutionContext>();
IOrganizationServiceFactory serviceFactory = activityContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService _orgService = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = activityContext.GetExtension<ITracingService>();
Entity survey = _orgService.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true));
if (survey.GetAttributeValue<EntityReference>("bf_survey_singleval") != null)
{
Entity evalobject = _orgService.Retrieve(survey.GetAttributeValue<EntityReference>("bf_survey_singleval").LogicalName,
survey.GetAttributeValue<EntityReference>("bf_survey_singleval").Id,
new ColumnSet("regardingobjectid"));
if (evalobject.GetAttributeValue<EntityReference>("regardingobjectid") != null)
{
Entity semestraplanaieraksts = _orgService.Retrieve(evalobject.GetAttributeValue<EntityReference>("regardingobjectid").LogicalName,
evalobject.GetAttributeValue<EntityReference>("regardingobjectid").Id,
new ColumnSet(true));
QueryExpression qrystudprieks = new QueryExpression()
{
EntityName = "bf_st_semester_planrow",
ColumnSet = new ColumnSet(true)
};
qrystudprieks.Criteria.AddCondition("bf_st_semester_planro_semester_planw", ConditionOperator.Equal, semestraplanaieraksts.Id);
var prieksmeti = _orgService.RetrieveMultiple(qrystudprieks).Entities;
foreach (var prieksmets in prieksmeti)
{
if (prieksmets.GetAttributeValue<EntityReference>("bf_st_semester_planrow_semester_plan") != null)
{
Entity studsem = _orgService.Retrieve(prieksmets.GetAttributeValue<EntityReference>("bf_st_semester_planrow_semester_plan").LogicalName,
prieksmets.GetAttributeValue<EntityReference>("bf_st_semester_planrow_semester_plan").Id,
new ColumnSet(true));
if (studsem.GetAttributeValue<EntityReference>("bf_st_semester_plans_study_plan") != null)
{
Entity studplans = _orgService.Retrieve(studsem.GetAttributeValue<EntityReference>("bf_st_semester_plans_study_plan").LogicalName,
studsem.GetAttributeValue<EntityReference>("bf_st_semester_plans_study_plan").Id,
new ColumnSet(true));
if (studplans.GetAttributeValue<EntityReference>("bf_st_study_plans_student") != null)
{
Entity students = _orgService.Retrieve(studplans.GetAttributeValue<EntityReference>("bf_st_study_plans_student").LogicalName,
studplans.GetAttributeValue<EntityReference>("bf_st_study_plans_student").Id,
new ColumnSet(true));
Entity token = new Entity()
{
LogicalName = "bf_surveytoken"
};
token.Attributes.Remove("activityid");
token.Id = Guid.NewGuid();
string regardingobjecttype = "bf_survey";
token["bf_surveytoken_survey"] = new EntityReference(regardingobjecttype, survey.Id);
token["regardingobjectid"] = new EntityReference("bf_sudents", students.Id);
_orgService.Create(token);
}
}
}
}
}
}
}
}
}