Answered by:
Need help with lookup Currency field!

Question
-
Hi!
Im very new to coding so most of my code is from resources I found on the internet. The full code should create a new record triggered OnChange on field level. This array works fine for the opportunity lookup, however when I try to make the same approach with the Currency field I get failure.
var value = new Array(); value[0] = new Object(); value[0].id = Xrm.Page.data.entity.getId(); value[0].name = Xrm.Page.getAttribute("fieldname").getValue(); value[0].entityType = "fieldname"; JSONAcc."fieldname" = { Id: value[0].id, LogicalName: "fieldname", Name: value[0].name };
This does not work:
var currencyField = new Array(); currencyField[0] = new Object(); currencyField[0].id = Xrm.Page.data.entity.getId(); currencyField[0].name = Xrm.Page.getAttribute("fieldname").getValue(); currencyField[0].entityType = "fieldname"; alert(currencyField); JSONAcc."fieldname" = { Id: currencyField[0].id, LogicalName: "fieldname", Name: currencyField[0].name };
Can you explain why?
Full code:
function CreateRecord() { var createrecord; var context = Xrm.Page.context; var clientUrl = context.getClientUrl(); var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc"; var JSONAcc = {}; // Array for lookup to opportunity record var value = new Array(); value[0] = new Object(); value[0].id = Xrm.Page.data.entity.getId(); value[0].name = Xrm.Page.getAttribute("fieldname").getValue(); value[0].entityType = "fieldname"; var currencyField = new Array(); currencyField[0] = new Object(); currencyField[0].id = Xrm.Page.data.entity.getId(); currencyField[0].name = Xrm.Page.getAttribute("fieldname").getValue(); currencyField[0].entityType = "fieldname"; alert(currencyField); ///////////////////////////////////////////////////////////// // Specify the ODATA entity collection var ODATA_EntityCollection = "/"entitySet"; ///////////////////////////////////////////////////////////// // Define attribute values for the CRM object you want created JSONAcc."fieldname" = "TEST"; JSONAcc."fieldname" = { Id: value[0].id, LogicalName: "fieldname", Name: value[0].name }; JSONAcc."fieldname" = { Id: currencyField[0].id, LogicalName: "fieldname", Name: currencyField[0].name }; //Parse the entity object into JSON var jsonEntity = window.JSON.stringify(JSONAcc); //Asynchronous AJAX function to Create a CRM record using OData $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", datatype: "json", url: clientUrl + ODATA_ENDPOINT + ODATA_EntityCollection, data: jsonEntity, beforeSend: function (XMLHttpRequest) { //Specifying this header ensures that the results will be returned as JSON. XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function (data, textStatus, XmlHttpRequest) { alert("success"); var NewCRMRecordCreated = data["d"]; createrecord = NewCRMRecordCreated."fieldname"; }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert("failure"); } }); }
- Edited by daffa1 Friday, April 21, 2017 11:18 AM Privacu
Wednesday, April 19, 2017 9:12 AM
Answers
-
It's just a guess - is there any chance that you have other lookup that references Transaction Currency and this field is placed on form?
Dynamics CRM MVP
Read My blog
Subscribe for one of my courses- Marked as answer by daffa1 Friday, April 21, 2017 11:25 AM
Thursday, April 20, 2017 3:02 PMModerator -
Hey,
For currency fields you should use something like following:
JSONAcc.new_currencyfield = {Value: "1000"};
Dynamics CRM MVP
Read My blog
Subscribe for one of my courses- Marked as answer by Andrii ButenkoMVP, Moderator Friday, April 21, 2017 2:20 PM
- Unmarked as answer by Andrii ButenkoMVP, Moderator Friday, April 21, 2017 2:20 PM
- Proposed as answer by Andrii ButenkoMVP, Moderator Friday, April 21, 2017 2:20 PM
- Marked as answer by daffa1 Friday, April 21, 2017 2:39 PM
Friday, April 21, 2017 12:43 PMModerator
All replies
-
I tried this snippet aswell. I get the GUID, name and entitytype message along with "success", it creates a new record but the Currency field is not related to what the opportunity has.var lookup = new Array(); lookup = new Object(); lookup = Xrm.Page.getAttribute("fieldname").getValue(); if (lookup != null) { var currencyName = lookup[0].name; var currencyId = lookup[0].id; var currencyEntityType = lookup[0].entityType; } alert(currencyName + currencyId + currencyEntityType); JSONAcc."fieldname" = { Id: currencyId[0].id, LogicalName: "fieldname", Name: currencyName[0].name };
- Edited by daffa1 Friday, April 21, 2017 11:17 AM
Wednesday, April 19, 2017 12:00 PM -
Hello,
Try following code:
function CreateOpportunityMonth() { var opportunityMonthId; var context = Xrm.Page.context; var clientUrl = context.getClientUrl(); var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc"; var JSONAcc = {}; // Array for lookup to opportunity record var value = new Array(); value[0] = new Object(); value[0].id = Xrm.Page.data.entity.getId(); value[0].name = Xrm.Page.getAttribute("name").getValue(); value[0].entityType = "opportunity"; var currencyField = Xrm.Page.getAttribute("transactioncurrencyid").getValue(); ///////////////////////////////////////////////////////////// // Specify the ODATA entity collection var ODATA_EntityCollection = "/new_opportunitymonthSet"; ///////////////////////////////////////////////////////////// // Define attribute values for the CRM object you want created JSONAcc.new_name = "TEST"; JSONAcc.new_opportunitymonthsId = { Id: value[0].id, LogicalName: "opportunity", Name: value[0].name }; if (currencyField != null) JSONAcc.TransactionCurrencyId = { Id: currencyField[0].id, LogicalName: "transactioncurrency", Name: currencyField[0].name }; //Parse the entity object into JSON var jsonEntity = window.JSON.stringify(JSONAcc); //Asynchronous AJAX function to Create a CRM record using OData $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", datatype: "json", url: clientUrl + ODATA_ENDPOINT + ODATA_EntityCollection, data: jsonEntity, beforeSend: function (XMLHttpRequest) { //Specifying this header ensures that the results will be returned as JSON. XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function (data, textStatus, XmlHttpRequest) { alert("success"); var NewCRMRecordCreated = data["d"]; opportunityMonthId = NewCRMRecordCreated.new_opportunitymonthid; }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert("failure"); } }); }
Dynamics CRM MVP
Read My blog
Subscribe for one of my courses- Proposed as answer by Andrii ButenkoMVP, Moderator Wednesday, April 19, 2017 2:47 PM
Wednesday, April 19, 2017 2:47 PMModerator -
Thanks for the answer. The code goes through and creates the record but it still wont change the value of the currency field on the new record..
- Edited by daffa1 Friday, April 21, 2017 11:25 AM
Thursday, April 20, 2017 7:52 AM -
It's just a guess - is there any chance that you have other lookup that references Transaction Currency and this field is placed on form?
Dynamics CRM MVP
Read My blog
Subscribe for one of my courses- Marked as answer by daffa1 Friday, April 21, 2017 11:25 AM
Thursday, April 20, 2017 3:02 PMModerator -
Thanks! There were some other processes interferring with the record, with them gone it is working.
Im still having problem with assigning the value of the other currency field though. Even just setting the value to a number like "1000" gives me failure, what is the syntax for Currency field? Im getting "Bad Request 400" and "The request should be a valid top-level resource object".
Also I would be very happy if you could edit out your code for privacy purposes.
- Edited by daffa1 Friday, April 21, 2017 12:39 PM
Friday, April 21, 2017 11:24 AM -
Hey,
For currency fields you should use something like following:
JSONAcc.new_currencyfield = {Value: "1000"};
Dynamics CRM MVP
Read My blog
Subscribe for one of my courses- Marked as answer by Andrii ButenkoMVP, Moderator Friday, April 21, 2017 2:20 PM
- Unmarked as answer by Andrii ButenkoMVP, Moderator Friday, April 21, 2017 2:20 PM
- Proposed as answer by Andrii ButenkoMVP, Moderator Friday, April 21, 2017 2:20 PM
- Marked as answer by daffa1 Friday, April 21, 2017 2:39 PM
Friday, April 21, 2017 12:43 PMModerator -
Thanks, that works. However I want the value from another entity.
I've tried these but none of them work:
var currencyfield = Xrm.Page.getAttribute("fieldname").getValue();
JSONAcc.new_currencyfield = {Value: currencyfield};
and
JSONAcc.new_currencyfield = { Value: Xrm.Page.data.entity.attributes.get("fieldname").getValue() };
Friday, April 21, 2017 2:39 PM -
Ok. How do you get that values from other entity? With Xrm.Page.getAttribute... you can get only values avaialble of form. To get values from other entities you will have to use Rest to retrieve it:
https://msdn.microsoft.com/en-us/library/gg334427%28v=crm.7%29.aspx
https://msdn.microsoft.com/en-us/library/gg309549%28v=crm.7%29.aspx
Dynamics CRM MVP
Read My blog
Subscribe for one of my coursesFriday, April 21, 2017 2:54 PMModerator -
I would like for example to get the field data from "opportunityvalue" (lets say its 1000) from the opportunity record and put it in the amount field on the created custom object.
It should work with Xrm.Page.getAttribute? Im thinking its more like syntax error since Im not familiar with this coding.
So instead of "JSONAcc.new_currencyfield = {Value: "1000"};", I want the Value to be the field data from the currency field in the opportunity record.Monday, April 24, 2017 8:47 AM -
If you want to get field from record that's not currently opened you can't get it without endpoint call. To get it you should get id of opportunity (through lookup or other way), call endpoint to get that value and use after. Good luck.
Dynamics CRM MVP
Read My blog
Subscribe for one of my coursesMonday, April 24, 2017 1:40 PMModerator