web
You’re offline. This is a read only version of the page.
close
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 729

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
    }
	);
}

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    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);
            }
        );
        
    }

  • TylerS.Dev Profile Picture
    729 on at

    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
    PabloCRP Profile Picture
    1,088 on at

    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
    729 on at

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

  • TylerS.Dev Profile Picture
    729 on at

    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"];

  • Suggested answer
    Community Member Profile Picture
    on at

    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
    729 on at

    Thank you both, it is working now.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 60 Most Valuable Professional

#2
#ManoVerse Profile Picture

#ManoVerse 51

#3
Satyam Prakash Profile Picture

Satyam Prakash 42

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans