Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

how to get created record in plugin

Posted on by Microsoft Employee

Entity entity = (Entity)context.InputParameters["Target"];


if (entity.LogicalName == "account")
{

Entity Account = (Entity)service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true));
tracingService.Trace("1");

Guid ContactGuid = Account.Id;
string id = entity.Id.ToString();
tracingService.Trace("9");
//throw new Exception("Contact guid" + ContactGuid);
Entity objContact = service.Retrieve("contact", ContactGuid, new ColumnSet(true));
//Entity objContact = (Entity)service.Retrieve("contact", ((EntityReference)entity.Attributes["new_account"]).Id, new ColumnSet(true));
//Entity objContact = (Entity)service.Retrieve("contact", ((EntityReference)Account.Attributes["new_account"]).Id, new ColumnSet(true));
tracingService.Trace("2");
string Updatename = Account.GetAttributeValue<string>("name");
tracingService.Trace("3");
objContact.Attributes["fullname"] = Updatename;
tracingService.Trace("4");
string Updateemail = Account.GetAttributeValue<string>("emailaddress1");
tracingService.Trace("5");
objContact.Attributes["emailaddress1"] = Updateemail;
tracingService.Trace("6");
string Updateaddress = Account.GetAttributeValue<string>("address1_composite");
tracingService.Trace("7");
objContact.Attributes["address1_composite"] = Updateaddress;

service.Update(objContact);
}

*This post is locked for comments

  • Suggested answer
    Conor Gallagher Profile Picture
    Conor Gallagher 715 on at
    RE: how to get created record in plugin

    At an initial glance there are a few problems in the code.

    Firstly, if this is a create plugin "Target" (i.e. context.InputParameters["Target"]) should already contains all of the attributes you need for the newly created record (Target will be your account).  

    Next, what stage have you registered this plugin? If it's in a pre stage then performing a "service.Retrieve" on that newly created entity isn't going to work.

    Next issue is you assign the Account.Id to the ContactGuid and then you try retrieve a contact record (using an account id). This isn't going to work, you've got the wrong Id here. I'm unsure of what you are trying to achieve so struggling to advise an alternative.

    Finally, your question is a little vague. I've interpreted what potentially might be your problem from the title, but all you've posted is code. How far is your trace getting if you download log? Or if it's running asynchronously, what is outputted in the system jobs detail section?

    Edit: I spotted a lengthy tag giving me slightly more details! 

    You're trying to update a contact when an account is updated. But you have a problem in this code:

    Guid ContactGuid = Account.Id;
    Entity objContact = service.Retrieve("contact", ContactGuid, new ColumnSet(true));

    As I said, this isn't going to work, you've got the wrong Id here. Are you trying to update ALL contacts linked to that account? If so you need to do a Retrieve multiple where the parentcustomerid on the contact record equals the account id just updated. For more information on queries/retrieve multiples please refer to the SDK: 

    https://msdn.microsoft.com/en-us/library/gg334708.aspx

    https://msdn.microsoft.com/en-us/library/gg328149.aspx

    (I just noticed the Microsoft samples are a little bit pants... who wrote that spaghetti code?!?)

  • Suggested answer
    Rahul-Waghmare Profile Picture
    Rahul-Waghmare 990 on at
    RE: how to get created record in plugin

    Please check the privileges and permission for those entities for particular user's security role.

    Regards,

    Rahul

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: how to get created record in plugin

    Hi,

     I am getting error "The requested record was not found or you do not have sufficient permissions to view it."

  • Suggested answer
    Rahul-Waghmare Profile Picture
    Rahul-Waghmare 990 on at
    RE: how to get created record in plugin

    Hi,

    Please check below link, this may help you.

    community.dynamics.com/.../plugin-to-update-children-records-when-parent-record-is-upadated

    Regards,

    Rahul

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans