I have integrated an Add New Lead button into the main form on the Homepage for contacts.
Clicking this button triggers a script that opens a new form and passes "Crm Parameter FirstSelectedItemId" as a parameter.
By selecting a contact and clicking create new lead, I can pass the ID as a parameter to the function:
function openNewLead(SelectedID) {
parameters["customer"] = SelectedID;
Xrm.Utility.openEntityForm("lead", null, parameters);
}
The field "customer" is a lookup field. However, I encountered an issue where it populates the lookup but does not pass the full name correctly. It works fine after saving and refreshing!
To address this, I attempted:
function openNewLead(SelectedID) {
if (SelectedID != null) {
var parameters = {};
var request = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/ContactSet?$select=FullName&$filter=ContactId eq guid'" + SelectedID + "'";
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: request,
async: false,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
if (data.d.results.length > 0) {
var lookupValue = new Array();
lookupValue[0] = new Object();
lookupValue[0].id = SelectedID;
lookupValue[0].name = data.d.results[0].FullName;
lookupValue[0].entityType = "contact";
parameters["customer"] = lookupValue;
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
/*Error Occurred*/
}
});
Xrm.Utility.openEntityForm("lead", null, parameters);
}
else {
Xrm.Utility.openEntityForm("lead");
}
}
This solution does not work from the homepage/main screen since no reference can be added for JSON.
Therefore, my question is how do I reference JSON from here or is there a more efficient way to approach this?
Thank you