Question

I Have added a Add New Lead button to the Homepage Main form of contacts

This calls a script to open a new form passing Crm Parameter FirstSelectedItemId

So when I have selected a contact I can click create new lead & pass the Id as a parameter to the function:

function openNewLead(SelectedID) {
      parameters["customer"] = SelectedID;
      Xrm.Utility.openEntityForm("lead", null, parameters);
}

"customer" is a lookup field Now I can use this and it populates the lookup but im not passing the full name so doesn't work correctly. if I save and refresh its fine!

So I tried:

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 doesn't work from the homepage/main screen as no reference can be added for Json

So the question is how do I reference json from here or is there a better way to write this?

Thank you

Was it helpful?

Solution 2

Solution:

New lead Button on main/homepage This calls a script to open a new form passing CrmParameter FirstSelectedItemId

function openNewLead(SelectedID) {
    if (SelectedID != null) {
        var parameters = {};

        var contact = {};
        contact.Id = SelectedID;

        var jsonContact = JSON.stringify(contact);

        var PassContactReq = new XMLHttpRequest();
        PassContactReq.open("GET", Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/ContactSet?$select=ContactId, FullName&$filter=ContactId eq guid'" + SelectedID + "'");
        PassContactReq.setRequestHeader("Accept", "application/json");
        PassContactReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        PassContactReq.onreadystatechange = function () {
            PassContact(this);
        };
        PassContactReq.send(jsonContact);

        function PassContact(PassContactReq) {
            if (PassContactReq.readyState == 4 /* complete */) {
                PassContactReq.onreadystatechange = null; //avoids memory leaks
                if (PassContactReq.status == 200) {
                    //Success

                    parameters["customer"] = JSON.parse(PassContactReq.responseText).d.results[0].ContactId;
                    parameters["customername"] = JSON.parse(PassContactReq.responseText).d.results[0].FullName;

                    Xrm.Utility.openEntityForm("lead", null, parameters);
                }
                else {
                    //Failure
                    Xrm.Utility.openEntityForm("lead");
                }
            }
        };

    } else {
        Xrm.Utility.openEntityForm("lead");
    }
}

Awesome :) thanks @Nicknow for comment!

As this was a custom lookup field the name differs for the parameters too: Ignore the "id" part of the string & no type to set.

Took too long to find this solution so hopefully it will help others :)

OTHER TIPS

Try this change

success: function (data, textStatus, XmlHttpRequest) {
                if (data.d.results.length > 0) {
                     parameters["customerid"] = SelectedID;
                     parameters["customeridname"] = data.d.results[0].FullName;
                     parameters["customeridtype"] = "contact";    
                }
            }

try add javascript actions with referenced webresources and some dummy function name. Like "isNaN". Ribbon Xml will looks like:

<Actions>
        <JavaScriptFunction FunctionName="isNaN" Library="new_json2"></JavaScriptFunction>
        <JavaScriptFunction FunctionName="isNaN" Library="new_jquery"></JavaScriptFunction>
        <JavaScriptFunction FunctionName="someFunctionUsingCoolExternalLibs" Library="new_referencinglibrary"></JavaScriptFunction>
      </Actions>

Sorry for my english :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top