Quantcast
Channel: Forum CRM Development
Viewing all articles
Browse latest Browse all 1000

CRM 2011 Plug-in Update (Post-Oper): retrieving and updating a single record

$
0
0

On Update of an <account> record, the plug-in must update a related record (new_port) having a one-to-one relationship with account through new_accountid.

The code below doesn't do it!
I debugged and the id parameter received by the method is correct.
Also, new_port was created as a custom Activity entity.

Any idea what am I doing wrong?

        private static void UpdateAccountPort(IOrganizationService service, Guid id)
        {
            try
            {
                using (var crm = new XrmServiceContext(service))
                {
                    var account = crm.AccountSet.Where(
                        a => a.AccountId == id).First();

                    var port = crm.new_portSet.Where(
                        p => p.new_accountid.Equals(id)).First();

                    port.Subject = account.Name;
                    port.new_territoryid = account.TerritoryId;
                    port.new_relationshiptypecode = account.CustomerTypeCode.Value;
                    port.new_accountorcontactownerid = account.OwnerId;
                    crm.Attach(port);
                    crm.UpdateObject(port);

                    crm.SaveChanges();
                }
            }
            catch (FaultException ex)
            {
                throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
            }
        }


Frank


Viewing all articles
Browse latest Browse all 1000

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>