0

I'm working on a form 'Enrollment Adoptions' in CRM 2015, specifically with three fields; Product, Component, and Editorial. 'Product' is a lookup field referencing the Product Entity. 'Component' and 'Editorial' are simple text fields to be filled using javascript. Each product in the Product Entity has a corresponding component and editorial, which are OptionSets in the product form.

The goal: I want a function to trigger OnChange with 'Product' that looks to that specific product record, gets its corresponding component and editorial info, and auto-fills the 'Component' and 'Editorial' fields in 'Enrollment Adoptions'.

The problem: I can't seem to figure out how to get the value from the "product' lookup field and use it to get the information I need.

Here's a bit of code I've been testing:

function populateProduct(blank)
{
    if (blank != null || blank != undefined)
    {
        var productName;
        var product = Xrm.Page.getAttribute(blank);
        if (product.getValue != null || product.getValue != undefined)
        {
            productName = product.getValue()[0].name;
            console.log("the product name is " + productName);
            var componentXml = "<fetch mapping='logical'>"+"<entity name='product'>"+"<attribute name='new_component'/>"+"<filter>"+"<condition attribute = 'name' operator='eq' value='"+productName+"' />"+"</filter>"+"</entity>"+"</fetch>";
            var fetchComponent = XrmServiceToolkit.Soap.Fetch(componentXml);
            console.log("the respective component is " + fetchComponent);
        }
    }
}

The log statements are just to test if I'm getting the info I need while testing.

The log statements show me that I'm getting the product name for sure but I'm not getting the corresponding component.

I looked into building queries using Fetch but I don't think I understand them correctly because it isn't getting the info I'm hoping for. Obviously this is very much an incomplete function but I wanted to make sure I was getting the information I needed before writing out the rest.

Any advice?

Guido Preite
  • 14,905
  • 4
  • 36
  • 65
  • That fetch statement looks fine (though I would switch to retrieving by product id instead of name). Open Fiddler and inspect the response to your fetch call. If the response is not an error and contains the information you want, then it's just a matter of figuring out how to correctly access the response object. If the response is an error then you know something is wrong with the fetch. – Polshgiant Apr 26 '16 at 21:59
  • Forgive my ignorance but what is Fiddler? Also, would you know how to correctly access the object? The second log statement in my code gives me "the respective component is [object Object]" – Carlos Farmer Apr 27 '16 at 12:39

1 Answers1

0

The response from the fetch call returns objects that you have to dig into, like this:

var componentXml = [
    "<fetch mapping='logical'>",
        "<entity name='product'>",
            "<attribute name='new_component'/>",
            "<filter>",
                "<condition attribute = 'name' operator='eq' value='"+productName+"' />",
            "</filter>",
        "</entity>",
    "</fetch>"].join('');
var products = XrmServiceToolkit.Soap.Fetch(componentXml);

if (products.length > 0) {
    if (products[0].attributes.productid != undefined)
        console.log("the product id is", products[0].attributes.productid);
        console.log("new_component is", products[0].attributes.new_component);
    }
}
Polshgiant
  • 3,595
  • 1
  • 22
  • 25