web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

JavaScript to Retrieve Field Values from Opportunity and Pass to Opportunity Close as Text Fields

(0) ShareShare
ReportReport
Posted on by 10

We have a need in our process to pass information from three fields in the Opportunity record to the Opportunity Close form.  Two of these fields are Lookup fields to other custom entities while one is an option set.  This information was to passed through JavaScript but I am having issues pulling the text from the lookup fields and so must have something wrong.  (I'm a bit new to JavaScript).  I'm hoping someone might be able to help with this coding or perhaps there is a better way to pass this information over.  Below is a sample of the JavaScript.  Thanks.

function getRelatedInfo(e) {

    // Get the Form Context
    var formContext = e.getFormContext();

    // Set variables for the related entity to get data from
    try{var recordGuid = formContext.getAttribute("opportunityid").getValue()[0].id; }
    catch(err) {}

    // If the recordGuid contains data
    if (recordGuid != null) {
   
        // Use the WebApi to get data    
        Xrm.WebApi.retrieveRecord("opportunity", recordGuid, "?$select=z2t_optype&$expand=z2t_Division($select=z2t_name),z2t_Make($select=z2t_name)").then(
           
            // If successful, set variables for the results
            function success(result) {
                var OpType = result.z2t_optype;

                if (result.hasOwnProperty("z2t_Division") && result["z2t_Division"] !== null) {
                    var Division = result["z2t_Division"]["z2t_name"]; // Text
                }
                if (result.hasOwnProperty("z2t_Make") && result["z2t_Make"] !== null) {
                    var Make = result["z2t_Make"]["z2t_name"]; // Text
                };
           
       

                // Set the form fields
                formContext.getAttribute("gp_opportunitytype").setValue(OpType);
                formContext.getAttribute("gp_Division").setValue(z2t_Division_z2t_name);
                formContext.getAttribute("gp_Make").setValue(z2t_Make_z2t_name);
       

            },                                                  
                       
I have the same question (0)
  • David A.-GP Profile Picture
    10 on at

    As a follow-up, I have also attempted this snippet of JavaScript with no success.  Through none of these efforts have I been able to set the value of the text fields from the results of the lookups.

     // Use the WebApi to get data    

           Xrm.WebApi.retrieveRecord("opportunity", recordGuid, "?$select=_z2t_division_value,_z2t_make_value,z2t_optype").then(

               // If successful, set variables for the results

               function success(result) {

                   var opportunityid = result["opportunityid"]; // Guid

                   var z2t_division = result["_z2t_division_value"]; // Lookup

                   var z2t_division_formatted = result["_z2t_division_value@OData.Community.Display.V1.FormattedValue"];

                   var z2t_division_lookuplogicalname = result["_z2t_division_value@Microsoft.Dynamics.CRM.lookuplogicalname"];

                   var z2t_make = result["_z2t_make_value"]; // Lookup

                   var z2t_make_formatted = result["_z2t_make_value@OData.Community.Display.V1.FormattedValue"];

                   var z2t_make_lookuplogicalname = result["_z2t_make_value@Microsoft.Dynamics.CRM.lookuplogicalname"];

                   var OpType = result["z2t_optype"]; // Choice

                   var z2t_optype_formatted = result["z2t_optype@OData.Community.Display.V1.FormattedValue"];

                   // Set the form fields

                   formContext.getAttribute("gp_opportunitytype").setValue(OpType);

                   formContext.getAttribute("gp_Division").setValue(z2t_division_lookuplogicalname);

                   formContext.getAttribute("gp_Make").setValue(z2t_make_lookuplogicalname);

  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at

    Hi,

    You will find bellow an updated version of your code with annotations.

    // async here permits to use await in Xrm.WebApi.retrieveRecord
    async function getRelatedInfo(e) {
        debugger; // The browser will stop here when you open the form with the debugger open (F12). Use it to help you debug.
        const formContext = e.getFormContext();
    
        try {
            // Get OpportunityId.
            const opportunityAttr = formContext.getAttribute("opportunityid");
            if (opportunityAttr == null) {
                Xrm.Navigation.openAlertDialog({ text: "Opportunity field is missing." });
                return;
            }
    
            const opportunityRef = opportunityAttr.getValue()[0];
        
            // Retrieves the Opportunity record with an await, if an error occurs, the catch block will be executed.
            const opportunity = await Xrm.WebApi.retrieveRecord(
                "opportunity",
                opportunityRef.id,
                // Could you ensure that the fields are correct?
                "?$select=z2t_optype&$expand=z2t_Division($select=z2t_name),z2t_Make($select=z2t_name)"
            );
    
            if (opportunity == null) {
                Xrm.Navigation.openAlertDialog({ text: "Opportunity cannot be found." });
                return;
            }
    
            const opType  = opportunity.z2t_optype;
            const divisionName = opportunity.z2t_Division?.z2t_name;
            const makeName = opportunity.z2t_Make?.z2t_name;
    
            // Set the form fields
            if (opType != null) {
                formContext.getAttribute("gp_opportunitytype").setValue(opType); // Ensure that the field name is correct.
            }
    
            if (divisionName != null) {
                formContext.getAttribute("gp_division").setValue(divisionName); // gp_division or gp_Division ? Ensure that gp_division is a string field.
            }
    
            if (makeName != null) {
                formContext.getAttribute("gp_make").setValue(makeName); // gp_make or gp_Make ? Ensure that gp_make is a string field.
            }
        }
        catch (error) {
            Xrm.Navigation.openAlertDialog({ text: `An unexpected error occured: ${error.message}. Stack: ${error.stack}` });
        }
    }

    Can you share the error message if it occurs?

  • David A.-GP Profile Picture
    10 on at

    I rechecked the fields and had uppercase in the destination field (gp_Division vs gp_division).  The second snippet of code is what is actually working for me.  Thank you for your response.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans