How can I populate fields with information related to single lookup field?

220 views Asked by At

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?

1

There are 1 answers

0
Polshgiant On

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);
    }
}