Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

JavaScript: reference from 1:N relationship QueueItems to Projects

Posted on by Microsoft Employee

I want to auto update fields in Projects when a similar field in QueueItems changes.  The 1:N relationship is msdyn_project_QueueItems, with a lookup field labeled "Object."

I am using the following code and getting a non-descriptive message.

function VPNApplicable_Opt() {

var entityVarSent, entityVarRecieved, entityVarApplicable, lookupFieldObject;

lookupFieldObject = Xrm.Page.getAttribute('object(Project)');
if (lookupFieldObject.getValue() != null) {
entityVarRecieved = lookupFieldObject.getValue()[0].id;
entityVarSent = lookupFieldObject.getValue()[0].entityType;
entityVarApplicable = lookupFieldObject.getValue()[0].name;
alert(lookupFieldObject + ' lookupFieldObject \r\n' +
entityVarRecieved +' entityVarRecieved \r\n' +
entityVarSent + ' entityVarSent \r\n' +
entityVarApplicable + ' entityVarApplicable ');
} else {
alert(lookupFieldObject + ' is not an entity');
}
}

I suspect the lookup field is incorrect but can't figure out the right value.

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript: reference from 1:N relationship QueueItems to Projects

    *** Content removed ***

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript: reference from 1:N relationship QueueItems to Projects

    Hello Ravi,

    Yes, the learning process is complete - the script works.

    Thank you again for your knowledge and patience.

    All the best,

    Lannie

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: JavaScript: reference from 1:N relationship QueueItems to Projects

    Hi,

    Yes, Rest Builder does work in V9. Infact ibuild the above code drom my v9 enviroment.

    Ye, i did notice that when you copy paste the code you need to change the quotes.

    Is it fixed for you?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript: reference from 1:N relationship QueueItems to Projects

    Kudos Ravi,

    Thank you for your assistance.

    We are running Dynamics 365 version 9.  The latest version of CRM Rest Builder 2.5 does not purport to support Ver 9.  Can you confirm that it doe work with Version 9?

    Also, a lesson learned.  When I copied the template from the link you provided, the quotation marks needed to be changed.  You might mention that to the next person you rescue.

    All the best,

    Lannie

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: JavaScript: reference from 1:N relationship QueueItems to Projects

    I would also suggest you tpo download and use CRM Rest Builder tool buiild the WEB API request. This is the easiest way to build the query as well as execute it and see the results.

    carldesouza.com/dynamics-crm-rest-builder

    You can download the tool here- github.com/.../releases

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: JavaScript: reference from 1:N relationship QueueItems to Projects

    Okay.. inititla I thought you want to retrieve a value from a related entity (Project) and update the current entity(queuitem) but I understand that you need to retrieve the value from current entity (queueitem)  and update the related entity (project).

    For this you need to do something like this-

    1. Retrieve the project id which you need to update

    var lookupObject = Xrm.Page.getAttribute(“objectid”).getValue();

    var newid = lookupObject[0].id.slice(1, -1);

    2. Retrieve the field value which you want to update on project entity

    var fieldValueToCopy = Xrm.Page.getAttribute(“<fieldschemaname>”).getValue();  // Assuming its a text field

    3. Update the project with this field value

    ==================

    var entity = {};

    entity.msdyn_description = fieldValueToCopy; // This is the part where you will specify which field you want to update with what value. For test purpose, I an using description field on the project entity, you need to change this.

    var req = new XMLHttpRequest();

    req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/msdyn_projects(" + newid + ")", true);  // here the new id if the id of the project which you have extracted above

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.onreadystatechange = function() {

       if (this.readyState === 4) {

           req.onreadystatechange = null;

           if (this.status === 204) {

               //Success - No Return Data - Do Something

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send(JSON.stringify(entity));

    ==================

    Do note that the updating a field also depend upon the type of field we are updating.

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript: reference from 1:N relationship QueueItems to Projects

    Hello Ravi,

    Thank you for your assistance.  I know how to use workflow.  I am using this project to learn how to use JavaScript to accomplish the same task.  The current task is using a two-option field.  I have another task that requires the manipulation of a few multi-option lists.  So, if you would, please answer the last request.

    Thank you for your assistance,

    Lannie

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: JavaScript: reference from 1:N relationship QueueItems to Projects

    Hi,

    Sorry was a little confused. So you have some fields on Queue Item entity and wants to populate the same data on your project entity. If that is correct then you don't need script, you can use workflow. The above shared code is to retrieve the value from a related entity which is opposite to what you are looking for.

    Create a real time workflow which triggers on change of your specified field, and then checks the Type field of Queue item, if it is project

    8117.queueitem1.png

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: JavaScript: reference from 1:N relationship QueueItems to Projects

    Hello Ravi,

    I am using the original Queue Item entity.  I was able to get the first part of the script working by using "ObjectID" as the lookup - in 1:N relation between Projects and Queue Item.

    My code is below:  

    This is basically the template you shared.  Which variables need to be replaced?  For example, in the 5th line "entitypluralname" and "_prefix_fieldname_value".  What are they referencing?  Anymore subsitutions?  Thank you for your patience, I am still a bit of a newbie.

    *******************************

    function VPNApplicable_Opt() {

    debugger;

    var lookupObject = Xrm.Page.getAttribute(“objectid”).getValue();

    var newid = lookupObject[0].id.slice(1, -1);

    var req = new XMLHttpRequest();

    req.open(“GET”, Xrm.Page.context.getClientUrl() + “/api/data/v8.1/entitypluralname(” + newid + “)?$select=_prefix_fieldname_value”, true);

    req.setRequestHeader(“OData-MaxVersion”, “4.0”);

    req.setRequestHeader(“OData-Version”, “4.0”);

    req.setRequestHeader(“Accept”, “application/json”);

    req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);

    req.setRequestHeader(“Prefer”, “odata.include-annotations=\”*\””);

    req.onreadystatechange = function () {

    if (this.readyState === 4) {

    req.onreadystatechange = null;

    if (this.status === 200) {

    var result = JSON.parse(this.response); // you will get the retrieved value in object we stored in result var.

    var retrivedvalue= result._prefix_fieldname_value; //get the id of the field

    var retrivedformatedvalue= result[“_prefix_fieldname_value@OData.Community.Display.V1.FormattedValue”]; //get the formatted name of the field

    if (retrivedvalue!= null) {

    var value = new Array();

    value[0] = new Object();

    value[0].id = retrivedvalue;

    value[0].name = retrivedformatedvalue;

    value[0].entityType = “entityname”;

    Xrm.Page.getAttribute(“fieldname”).setValue(value); //set the lookupObject value finally

    } else

    alert(“some textt!!!!!!”) // optional

    } else {

    Xrm.Utility.alertDialog(this.statusText);

    }

    }

    };

    req.send();

    }

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: JavaScript: reference from 1:N relationship QueueItems to Projects

    Hi,

    No, the Xrm.Page.getAttribute script will return the attributes of the current entity. So if you are running it on Queue Item entity.  Is this Queue Item entity is the out of box entity or you have created your own. Does this have a lookup field of project?

    It will help me understand more if you could share screenshot of your Queue Item entity.

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans