Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to set value for lookup field when create record by plugin in CRM 2016 on-premise

Posted on by 40

Dear all,

I have problem with plugin as subject.

I have entity test_contract which have field "Parent Contract", when plugin create new contract from currentContract, after that test_ParentContract will lookup to currentContract(old contract).

But I can't set value for that field.

Below my code:

var currentContract = serviceContext.test_contractSet.FirstOrDefault(x => x.Id == contract.Id);

Entity newContract = new Entity("test_contract");

{

......some fields;

newContract["test_ParentContract"] = new EntityReference("test_contract", new Guid(currentContract.Id.ToString()));

service.Create(newContract);

}

Please help to resolve this case.

Thanks,

SonDang

*This post is locked for comments

  • Son Dang Profile Picture
    Son Dang 40 on at
    RE: How to set value for lookup field when create record by plugin in CRM 2016 on-premise

    Correct. I used 

    currentContract.ToEntityReference();

  • Verified answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: How to set value for lookup field when create record by plugin in CRM 2016 on-premise

    Hi,

    It looks ok, so the problem must be elsewhere. Please share with us the error that you have.

    Apart from that please simplify your code as it hurts eyes :) Insted of :

    new EntityReference("test_contract", new Guid(currentContract.Id.ToString()));

    just put:

    currentContract.ToEntityReference();

  • Verified answer
    shivaram Profile Picture
    shivaram 3,315 on at
    RE: How to set value for lookup field when create record by plugin in CRM 2016 on-premise

    Is it working now?

  • Verified answer
    Son Dang Profile Picture
    Son Dang 40 on at
    RE: How to set value for lookup field when create record by plugin in CRM 2016 on-premise

    Hi Shivaram Santosh,

    Yep, new contract is child of old contract (on a entity)

  • shivaram Profile Picture
    shivaram 3,315 on at
    RE: How to set value for lookup field when create record by plugin in CRM 2016 on-premise

    Hi Son dang,

    I am confused with question, as per my understanding, you created self relation with testcontract?

  • Son Dang Profile Picture
    Son Dang 40 on at
    RE: How to set value for lookup field when create record by plugin in CRM 2016 on-premise

    Thanks Goutham A,

    I try to hardcode with Id of currentcontract as below but issue still happen.

    newContract.Attributes["test_ParentContract"] = new EntityReference("test_contract", new Guid("d0018a83-b6bd-e611-80d6-76113887469e"));

    :(

  • Suggested answer
    Goutham A Profile Picture
    Goutham A on at
    RE: How to set value for lookup field when create record by plugin in CRM 2016 on-premise

    Hi son,

    I see the code to assign lookup value(entity refernce) is correct. Can you check once if the guid(currentContract.Id) is having correct  value. Also  for testing purposes check by giving the hardcoded guid.  

    Also please check once by refreshing the page after the plugin executes.

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans