Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
1.7 kB
1
Indexable
Never
namespace Norriq.Crm5.NVA.Plugins.contact
{
    public class CreateChangeDivisionLogEntry : BasePluginVoid
    {
        protected override void BaseExecute()
        {
            if (context.MessageName != "Update") return;
            if (!targetEntity.Contains("nrq_privateaddress_divisionid")) return;
            if (GetValue<EntityReference>("nrq_privateaddress_divisionid") == null)
                return;
            if (!preImageEntity.Contains("nrq_privateaddress_divisionid")) throw new InvalidPluginExecutionException("No From Division found.");

            var contactRef = targetEntity.ToEntityReference();
            var toDivisionRef = targetEntity.GetAttributeValue<EntityReference>("nrq_privateaddress_divisionid");
            var fromDivisionRef = preImageEntity.GetAttributeValue<EntityReference>("nrq_privateaddress_divisionid");

            if (toDivisionRef.Id == fromDivisionRef.Id) return;

            var contact = service.Retrieve(contactRef.LogicalName, contactRef.Id, new ColumnSet("fullname"));
            var toDivision = service.Retrieve(toDivisionRef.LogicalName, toDivisionRef.Id, new ColumnSet("nrq_name"));

            var memberRelocation = new Entity("nrq_memberrelocation");
            memberRelocation["nrq_name"] = $"{contact.GetAttributeValue<string>("fullname")} - {fromDivisionRef.Name} - {toDivision.GetAttributeValue<string>("nrq_name")}";
            memberRelocation["nrq_todivisionid"] = toDivisionRef;
            memberRelocation["nrq_fromdivisionid"] = fromDivisionRef;
            memberRelocation["nrq_contactid"] = contactRef;
            service.Create(memberRelocation);
        }
    }