Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Answered

Set Value in Lookup Filed using Plugin D365

Posted on by 267

Hello Team,

here i update the Lookup value in (SalesOrderDetail)  Entity Lookup Filed (msdyn_project) it is updated but need to refresh the page after showing value in Lookup Filed i want to need without refresh set the value in Lookup Field

Below my code

SalesOrderDetail objSalesOrderDetailsUpdate = new SalesOrderDetail()
{
Id = contractLineGuid,
msdyn_Project = new EntityReference(msdyn_project.EntityLogicalName, ProjectEntity.Id)
};

//Update the Contract record
currentUserService.Update(objSalesOrderDetailsUpdate);

  • Verified answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Set Value in Lookup Filed using Plugin D365

    Hi,

    I tried below code and it works as expected -

    function OpenContactQC(primaryControl)

    {

    var formContext=primaryControl;

    var entityFormOptions = {};

    entityFormOptions["entityName"] = "contact";

    entityFormOptions["useQuickCreateForm"] = true;

    // Set default values for the Contact form

    var formParameters = {};

    formParameters["firstname"] = "Sample";

    formParameters["lastname"] = "Contact";

    formParameters["fullname"] = "Sample Contact";

    formParameters["emailaddress1"] = "contact@adventure-works.com";

    formParameters["jobtitle"] = "Sr. Marketing Manager";

    formParameters["donotemail"] = "1";

    formParameters["description"] = "Default values for this record were set programmatically.";

    // Open the form.

    Xrm.Navigation.openForm(entityFormOptions, formParameters).then(

       function (obj) {

           console.log(obj);

    console.log("Created new " + obj.savedEntityReference[0].entityType + " named '" + obj.savedEntityReference[0].name + "' with id:" +

    obj.savedEntityReference[0].id);

    // Create new lookup array

      var lookup = [];

       lookup[0] = {};

       lookup[0].id = obj.savedEntityReference[0].id;

       lookup[0].entityType = obj.savedEntityReference[0].entityType;

       lookup[0].name = obj.savedEntityReference[0].name;

      // Get and Set New Lookup

       var emergencyContact = formContext.getAttribute("msft_emergencycontact");

       emergencyContact.setValue(lookup);

       },

       function (error) {

           console.log(error);

       });

    }

    6114.CR48.PNG

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Ashik Patel Profile Picture
    Ashik Patel 267 on at
    RE: Set Value in Lookup Filed using Plugin D365

    Hello Bipin,

    here i change my Code and checked but still not working

    Old Code

    function openDialog()
    {
    var params = {};

    Xrm.Utility.openQuickCreate("msdyn_project", null, params).then(function () { console.log("Success"); }, function (error) {
    console.log(error.message);
    });
    }

    New Code

    function openDialog() {

    var entityFormOptions = {};
    entityFormOptions["entityName"] = "msdyn_project";
    entityFormOptions["useQuickCreateForm"] = true;
    Xrm.Navigation.openForm(entityFormOptions, null).then(
    function(lookup) {
    console.log("Success");
    },
    function(error) {
    console.log("Error");
    }
    );

    }

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Set Value in Lookup Filed using Plugin D365

    Hi,

    Xrm.Utility.openQuickCreate is deprecated -

    docs.microsoft.com/.../xrm-utility

    You should use openForm instead -

    docs.microsoft.com/.../openform

    So update your code to use openForm and then set the lookup in successCallback method.

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Ashik Patel Profile Picture
    Ashik Patel 267 on at
    RE: Set Value in Lookup Filed using Plugin D365

    Hello Bipin,

    i using Below code to  opening Quick form using Ribbon

    function openDialog()

    {

      var params = {};

    Xrm.Utility.openQuickCreate("msdyn_project", null, params).then(function () { console.log("Success"); }, function (error) {

       console.log(error.message);

    });

    }

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Set Value in Lookup Filed using Plugin D365

    HI,

    You can set you lookup field using Javascript in successcallback function.

    docs.microsoft.com/.../openform

    I hope you are using openform method to open quick create form from your ribbon button, if yes then you can easily the lookup with current created record.

    successCallback Function No A function to execute when the record is saved in the quick create form.

    This function is passed an object as a parameter. The object has a savedEntityReference array with the following properties to identify the record(s) displayed or created:

    entityType: The logical name of the table.

    id: A string representation of a GUID value for the record.

    name: The primary column value of the record displayed or created.

    NOTE:

    The successCallback function is not executed when you open a form for an existing or new record.

    The successCallback function is executed only when you save a record in a quick create form that was opened using the openForm method.

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Ashik Patel Profile Picture
    Ashik Patel 267 on at
    RE: Set Value in Lookup Filed using Plugin D365

    My Plugin Code

     if (context.InputParameters.Contains("Target"))

                   {

                       BDOContext dbContext = new BDOContext(currentUserService);

                       Entity ProjectEntity = (Entity)context.InputParameters["Target"];

                       ProjectEntity.Id = context.PrimaryEntityId;

                       Guid userId = context.InitiatingUserId;

                       if (ProjectEntity.Contains("ispl_contractlinenumber"))

                       {

                           string ispl_contractlinenumber = (string)ProjectEntity["ispl_contractlinenumber"];

                           Guid contractLineGuid = Guid.Parse(ispl_contractlinenumber);

                           SalesOrderDetail objsalesorderdetail = dbContext.SalesOrderDetailSet.Where(c => c.Id == contractLineGuid).FirstOrDefault();

                           SalesOrderDetail objSalesOrderDetailsUpdate = new SalesOrderDetail()

                           {

                               Id = contractLineGuid,

                               msdyn_Project = new EntityReference(msdyn_project.EntityLogicalName, ProjectEntity.Id)//new EntityReference("msdyn_project", ProjectEntity.Id)

                           };

                           //Update the Contract record

                           currentUserService.Update(objSalesOrderDetailsUpdate);

                       }

                   }

  • Ashik Patel Profile Picture
    Ashik Patel 267 on at
    RE: Set Value in Lookup Filed using Plugin D365

    Here when i create new create Project button Quick Create Project Form Will open for creating new Project  after that when i press save and close that time project will be create 

    In Parent form one Project field (Lookup) this project will be attached after create.

    Now Project is created but not attached automatically in lookup field  need to refresh page after attach

    Here i attach snap7356.Project.png

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Set Value in Lookup Filed using Plugin D365

    Hi,

    Please share some screenshot of what is happening and what do you want because it is not very clear from your post?

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Ashik Patel Profile Picture
    Ashik Patel 267 on at
    RE: Set Value in Lookup Filed using Plugin D365

    Hello Bipin,

    my plugin is already sync mode

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Set Value in Lookup Filed using Plugin D365

    Hi,

    Change your plugin step to execute in sync mode instead of async to see the result instant.

    7573.CR47.PNG

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

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