web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Convert Entity in a EntityReference

(0) ShareShare
ReportReport
Posted on by 445

Hello,

I create a lead in my code and now i want to put out the EntityReference from the lead but everything i try wont work..

This is my code:

 

Entity lead = new Entity("lead");
lead["subject"] = "Test";
lead["salutation"] = anrede;
lead["firstname"] = vorname;
lead["lastname"] = nachname;
lead["address1_line1"] = "";
lead["address1_postalcode"] = "";
lead["city"] = "";
lead["telephone1"] = telefon;
lead["emailaddress1"] = email;
_lead = service.Create(lead);




Entity leadid = new Entity("lead");
leadid = service.Retrieve(lead.LogicalName, _lead, new ColumnSet(true));                                  
                                                                                
Lead.Set(executionContext, leadid.ToEntityReference());

Thank you in advance!

Kind Regards

Léon

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Preeti Sharma Profile Picture
    2,678 on at

    Hi,

    To get entityrefrence you should write following code:

    EntityReference leadEntityRefrence=new EntityReference("lead", leadid );

    Thanks:)

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Léon,

    The code you have shared seems correct. It seems something is not right in your parameters.

    Can you share the full code?

    below is the sample code-

    ----------------------

    public class GetLeadRefrence : CodeActivity

       {

           // Output Lead Reference

           [Output("LeadReference")]

           [ReferenceTarget("lead")]

           public OutArgument<EntityReference> LeadRef { get; set; }

           protected override void Execute(CodeActivityContext executionContext)

           {

               try

               {

                   ITracingService tracingService = executionContext.GetExtension<ITracingService>();

                   IWorkflowContext workflowContext = executionContext.GetExtension<IWorkflowContext>();

                   IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

                   IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);

                   Entity lead = new Entity("lead");

                   lead["subject"] = "Test Lead from workflow";              

                   var leadId = service.Create(lead);

                   LeadRef.Set(executionContext, new EntityReference("lead", leadId));

               }

               catch (Exception pEx)

               {

                   throw new InvalidPluginExecutionException("Error in custom workflow activity!!! " + pEx.Message);

               }

           }

       }

    ---------------------------------------

    Hope this helps

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    You can go through the link

    crmbusiness.wordpress.com/.../hosk-crm-dev-tip-get-entityreference-from-entity

  • BlackBeard Profile Picture
    445 on at

    Okei now i have a new problem.

    I got the error Data at the root level is invalid. Line 1, position 1.

    The Error must be at this position:

                                byte[] data = Convert.FromBase64String(ent.Attributes["body"].ToString());
                                string xml = Encoding.UTF8.GetString(data);
    
                                tracingService.Trace("läuft1");
    
                                XmlDocument doc = new XmlDocument();
                                doc.LoadXml(xml);
                                doc.PreserveWhitespace = false;
                                XmlNode root = doc.DocumentElement;


    Can anyone help me?

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    "Data at the root level is invalid. Line 1, position 1." is normal because f the incorrect xml format. You n=would need to check if the xml you are trying to load is correct and well formed.

    Also, it is always preferred to open a new thread for new issues as it helps other who are searching for the resolution on the same issue and other community member to provide their suggestions.

    Hope this helps.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans