Skip to content

Commit 0e03078

Browse files
committed
Add Create Message code
1 parent 7711621 commit 0e03078

File tree

1 file changed

+61
-6
lines changed

1 file changed

+61
-6
lines changed

CCOF.Infrastructure.Plugins/App Status History/AppStatusHistory.cs

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,61 @@ void IPlugin.Execute(IServiceProvider serviceProvider)
8383
switch (context.MessageName)
8484
{
8585
case "Create":
86+
postStatus = entity.GetAttributeValue<OptionSetValue>("statuscode");
87+
tracingService.Trace($"Choice Value: {postStatus.Value}");
88+
89+
var request = new RetrieveAttributeRequest
90+
{
91+
EntityLogicalName = entity.LogicalName,
92+
LogicalName = "statuscode",
93+
RetrieveAsIfPublished = true
94+
};
95+
96+
var response = (RetrieveAttributeResponse)service.Execute(request);
97+
var attributeMetadata = (EnumAttributeMetadata)response.AttributeMetadata;
98+
99+
var postOption = attributeMetadata.OptionSet.Options.FirstOrDefault(opt => opt.Value == postStatus.Value);
100+
string preChoiceLabel = string.Empty;
101+
string postChoiceLabel = string.Empty;
102+
if (postOption != null)
103+
{
104+
postChoiceLabel = postOption.Label.UserLocalizedLabel.Label;
105+
tracingService.Trace($"Choice Label: {postChoiceLabel}");
106+
}
107+
else
108+
{
109+
tracingService.Trace("Choice value not found in metadata.");
110+
}
111+
112+
statusHistoryRecord = new Entity("ccof_applicationstatushistory");
113+
statusHistoryRecord["ccof_logdate"] = DateTime.UtcNow;
114+
statusHistoryRecord["ccof_lookuptable"] = entity.LogicalName;
115+
statusHistoryRecord["ccof_statusvalue"] = (int)postStatus.Value;
116+
statusHistoryRecord["ccof_statuslabel"] = postChoiceLabel;
117+
statusHistoryRecord["ccof_messagetype"] = new OptionSetValue(100000000);
118+
statusHistoryRecord["ccof_postownerid"] = new EntityReference(
119+
postOwner.LogicalName,
120+
postOwner.Id
121+
);
122+
statusHistoryRecord["ccof_application"] = new EntityReference(
123+
"ccof_application",
124+
postApp.Id
125+
);
126+
statusHistoryRecord["ccof_statushistoryregardingid"] = new EntityReference(
127+
entity.LogicalName,
128+
entity.Id
129+
);
130+
statusHistoryRecord["ccof_operationuser"] = new EntityReference(
131+
"systemuser",
132+
currentUserId
133+
);
134+
statusHistoryRecord["ccof_account"] = new EntityReference(
135+
"account",
136+
account.Id
137+
);
138+
Guid recordId = service.Create(statusHistoryRecord);
139+
tracingService.Trace($"Record created successfully with ID: {recordId}");
140+
86141
break;
87142

88143
case "Update":
@@ -107,20 +162,20 @@ void IPlugin.Execute(IServiceProvider serviceProvider)
107162
postStatus = entity.GetAttributeValue<OptionSetValue>("statuscode");
108163
tracingService.Trace($"Choice Value: {postStatus.Value}");
109164

110-
var request = new RetrieveAttributeRequest
165+
request = new RetrieveAttributeRequest
111166
{
112167
EntityLogicalName = entity.LogicalName,
113168
LogicalName = "statuscode",
114169
RetrieveAsIfPublished = true
115170
};
116171

117-
var response = (RetrieveAttributeResponse)service.Execute(request);
118-
var attributeMetadata = (EnumAttributeMetadata)response.AttributeMetadata;
172+
response = (RetrieveAttributeResponse)service.Execute(request);
173+
attributeMetadata = (EnumAttributeMetadata)response.AttributeMetadata;
119174

120175
var preOption = attributeMetadata.OptionSet.Options.FirstOrDefault(opt => opt.Value == preStatus.Value);
121-
var postOption = attributeMetadata.OptionSet.Options.FirstOrDefault(opt => opt.Value == postStatus.Value);
122-
string preChoiceLabel = string.Empty;
123-
string postChoiceLabel = string.Empty;
176+
postOption = attributeMetadata.OptionSet.Options.FirstOrDefault(opt => opt.Value == postStatus.Value);
177+
preChoiceLabel = string.Empty;
178+
postChoiceLabel = string.Empty;
124179
if (preOption != null)
125180
{
126181
preChoiceLabel = preOption.Label.UserLocalizedLabel.Label;

0 commit comments

Comments
 (0)