locked
Quick Create Contact RRS feed

  • Question

  • I copied this off Gareth Tucker’s web-site, however, I’m battling a bit and therefore this post.

    The Script below works fine, except for the line >> CRMObject.parentcustomerid = lookupid; <<

    I can’t set CustomerPartentId when creating the contact.

    Can anyone help?

    function CreateNewContact() {

        //Get the lookup field
        lookupfield = Xrm.Page.getAttribute("regardingobjectid").getValue();

        //This will get the lookup field guid if there is value present in the lookup
        if (lookupfield != null) {
        var lookupid = lookupfield[0].id;
        }
        //Else the function will return and no code will be executed.
        else {
        return;
        }


        var context = Xrm.Page.context;

        var serverUrl = context.getServerUrl();

        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";

        var CRMObject = new Object();


        /////////////////////////////////////////////////////////////

        // Specify the ODATA entity collection

        var ODATA_EntityCollection = "/ContactSet";

       /////////////////////////////////////////////////////////////

        // Define attribute values for the CRM object you want created

        CRMObject.LastName = "FREDDY";
        CRMObject.parentcustomerid = lookupid;


     
        //Parse the entity object into JSON

        var jsonEntity = window.JSON.stringify(CRMObject);

     
        //Asynchronous AJAX function to Create a CRM record using OData

        $.ajax({ type: "POST",

            contentType: "application/json; charset=utf-8",

            datatype: "json",

            url: serverUrl + 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"];

                alert("CRM GUID created: " + NewCRMRecordCreated.ContactId);

            },

            error: function (XMLHttpRequest, textStatus, errorThrown) {

                alert("failure");

            }

        });

    }

    Monday, April 23, 2012 7:36 PM

Answers

  • Try this:

    CRMObject.ParentCustomerId = { Id: lookupfield[0].id, LogicalName: lookupfield[0].entityType, Name: lookupfield[0].name };
    The ParentCustomerId (all attributes via OData/REST use the schema name which is mixed case - from memory I think this is how ParentCustomerId is defined but you can verify via customisations if my memory is dodgy) is an entity reference, so you have to supply an object with the above defined properties.


    --pogo (pat) @ pogo69.wordpress.com

    Monday, April 23, 2012 7:58 PM

All replies

  • Try this:

    CRMObject.ParentCustomerId = { Id: lookupfield[0].id, LogicalName: lookupfield[0].entityType, Name: lookupfield[0].name };
    The ParentCustomerId (all attributes via OData/REST use the schema name which is mixed case - from memory I think this is how ParentCustomerId is defined but you can verify via customisations if my memory is dodgy) is an entity reference, so you have to supply an object with the above defined properties.


    --pogo (pat) @ pogo69.wordpress.com

    Monday, April 23, 2012 7:58 PM

  • Use the below code to set the values of Lookup
    //Set a lookup value
        CRMObject.PrimaryContactId = { Id: lookupid, LogicalName: "contact", Name: 'Susan Burk (sample)' };
     or just replace your CRMObject.parentcustomerid = lookupid; with
    CRMObject.parentcustomerid = lookupfield;


    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!


    Monday, April 23, 2012 8:31 PM