Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Get Prospect Name from Post Create Opportunity Plugin

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hello! I'm trying to make a plugin to insert the firstname, middlename, and lastname from a prospect into my database using a web service after an opportunity is created, but I do not know how to get the prospect information from an opportunity plugin.

If I assign entity["name"].ToString(); to a variable, it returns the name of the Opportunity, but if I assign entity["originatingleadid"].ToString(); it returns "Microsoft.Xrm.Sdk.EntityReference".

Then, after the information is inserted in the database, my web service returns an ID. How can I save this Id in an Opportunity field? 

Here's the code of my plugin.

Thanks in advance.

public void Execute(IServiceProvider serviceProvider)
{
  IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
  IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
  IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

  if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
  {
   Entity entity = (Entity)context.InputParameters["Target"];

   if (entity.LogicalName != "opportunity")
    return;

    nom = entity["name"].ToString();
    app = entity["originatingleadid"].ToString();
    apm = entity["ownerid"].ToString();

    crmPlugins.crmPlugInsert.WebReference.websCRM webService = new crmPlugins.crmPlugInsert.WebReference.websCRM();
    folioS = webService.Insert(nom, app, apm);
  }
}

*This post is locked for comments

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Get Prospect Name from Post Create Opportunity Plugin

    Hi,

    1. what field you need to pass into your webservice? you can refer to the link i shared .

    2. Yes, you are missing the privilege to update the record.

    if the record is opportunity, Grant the "Write privilege to opportunity under the security role."

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get Prospect Name from Post Create Opportunity Plugin

    Thanks for your answer. I just have a couple of questions.

    1. Can you please tell me how I make the queries or point me to the documentation I need to make them?

    2. I'm getting an "Access denied" alert for "AccessRights: WriteAccess Detail". Do I have to do something additional to create or update a value?

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Get Prospect Name from Post Create Opportunity Plugin

    Hi,

    originatingleadid is of type EntityReference i.e. it is a reference to other entity (which is lead). You first need to workout which field value from opportunity entity you want pass to your web service? I doubt your web service is expecting guid value of the lead associated to opportunity entity?

    To update the value back to CRM, you could use the above code shared by Alex.

    Hope this helps.

  • Verified answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Get Prospect Name from Post Create Opportunity Plugin

    Hi,

    you should use "EntityReference" for your originatingleadid, because it is another entity.

    [View:https://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.entityreference.aspx:750:50]

    25516.7.png

    After you get the ID of the lead/prospect.

    Write a query to get the field value by passing the leadid. If you wish to get more fields from lead.

    [View:https://msdn.microsoft.com/en-us/library/gg328046.aspx:750:50]

    if you are using lead name :

    EntityReference Lead = entity.GetAttributeValue<EntityReference>("originatingleadid");

    string leadname = Lead.Name;

    The value returned from your webservice, use below method to update into your current opportunity.

    Entity opp = new Entity("opportunity");
    opp.Id = entity.Id;
    opp.Attributes["yourfield"] = Your return value;
    service.Update(opp);

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans