Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

update lookup after post create plugin

Posted on by Microsoft Employee

hi , 

i have post create plugin in entity A ,

after create plugin i want to update other entity B , that have lookup of A , with new created record , 

but its not work and i have error like this 

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #F9613D51Detail:
<OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">
<ErrorCode>-2147220970</ErrorCode>
<ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" />
<Message>System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #F9613D51</Message>
<Timestamp>2018-06-27T04:31:01.6868243Z</Timestamp>
<InnerFault i:nil="true" />
<TraceText>

i cant see in sql new created record and when i want to set the id to entity b , Although I see Created  record in plugin i cant set it to records of entity B 

and i have another question , how can i find error details with Reference number 

this is my code sample 

IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService _service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

if (context.MessageName.ToLower() == "create")
{

Entity ExchangeRate = (Entity)context.InputParameters["Target"];
ExchangeRate = _service.Retrieve(ExchangeRate.LogicalName, ExchangeRate.Id, new ColumnSet(true));

QueryExpression FI = new QueryExpression("FI");
FI.ColumnSet = new ColumnSet(true);
FI.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
EntityCollection ECI = _service.RetrieveMultiple((QueryBase)FI);

for (int index = 0; index < ECI.Entities.Count; index++)
{
Entity FIEntity = ECI.Entities[index];
FIEntity["saba_exchangeratefinacialinformationid"] = ExchangeRate.Id;
_service.Update(FIEntity);


}
}

exhangerate is etity A 

FI is entity B include lookup of A 

thanks for your answer 

*This post is locked for comments

  • Verified answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: update lookup after post create plugin

    No you have to use for each loop only. Nothing is there to update complete entity set in single statement just like in SQL.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: update lookup after post create plugin

    thanks for your answer its done  ,but i have another question ,   do we have any way to update whole record with something like stored procedures  instead use foreach loop

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: update lookup after post create plugin

    thanks , its  work

  • Verified answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: update lookup after post create plugin

    You don't need to write any code to get Id, you can simply use like below

    Entity ExchangeRate = (Entity)context.InputParameters["Target"];
    
    QueryExpression FI = new QueryExpression("FI");
    FI.ColumnSet = new ColumnSet(true);
    FI.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
    EntityCollection ECI = _service.RetrieveMultiple((QueryBase)FI);
    
    for (int index = 0; index < ECI.Entities.Count; index++)
    {
    Entity FIEntity = ECI.Entities[index]; 
    FIEntity["saba_exchangeratefinacialinformationid"] = new EntityReference("A entity logicalname",ExchangeRate.Id);
    _service.Update(FIEntity);
    
    
    } 



  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: update lookup after post create plugin

    change this line :FIEntity["saba_exchangeratefinacialinformationid"] = ExchangeRate.Id;

    with

    FIEntity["saba_exchangeratefinacialinformationid"] =  new EntityReference(ExchangeRate.LogicalName , ExchangeRate.Id);

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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans