locked
Simple Javascript Question RRS feed

  • Question

  • I've been searching around but always find really complicated solutions to these things, so I am hoping someone here can give me a simple answer - and if the solution is more complicated, at least give me some pictures showing me how to do it.

    Let's start with this one: We only have one price list, and would like the "Price List" lookup box for making new quotes to always have that value filled in.  Ideally it would still be changeable in the event that we make another price list in the future, but this would save our salespeople from constantly having to enter the value for every quote they make, since it's a required field.

    I know the GUID of the price list, for sake of discussion let's just say it's {ABC}.  I don't need to do any lookups, since I already know what I want the value to be.  I'm assuming I can do this with an OnLoad event for the form?  I made an OnLoad event called new_setpricelist, but am not sure what code I would need to use in that box.

    Another one I want to set is the name of the quote.  I would like it to automatically fill in "Quote for [customer name]", since ultimately the names that quotes have are unimportant anyway.  I tried making a workflow that would set it, and it does work, but it won't be able to do anything until after the record is already created, meaning I have to type something in for the name, and then it will be changed by the workflow.  Can this sort of thing also be done with an OnLoad?

    Thanks in advance for any help.  I'm not very good at programming so I need all the help I can get.

    Monday, February 4, 2013 5:29 PM

Answers

  • Something like this would work - but keep in mind since the GUID is hard-coded it is not portable between organizations.

    Attach to the form's OnLoad event - should populate the price list field when creating a new record. 

    function OnLoad() {
        if (Xrm.Page.ui.getFormType() == 1) {
            Xrm.Page.getAttribute("pricelevelid").setValue(GetDefaultPriceList());
        }
    }
    
    function GetDefaultPriceList() {
        var value = new Array();
        value[0] = new Object();
        value[0].id = "AC22410E-4333-E211-B5F5-78E7D162EE7D";
        value[0].name = "Main Price List Name Goes Here";
        value[0].entityType = "pricelevel";
        return value;
    }


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Monday, February 4, 2013 5:42 PM
    Moderator
  • For setting the name - you can make the field read-only and then use a script like this on the form's OnSave event.

    function OnSave() {
        var customer = Xrm.Page.getAttribute("customerid").getValue();
        Xrm.Page.getAttribute("name").setValue("Quote for " + customer[0].name);
        Xrm.Page.getAttribute("name").setSubmitMode("always");
    }


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Monday, February 4, 2013 5:46 PM
    Moderator
  • You could set the name OnLoad - just add check to make sure it is populated in case they don't create it from another record. 

    Here's an example of setting the name OnLoad and setting a date field to tomorrow's date. You can use the same library - typically it is easier I think to group all the code for an entity in a single library if it isn't shared among other entity types).

    function OnLoad() {
        if (Xrm.Page.ui.getFormType() == 1) {
            Xrm.Page.getAttribute("pricelevelid").setValue(GetDefaultPriceList());
            var customer = Xrm.Page.getAttribute("customerid").getValue();
            if (customer != null) {
                Xrm.Page.getAttribute("name").setValue("Quote for " + customer[0].name);
            }
    	var today = new Date();
    	var tomorrow = new Date(today.setDate(today.getDate() + 1));
    	Xrm.Page.getAttribute("new_orderdate").setValue(tomorrow);
        }
    }
    
    function GetDefaultPriceList() {
        var value = new Array();
        value[0] = new Object();
        value[0].id = "AC22410E-4333-E211-B5F5-78E7D162EE7D";
        value[0].name = "Main Price List Name Goes Here";
        value[0].entityType = "pricelevel";
        return value;
    }


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    • Proposed as answer by Adam Vero Monday, February 4, 2013 10:02 PM
    • Marked as answer by drfsupercenter Monday, February 4, 2013 10:39 PM
    Monday, February 4, 2013 7:16 PM
    Moderator
  • Yes - you can add more lines to set additional date fields. Setting to today's date could be as simple as:

    Xrm.Page.getAttribute("new_orderdate").setValue(new Date());

    The .getDate() gets the day number from the date variable. 

    For non-lookup fields you can just use .setValue(value)

    Lookups are special because the value they contain isn't just a single value but rather an array (multiple) of values. You don't have to use a separate function to set, you could just as easily do something like this:

    function OnLoad() {
        if (Xrm.Page.ui.getFormType() == 1) {
            var value = new Array();
            value[0] = new Object();
            value[0].id = "AC22410E-4333-E211-B5F5-78E7D162EE7D";
            value[0].name = "Main Price List Name Goes Here";
            value[0].entityType = "pricelevel";
            Xrm.Page.getAttribute("pricelevelid").setValue(value);
        }
    }

    Sometimes it makes things more readable to break things out but you need to specify the GUID, the entity type and the friendly name.

    As far as changing colors, to me recollection I haven't seen anything that works well.

    Good luck. 


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    • Proposed as answer by Adam Vero Monday, February 4, 2013 10:02 PM
    • Marked as answer by drfsupercenter Monday, February 4, 2013 10:39 PM
    Monday, February 4, 2013 7:46 PM
    Moderator

All replies

  • Something like this would work - but keep in mind since the GUID is hard-coded it is not portable between organizations.

    Attach to the form's OnLoad event - should populate the price list field when creating a new record. 

    function OnLoad() {
        if (Xrm.Page.ui.getFormType() == 1) {
            Xrm.Page.getAttribute("pricelevelid").setValue(GetDefaultPriceList());
        }
    }
    
    function GetDefaultPriceList() {
        var value = new Array();
        value[0] = new Object();
        value[0].id = "AC22410E-4333-E211-B5F5-78E7D162EE7D";
        value[0].name = "Main Price List Name Goes Here";
        value[0].entityType = "pricelevel";
        return value;
    }


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Monday, February 4, 2013 5:42 PM
    Moderator
  • For setting the name - you can make the field read-only and then use a script like this on the form's OnSave event.

    function OnSave() {
        var customer = Xrm.Page.getAttribute("customerid").getValue();
        Xrm.Page.getAttribute("name").setValue("Quote for " + customer[0].name);
        Xrm.Page.getAttribute("name").setSubmitMode("always");
    }


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Monday, February 4, 2013 5:46 PM
    Moderator
  • Thanks for your quick replies.

    I think I'm putting this in the wrong place.  Can you tell me where it needs to go?  Here's my "Customize the System" view for Quotes:

    So you can see it's under the main form view, under that form's properties.  Do I need it under event handlers? If so, what's supposed to be put in form libraries? The terminology is a tad confusing.


    Monday, February 4, 2013 6:32 PM
  • I think you are just missing the step where you bind the function to the event.

    In your screenshot - click "Add" beneath Control - Form & Event - OnLoad

    Use A JavaScript Web Resource In Your Form


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Monday, February 4, 2013 7:01 PM
    Moderator
  • Aha. Thanks.  I just added the "OnLoad" function and it worked.

    A couple more little things.

    1. Say we create a quote from an account or contact record.  There are already mappings set up so it will map the address, etc.  In that case, the customer's name is already there, so is it possible to create the quote name instantly?  I can understand why you'd need to do an "on save" event in the case where you create one from scratch, but I doubt we will be doing that.

    2. There are a couple custom fields, such as "Order Date" that we are using for internal tracking purposes.  Can those be set to "today's date", or "today's date plus one day" using an OnLoad function?  If so, does it need to be its own form library? And what would the code look like if the field name is new_orderdate?

    Monday, February 4, 2013 7:06 PM
  • You could set the name OnLoad - just add check to make sure it is populated in case they don't create it from another record. 

    Here's an example of setting the name OnLoad and setting a date field to tomorrow's date. You can use the same library - typically it is easier I think to group all the code for an entity in a single library if it isn't shared among other entity types).

    function OnLoad() {
        if (Xrm.Page.ui.getFormType() == 1) {
            Xrm.Page.getAttribute("pricelevelid").setValue(GetDefaultPriceList());
            var customer = Xrm.Page.getAttribute("customerid").getValue();
            if (customer != null) {
                Xrm.Page.getAttribute("name").setValue("Quote for " + customer[0].name);
            }
    	var today = new Date();
    	var tomorrow = new Date(today.setDate(today.getDate() + 1));
    	Xrm.Page.getAttribute("new_orderdate").setValue(tomorrow);
        }
    }
    
    function GetDefaultPriceList() {
        var value = new Array();
        value[0] = new Object();
        value[0].id = "AC22410E-4333-E211-B5F5-78E7D162EE7D";
        value[0].name = "Main Price List Name Goes Here";
        value[0].entityType = "pricelevel";
        return value;
    }


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    • Proposed as answer by Adam Vero Monday, February 4, 2013 10:02 PM
    • Marked as answer by drfsupercenter Monday, February 4, 2013 10:39 PM
    Monday, February 4, 2013 7:16 PM
    Moderator
  • Cool, thanks.

    I take it I can just add more of the Xrm.Page.getAttribute lines for other date-related fields?  If I wanted to do today instead of tomorrow, can I just do setValue(today.getDate()) ?

    So basically, all I need to do for the OnLoad functions is do an Xrm.Page.getAttribute("attribute name").setValue(value)?  As far as lookups go... what would happen if I just put the GUID in for setValue? Would that break it, and I need to do a separate function even when I know what I want to set it to?

    Thanks for all the help though.  I appreciate it.  I'm tired of seeing "here's a guide, go figure the rest out yourself", since there are literally millions of different ways you can do everything and it's too easy to break the form that way.

    On a related note, you don't happen to know a way to make CRM 2011 show its read-only fields in a more readable color, do you? I remember trying to do that before, and it was determined that the OnLoad script to change the colors only worked in CRM 4.0 and lower, that 2011 doesn't let you do it...

    Monday, February 4, 2013 7:32 PM
  • Yes - you can add more lines to set additional date fields. Setting to today's date could be as simple as:

    Xrm.Page.getAttribute("new_orderdate").setValue(new Date());

    The .getDate() gets the day number from the date variable. 

    For non-lookup fields you can just use .setValue(value)

    Lookups are special because the value they contain isn't just a single value but rather an array (multiple) of values. You don't have to use a separate function to set, you could just as easily do something like this:

    function OnLoad() {
        if (Xrm.Page.ui.getFormType() == 1) {
            var value = new Array();
            value[0] = new Object();
            value[0].id = "AC22410E-4333-E211-B5F5-78E7D162EE7D";
            value[0].name = "Main Price List Name Goes Here";
            value[0].entityType = "pricelevel";
            Xrm.Page.getAttribute("pricelevelid").setValue(value);
        }
    }

    Sometimes it makes things more readable to break things out but you need to specify the GUID, the entity type and the friendly name.

    As far as changing colors, to me recollection I haven't seen anything that works well.

    Good luck. 


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    • Proposed as answer by Adam Vero Monday, February 4, 2013 10:02 PM
    • Marked as answer by drfsupercenter Monday, February 4, 2013 10:39 PM
    Monday, February 4, 2013 7:46 PM
    Moderator
  • Thank you for all the help.
    Monday, February 4, 2013 7:53 PM