Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Entity Reference cannot have Id and Key Attributes empty.

Posted on by Microsoft Employee

Hi,

I am getting the  "Entity Reference cannot have Id and Key Attributes empty."  error when I include

if (accountingPeriodGuid != Guid.Empty)
{
objLoanDetail[EmpLoanDetail.AccountingPeriod] = new EntityReference(EnityUtility.AccountingPeriod, accountingPeriodGuid);
}

above mentioned  logic in my code.Basically I am checking if EMI month is out of accounting period end month range then the accounting period lookup should remain empty..When I remove the above code from my code it works fine.Anyone know the reason?. My code is as given below:

if (EMIMonth > AccPeriodEndMonth && currentYear == AccPeriodEndYear) { accountingPeriodGuid = Guid.Empty; }

Entity objLoanDetail = new Entity(EnityUtility.EmployeeLoanDetail);
objLoanDetail[EmpLoanDetail.LoanReqId] = new EntityReference(EnityUtility.AdvanceLoanRequestEntity, Guid.Parse("3EDAAABF-BE9E-E711-816E-E0071B69C9D1"));
objLoanDetail[EmpLoanDetail.PrincipalAmount] = new Money(pAmount);
objLoanDetail[EmpLoanDetail.EmiAmount] = new Money(emi);
objLoanDetail[EmpLoanDetail.EmiMonth] = new OptionSetValue(EMIMonth);
objLoanDetail[EmpLoanDetail.Year] = new EntityReference(EnityUtility.YearEntity, yearGuid);
if (accountingPeriodGuid != Guid.Empty)
{
objLoanDetail[EmpLoanDetail.AccountingPeriod] = new EntityReference(EnityUtility.AccountingPeriod, accountingPeriodGuid);
}
objLoanDetail[EmpLoanDetail.EmiStatus] = new OptionSetValue(1);
service.Create(objLoanDetail);

*This post is locked for comments

  • RyanCPerry Profile Picture
    RyanCPerry 105 on at
    RE: Entity Reference cannot have Id and Key Attributes empty.

    If this is registered as a custom action called in a workflow, it is likely that you have specified an output entityreference arguement as required, but your workflow is returning null. Check to see if any of your action output variables are required. If so, set them to optional, save, publish, update plugins and try it then. This fixed the error for me. The problem wasn't the code, it was the action expecting something to be returned.

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Entity Reference cannot have Id and Key Attributes empty.

    Hi,

     where do you define that accountingPeriodGuid variable? Is the error happening every time? Is it a single-threaded application? Do you know if the error is coming from the "create" call or from the new EntityReference call?

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Entity Reference cannot have Id and Key Attributes empty.

    Hi,

    As the error message says, you need to validate the value of EnityUtility.AccountingPeriod & accountingPeriodGuid. They both have to contain the correct value.

    To confirm if this is the cause, you can hardcode these values and check. It is more likely that you have blank for EnityUtility.AccountingPeriod.

    Hope it helps.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Entity Reference cannot have Id and Key Attributes empty.

    Hi,

    in your if statement you also need to check if accountPeriod != null

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans