Skip to main content

Notifications

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
    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
    Community Member Profile Picture
    on at
    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
    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
    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
    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
    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
    Community Member Profile Picture
    on at
    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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 181 Super User 2025 Season 1

#2
Siv Sagar Profile Picture

Siv Sagar 149 Super User 2025 Season 1

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 124 Super User 2025 Season 1

Product updates

Dynamics 365 release plans