How to Insert data using lookup CRM 365 through Plugin C#

551 views Asked by At

i have a lookup "new_lookuptransactionheader" in my entity "new_trialxrmservicetoolkit". This lookup linked from "new_transactionheader" entity. How can i insert data using c# crm plugin? this mycode:

public void InsertDataUsingLookup(XrmServiceContext xrm, Entity entitiy, ITracingService tracer)
{
    new_trialxrmservicetoolkit trial = new new_trialxrmservicetoolkit();
    trial.new_name = "testplugin";
    trial.new_LookupTransactionHeader = null; //this is i don't know how to get value from new_LookupTransactionHeader
    trial.new_Notes = "this is test plugin using c#";
    xrm.AddObject(trial);
    xrm.SaveChanges();
}

I updated mycode and this solve:

public void InsertDataUsingLookup(XrmServiceContext xrm, Entity entitiy, ITracingService tracer)
    {
        new_trialxrmservicetoolkit trial = new new_trialxrmservicetoolkit();
        trial.new_name = "testplugin";
        trial.new_LookupTransactionHeader = new EntityReference("new_transactionheader", Guid.Parse("5564B5F0-0292-E711-8122-E3FE48DB937B"));
        trial.new_Notes = "this is test plugin using c#";
        xrm.AddObject(trial);
        xrm.SaveChanges();
    }
1

There are 1 answers

9
Arun Vinoth-Precog Tech - MVP On BEST ANSWER
trial.Attributes["new_LookupTransactionHeader"] = new EntityReference("new_transactionheader", new_transactionheaderId-GUID);

You have to use EntityReference like above to setup lookup attribute.