SBX - Search With Button

SBX - Forum Post Title

JavaScript: reference from 1:N relationship QueueItems to Projects

Microsoft Dynamics CRM Forum

LanCru5 asked a question on 9 Oct 2018 10:35 AM

Question Status

Verified

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.

Reply
Ravi Kashyap responded on 9 Oct 2018 7:50 PM
Suggested Answer

Hi,

This doesn't seem like a correct code to me. If the field you want to retrieve is on the same entity then you can refer the below link to get the value-

community.dynamics.com/.../how-to-get-and-set-a-lookup-field-using-javascript-in-dynamics-365

If the field is no the related entity, then you can refer this link:

community.dynamics.com/.../get-lookup-value-from-other-entity-and-set-it-on-the-form-using-web-api-in-microsoft-dynamics-crm

Hope this helps.

Reply
LanCru5 responded on 10 Oct 2018 11:07 AM

Hello Ravi,  

Thank you for your response.  As for your response, my challenge is I am implementing the script in the "Queue Item" entity.  When the two-option field, "VPNApplicable", changes I want to update the field in the Projects entity by the same name.  Looking at the link you provided for the 2nd option, it is not clear to me how the line of code, "var lookup= Xrm.Page.getAttribute(“attributename”).getValue();" will return the "attributename" from Projects, when I am invoking the script in "Queue Item."  I have attempted using it with no success.

Lannie

Reply
Ravi Kashyap responded on 10 Oct 2018 11:41 PM

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.

Reply
LanCru5 responded on 11 Oct 2018 12:09 AM

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

}

Reply
Ravi Kashyap responded on 11 Oct 2018 12:35 AM
Suggested Answer

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

Hope this helps.

Reply
LanCru5 responded on 11 Oct 2018 8:59 AM

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

Reply
Ravi Kashyap responded on 11 Oct 2018 9:15 PM
Verified Answer

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.

Reply
Ravi Kashyap responded on 11 Oct 2018 9:16 PM
Suggested Answer

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

Reply
LanCru5 responded on 12 Oct 2018 3:37 PM

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

Reply
Ravi Kashyap responded on 12 Oct 2018 5:21 PM

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?

Reply
LanCru5 responded on 13 Oct 2018 10:51 AM

Hello Ravi,

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

Thank you again for your knowledge and patience.

All the best,

Lannie

Reply
Ravi Kashyap responded on 11 Oct 2018 9:15 PM
Verified Answer

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.

Reply
Ravi Kashyap responded on 9 Oct 2018 7:50 PM
Suggested Answer

Hi,

This doesn't seem like a correct code to me. If the field you want to retrieve is on the same entity then you can refer the below link to get the value-

community.dynamics.com/.../how-to-get-and-set-a-lookup-field-using-javascript-in-dynamics-365

If the field is no the related entity, then you can refer this link:

community.dynamics.com/.../get-lookup-value-from-other-entity-and-set-it-on-the-form-using-web-api-in-microsoft-dynamics-crm

Hope this helps.

Reply
Ravi Kashyap responded on 11 Oct 2018 12:35 AM
Suggested Answer

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

Hope this helps.

Reply
Ravi Kashyap responded on 11 Oct 2018 9:16 PM
Suggested Answer

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

Reply

SBX - Two Col Forum

SBX - Migrated JS