locked
Set Custom Entity Field Value RRS feed

  • Question

  • I have a custom button on my dashboard and all other ribbons of my CRM 2011 online:

    • Onclick of the custom button a Java Script function is called.
    • The Purpose of this function is to set the value of a custom entity field. 

    Now problem is that for example:

    If I am on contact entity form then on clicking the button how I will change the value of the custom entity field?

    Friday, October 4, 2013 6:04 AM

All replies

  • I would think the easiest way would be to trigger a workflow from your JavaScript and have the workflow handle updating the value on your custom entity.

    http://www.mscrmconsultant.com/2013/03/execute-workflow-using-javascript-in.html


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism


    Friday, October 4, 2013 7:23 AM
  • Thanx Paul, so Inorder to execute a workflow I am required to create a workflow which will update my custom entity field value , right ?
    Friday, October 4, 2013 7:40 AM
  • Hi,

    Assuming  - You have a successful call to your Javascript function from the ribbon button on the Contact Form. (If not, please let me know)

    Before the call to custom entity is made you need to have the Guid of the custom entity record (This may be a field on your contact form. If not, please clarify the scenario).

    Once you have the Guid of the custom entity record that you want to update, you can make an ajax call to retrieve the custom entity object and then update with the X-HTTP-METHOD as MERGE.

    You can refer the JQuery sample at "SDK\samplecode\js\restendpoint\jqueryrestdataoperations" or "SDK\samplecode\js\restendpoint\restjquerycontacteditor"

    For Example:

    function Update_CustomEntity() {
        var customentityfieldoncontactform = Xrm.Page.getAttribute("customfieldname");
        if (customentityfieldoncontactform == null || customentityfieldoncontactform.getValue() == null) return;
    
        var customentityfieldoncontactformId = customentityfieldoncontactform.getValue()[0].id;
    
        $.ajax({
            type: "GET",
            async: false,                         // Making Sync Call
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/CustomEntitySet?$filter=CustomEntityPrimaryFieldId eq guid'" + customentityfieldoncontactformId + "'",
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data, textStatus, XmlHttpRequest) {
                record = data.d.results[0];
                var recordId = record.Id;
    
                    if recordId != null)
                        updatecustomentity(recordId, record, "CustomEntitySet", CustomEntityUpdateCompleted, null);
                }
                
                data = null;
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                errorHandler(XMLHttpRequest, textStatus, errorThrown);
            }
        });
    
    }
    function CustomEntityUpdateCompleted(data, textStatus, XmlHttpRequest) {
        alert("Update complete.");
    }
    
    
    function updatecustomentity(id, entityObject, odataSetName, successCallback, errorCallback) {
        if (!id) {
            alert("record id is required.");
            return;
        }
        else
        { id = encodeURIComponent(id); }
        
        if (!odataSetName) {
            alert("odataSetName is required.");
            return;
        }
        else
        { odataSetName = encodeURIComponent(odataSetName); }
    
        if (!entityObject) {
            alert("entityObject is required.");
            return;
        }
    
        var jsonEntity = window.JSON.stringify(entityObject);
    
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            data: jsonEntity,
            url: Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc" + "/" + odataSetName + "(guid'" + id + "')",
            beforeSend: function (XMLHttpRequest) {          
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
                XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
            },
            success: function (data, textStatus, XmlHttpRequest) {
                data = new Object();
                data.id = id;
                if (successCallback) {
                    successCallback(data, textStatus, XmlHttpRequest);
                }
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                if (errorCallback)
                    errorCallback(XmlHttpRequest, textStatus, errorThrown);
                else
                    errorHandler(XmlHttpRequest, textStatus, errorThrown);
            }
        });
    }
    
    function errorHandler(XMLHttpRequest, textStatus, errorThrown) {
        if (XMLHttpRequest.status == 12029)
        { return new Error("The attempt to connect to the server failed."); }
        if (XMLHttpRequest.status == 12007)
        { return new Error("The server name could not be resolved."); }
        var errorText;
        try
            { errorText = JSON.parse(XMLHttpRequest.responseText).error.message.value; }
        catch (e)
            { errorText = XMLHttpRequest.responseText }
        alert("Error : " +
            XMLHttpRequest.status + ": " +
            XMLHttpRequest.statusText + ": " + errorText);
        error = null;
        XMLHttpRequest = null;
    }
    The part of code that you need to update with your custom entity field are in bold.

    Moreover, before you run your Javascript function, remember to include the json.js and jquery.js files in your form library.

    In case you need more help, do let me know.

    I hope this will resolve your issue.

    Thanks,


    Anubhav Bajpai

    Friday, October 4, 2013 7:42 AM
  • I have button on contact entity form but my custom entity field is not on the contact form. that is the real issue'
    Friday, October 4, 2013 7:45 AM
  • If you did a workflow you would need to just run the workflow on the record of the custom entity you want to update.

    This would mean you need to know the ID of the workflow, and also the ID of the custom entity record.

    Once you have these (easiest way is to just hard-code them as long as they won't ever change), then you can trigger the workflow against the custom entity record, no matter where the button is clicked from.


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Friday, October 4, 2013 7:51 AM
  • Cool, Have you identified which custom entity record while being on the Contact Form you need to update. If yes, let me know the complete scenario.

    Thanks,


    Anubhav Bajpai


    Friday, October 4, 2013 7:52 AM
  • On clicking the button on Contact form -- > a custom entity 'ccs' has a field "new_response", I have to store the response in this field.
    Friday, October 4, 2013 8:05 AM
  • how do you relate CCS custom entity with your contact record. What is the relationship between the two?

    Are you updating any constant record with updates to the same field?(This doesn't makes sense)

    if the relationship is 1:M (Contact : CCS), then are you creating records for CCS any point of time and want to update the field of that record?

    As mentioned in my first post, Before trying to update the field of your custom entity you need to have the id of that entity record.

    I would really appreciate if you can share the complete scenario of how you are relating the records then it would be helpful to provide the solution. Since the custom record id is still unidentified, how can you update it.

    Thanks,


    Anubhav Bajpai

    Friday, October 4, 2013 8:51 AM
  • Thanx Anubhav'
    Friday, October 4, 2013 9:41 AM