Idiots Guide to Setting up a Default Unit and Price List

Hi all,

I need to set a default 'Price List' in the 'Opportunity' screen to bypass the need to save before entering the products. Now I've done some scratching around on the internet and come up with the following code:

// Call this function on Opportunity Form onLoad
function frmOnLoad() {
    // If form Type = Create
    if (Xrm.Page.ui.getFormType() == 1) {
        // 1st Parameter is Price List Name, 2nd Parameter is Price List GUID
        SetDefaultPriceList("MSC Product Set", "{BA46FE34-38BE-E211-896A-3C4A92DBDC51}");
    }
}
function SetDefaultPriceList(prmPriceListName, prmPriceListId) {
    //Create an array to set as the DataValue for the lookup control.
    var lookupData = new Array();
    //Create an Object add to the array.
    var lookupItem = new Object();
    //Set the id, typename, and name properties to the object.
    lookupItem.id = prmPriceListId;
    lookupItem.typename = "pricelevel";
    lookupItem.name = prmPriceListName;
    // Add the object to the array.
    lookupData[0] = lookupItem;
    // Set the value of the lookup field to the value of the array.
    Xrm.Page.getAttribute("pricelevelid").setValue(lookupData);
}


Problem is, I'm not sure where to put it and if I need to do anything else, unfortunetly none of the sites had any kind of walkthrough for it, just the code.

So thought I would see if there was anyone here that could help me out?

Also need to set 'Primary Unit' as the default unit on the 'Opportunity Product Window', would this be the same process?

Many thanks for all your help, it is greatly appreciated.

Kind regards

 

Nick

  • Hello Nick,

    1)  First you need to determine GUID of price list that you want to set by default, to do this — click on required price list record to open new window and press Ctrl+N. The new window will be opened with address bar, copy the link from address bar. Locate the text between %257b and %257d%, this will be GUID of desired record. For example:

    This is link I opened in my CRM
     http://192.168.1.72/Contoso/main.aspx?etc=1022&extraqs=%3f_gridType%3d1022%26etc%3d1022%26id%3d%257bDDBC78A0-49ED-E211-A28C-000C29B10F24%257d%26pagemode%3diframe%26preloadcache%3d1373893906971%26rskey%3d329111303&pagetype=entityrecord

    GUID is DDBC78A0-49ED-E211-A28C-000C29B10F24

    2) Than I changed the line in your script with correct values  SetDefaultPriceList("Test Price List", "{DDBC78A0-49ED-E211-A28C-000C29B10F24}");

    3) Now you just need to place this script on Opportunity form, to do so open Opportunity entity, select desired form, click form properties. Under form libraries click Add and in opened window click New. Type the name of the library (any name), select type script,  and click Text editor. Paste the script with correct values to the window and click OK. Click save and close. Select newly created web resourse and click Ok

    4) Create new event under Event handlers, click Add and select the library you just created.  In Function field type frmOnLoad. Click OK. Click OK. Save the Form and Publish customization.


    I am afraid you can't use the same code to set default primary unit in "Opportunity Product Window". For this task you need another function and it  should be placed on the Product form instead of Opportunity.

  • In reply to Roman Rudenko:

    Thanks Roman,

     

    Worked a treat, just need to look at sorting the default primary unit.

     

    Many thanks

     

    Nick

  • In reply to Roman Rudenko:

    Hi Roman would this code work?

     

    // Call this function on Opportunity Form onLoad
    function frmOnLoad() {
    // If form Type = Create
    if (Xrm.Page.ui.getFormType() == 1) {
    // 1st Parameter is Unit Name, 2nd Parameter is Unit GUID
    SetDefaultUnit("Primary Unit", "{B6A21FFE-268C-4A27-ADF4-F6B9DC41BC25}");
    }
    }
    function SetDefaultUnit(prmUnitName, prmUnitId) {
    //Create an array to set as the DataValue for the lookup control.
    var lookupData = new Array();
    //Create an Object add to the array.
    var lookupItem = new Object();
    //Set the id, typename, and name properties to the object.
    lookupItem.id = prmUnitId;
    lookupItem.typename = "uom";
    lookupItem.name = prmUnitName;
    // Add the object to the array.
    lookupData[0] = lookupItem;
    // Set the value of the lookup field to the value of the array.
    Xrm.Page.getAttribute("uom").setValue(lookupData);
    }

     

    Many thanks

     

    Nick

  • In reply to NeoGen:

    Hi all,

     

    The following code worked for me.

     

    // Call this function on Opportunity Form onLoad
    function frmOnLoad() {
    // If form Type = Create
    if (Xrm.Page.ui.getFormType() == 1) {
    // 1st Parameter is Unit Name, 2nd Parameter is Unit GUID
    SetDefaultUnit("Primary Unit", "{B6A21FFE-268C-4A27-ADF4-F6B9DC41BC25}");
    }
    }
    function SetDefaultUnit(prmUnitName, prmUnitId) {
    //Create an array to set as the DataValue for the lookup control.
    var lookupData = new Array();
    //Create an Object add to the array.
    var lookupItem = new Object();
    //Set the id, typename, and name properties to the object.
    lookupItem.id = prmUnitId;
    lookupItem.entityType = "uom";
    lookupItem.name = prmUnitName;
    // Add the object to the array.
    lookupData[0] = lookupItem;
    // Set the value of the lookup field to the value of the array.
    Xrm.Page.getAttribute("uomid").setValue(lookupData);
    }

     

    Regards

     

    Nick

Related
Recommended