Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Javascript not retrieving the correct Guid for lookup field and update trough web api not running

(0) ShareShare
ReportReport
Posted on by 725

I am trying to create a JavaScript function that fires during form's OnSave event.  This function needs to take the Guid of the email, search for related  queue items, and update a user lookup field with owner of the email.  I was able to get the Guid of the email and retrieve the queue items, but I keep getting the email Id instead of owner Id when using getEntityReference(), and it is not updating the lookup field in the queue item, any help is appreciated.

function updateEmailQueue(formContext) {

    var emailOwner = formContext.data.entity.getEntityReference("ownerid"); //formContext.data.entities.getAttribute("ownerid").getValue()
    console.log("email owner guid"   emailOwner.id);
    var emailId = formContext.data.entity.getId();
    console.log("email guid"   emailId);
	Xrm.WebApi.retrieveMultipleRecords("queueitem", "?$select=title,_objectid_value,_workerid_value&$filter=_objectid_value eq "   emailId).then(
    function success(queueItem) {
        //retrieve related queueitems and update work by field
        for (var i = 0; i < queueItem.entities.length; i  ) {
            console.log(queueItem.entities[i]);
            var queueItemId = queueItem.entities[i].queueItemId;
            console.log(queueItemId);
            var entity = {};
            entity["workerid_systemuser@odata.bind"] = "/systemusers("   emailOwner.id   ")";

            Xrm.WebApi.online.updateRecord("queueitem", queueItemId, entity).then(
            function success(result) {
                console.log("queueitem updated");
            },
            function(error) {
                Xrm.Utility.alertDialog(error.message);
            }   
            );

        }

    },
    function (error) {
        console.log(error.message);
		Xrm.Navigation.openAlertDialog(error.message);
        // handle error conditions
    }
	);
}

  • TylerS.Dev Profile Picture
    TylerS.Dev 725 on at
    RE: Javascript not retrieving the correct Guid for lookup field and update trough web api not running

    Thank you both, it is working now.

  • Suggested answer
    RE: Javascript not retrieving the correct Guid for lookup field and update trough web api not running

    Hi Tyler,

    The field you select is wrong, you need select QueueItemId not QueueId.

    pastedimage1604021687470v1.png

    And Request that created is that:

    var queueitemid = results.entities[i]["queueitemid"];

  • TylerS.Dev Profile Picture
    TylerS.Dev 725 on at
    RE: Javascript not retrieving the correct Guid for lookup field and update trough web api not running

    I rewrite the code using REST builder as Leah suggested, but the two variable below are both showing in console as UNDEFINED, how do I get the GUID for the queue item?

    var _queueid_value = results.entities[i]["_queueid_value"];

    var _queueid_value_formatted = results.entities[i]["_queueid_value@OData.Community.Display.V1.FormattedValue"];

  • TylerS.Dev Profile Picture
    TylerS.Dev 725 on at
    RE: Javascript not retrieving the correct Guid for lookup field and update trough web api not running

    I erased my previous reply, going to test it with console and let you know.

  • Verified answer
    PabloCRP Profile Picture
    PabloCRP 1,088 on at
    RE: Javascript not retrieving the correct Guid for lookup field and update trough web api not running

    take a look at this line

     var queueItemId = queueItem.entities[i].queueItemId;

    has queueItemId a valid GUID value?

    I would user either

    var queueItemId = queueItem.entities[i]["queueitemid"];
    
    OR
    
    var queueItemId = queueItem.entities[i].queueitemid;
    
    

  • TylerS.Dev Profile Picture
    TylerS.Dev 725 on at
    RE: Javascript not retrieving the correct Guid for lookup field and update trough web api not running

    I tried the revised code you provided on both my dev instance and a brand new trial instance, it is now pulling the user GUID correctly but it is still not updating the queue item record, any suggestions?

    Also, for line 9 ,  Xrm.WebApi.online.retrieveMultipleRecords("queueitem", "?$select=_objectid_value,title,_workerid_value&$filter=_objectid_value eq "+emailId+"") , is there a reason to add "" at the end?

  • Verified answer
    RE: Javascript not retrieving the correct Guid for lookup field and update trough web api not running

    Hi Tyler,

    You can try to use crm rest builder too, which can create request easily.

    Download page:https://github.com/jlattimer/CRMRESTBuilder

    1.Retrieve queueitem with filter.

    pastedimage1603872651730v1.png

    2.Update queueitem.

    pastedimage1603854813365v2.png

    3.Js code.

    function updateEmailQueue(executionContext) {
        var formContext = executionContext.getFormContext();
        var emailOwner = formContext.getAttribute('ownerid').getValue();
        var emailOwner = emailOwner[0].id.slice(1, -1);
        console.log("email owner guid"   emailOwner);
        var emailId = formContext.data.entity.getId().replace('{', '').replace('}', '');
        console.log("email guid"   emailId);
    
        Xrm.WebApi.online.retrieveMultipleRecords("queueitem", "?$select=_objectid_value,title,_workerid_value&$filter=_objectid_value eq " emailId "").then(
            function success(results) {
                for (var i = 0; i < results.entities.length; i  ) {
                    var _objectid_value = results.entities[i]["_objectid_value"];
                    var _objectid_value_formatted = results.entities[i]["_objectid_value@OData.Community.Display.V1.FormattedValue"];
                    var _objectid_value_lookuplogicalname = results.entities[i]["_objectid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                    var queueitemid = results.entities[i]["queueitemid"];
                    var title = results.entities[i]["title"];
                    var _workerid_value = results.entities[i]["_workerid_value"];
                    var _workerid_value_formatted = results.entities[i]["_workerid_value@OData.Community.Display.V1.FormattedValue"];
                    var _workerid_value_lookuplogicalname = results.entities[i]["_workerid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
    
                    var entity = {};
                    entity["workerid_systemuser@odata.bind"] = "/systemusers(" emailOwner ")";
                    
                    Xrm.WebApi.online.updateRecord("queueitem", "" queueitemid "", entity).then(
                        function success(result) {
                            var updatedEntityId = result.id;
                        },
                        function(error) {
                            Xrm.Utility.alertDialog(error.message);
                        }
                    );
                }
            },
            function(error) {
                Xrm.Utility.alertDialog(error.message);
            }
        );
        
    }

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans