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 :
Microsoft Dynamics CRM (Archived)

How to determine, on form load, the entity type for a field, then use that field to set another read-only field.

(0) ShareShare
ReportReport
Posted on by 730

Hello community,

This is my first post.  I've been using CRM 2011 for two weeks, and the solutions to many of my problems have been found by searching this forum, so thank you.

I have a custom field for Quote, called "Quote For" (id - new_QuoteForEntity) that comes from an option set with (currently) two possibilities.  I'd like that if a user creates an opportunity from an account or a contact that it automatically sets this field.  I have some code written in jScript below, set to occur onFormLoad, but it fails when the form loads, and I'm not sure why (likely cause I've never written in java before yesterday).  Your help would be much appreciated.

function setQuoteFor(){

var lookupItem = new Array();

lookupItem = XRM.Page.getAttribute("customerid").getValue();

if(lookup != null){

var output = newArray();

output[0] = new Object();

if (lookup[0].entityType == "contact"){

output[0].Value = 100,000,000;

output[0].Label = "Contact";

}

if (lookup[0].entityType == "account"){

output[0].Value = 100,000,001;

output[0].Label = "Account";

}

Xrm.Page.getAttribute("new_QuoteForEntity").setValue(output);

}

}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    jlattimer Profile Picture
    24,562 on at

    I think you can simplify it down to this:

    function setQuoteFor() {
        var lookup = Xrm.Page.getAttribute("customerid").getValue();
        if (lookup != null) {
            if (lookup[0].entityType == "contact") {
                Xrm.Page.getAttribute("new_quoteforentity").setValue(100000000);
            }
            if (lookup[0].entityType == "account") {
                Xrm.Page.getAttribute("new_quoteforentity").setValue(100000001);
            }
        }
    }
    

    A couple things to note:

    JavaScript is case sensitive so XRM is not the same as Xrm  - Xrm is correct

    Field names - are all lower case - on the screen showing the field data under customizations - use the Name field

    Option set fields can be set with just the integer value - since it is a number there won't be any commas

    You started out referencing "lookupItem" and then went to "lookup"

    But you are on the right track :)

  • mfrankel Profile Picture
    730 on at

    That worked perfectly, thank you Jason.

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans