Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 79901

Javascript help

$
0
0

Hi all,

So I've got a Boolean field on case that has the same logical name as a field on lead (that field is called new_field). I've connected the case to lead using a lead lookup field called new_lead.

Now whenever I toggle the Boolean field on case I want it to update the related Lead in the Lead lookup field. In the form I've passed the 

I've then written the following Javascript:

function testFunction (executionContext,paramOne){ // paramOne will contain the field logical name (new_field)
    var formContext = executionContext.getFormContext();
    var lead = formContext.getAttribute("new_lead").getValue(); // Get the value of the lead lookup field
    if (lead !== null) { // Check if the lead lookup field contains data
        var leadId = lead[0].id.slice(1, -1); //Slice the lead ID into a usable format
        var paramOneValue = formContext.getAttribute(paramOne).getValue(); // Get the field value of the passed field name
        var entity = {};
        entity.paramOne = paramOneValue; // Trying to update a field in leads that has the same logical name. I'm trying to pass paramOne as a variable
        // Normally paramOne would be the logical name of the field on lead (e.g. new_field). But it doesn't seem to want to use the paramOne pass field name

        var req = new XMLHttpRequest();
        req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/leads("+leadId+")", true);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function() {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 204) {
                    //Success
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send(JSON.stringify(entity));
    }
}

This seems to give me a error but when I modify the JS to the below it works

function testFunction (executionContext,paramOne){ // paramOne will contain the field logical name (new_field)
    var formContext = executionContext.getFormContext();
    var lead = formContext.getAttribute("new_lead").getValue(); // Get the value of the lead lookup field
    if (lead !== null) { // Check if the lead lookup field contains data
        var leadId = lead[0].id.slice(1, -1); //Slice the lead ID into a usable format
        var paramOneValue = formContext.getAttribute(paramOne).getValue(); // Get the field value of the passed field name
        var entity = {};
        entity.new_field = paramOneValue; // This seems to work if I replace paramOne with the actual lead field logical name

        var req = new XMLHttpRequest();
        req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/leads("+leadId+")", true);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function() {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 204) {
                    //Success
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send(JSON.stringify(entity));
    }
}

It seems it won't pass the parameter field logical name into my PATCH request.

Is there anyway to dynamically sync this with the passed paramOne?


Viewing all articles
Browse latest Browse all 79901

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>