Setting the default value of a reference field

CharlesR1
Kilo Guru

Hello all,

Hopefully a quick one. On our Purchase Order Line Item table, we need to set the default value on the Vendor field to be the same as the Vendor field on the parent Purchase Order. Both fields are references to the same 'Company' table, but we can't seem to get the selected vendor on the PO record to appear when a new line item is opened.

In the Default value section, we are using 'javascript:current.vendor = current.purchase_order.vendor.toString()', which we thought would pass the sys_id of the value, but it does not.

Any suggestions?

Thanks,

Charles

1 ACCEPTED SOLUTION

If you still cannot get the default value to work another alternative is to use a display business rule to save the sys_id and display value of the vendor in the g_scratchpad, then populate it with an onLoad client script.



Example BR:



When: Display


g_scratchpad.vendorID = current.purchase_order.vendor;


g_scratchpad.vendorName = current.purchase_order.vendor.getDisplayValue();



Example CS:



function onLoad() {


        g_form.setValue('vendor', g_scratchpad.vendorID, g_scratchpad.vendorName);


}



http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices


View solution in original post

18 REPLIES 18

Thanks Chuck - good to know!


Chuck Tomasi
Tera Patron

The default value is already going to do the assignment to the vendor field.   Have you tried



javascript:current.purchase_order.vendor


Hi Chuck, unfortunately this is not working for us either!



Thanks,



Charles


Thanks for the update. I'm trying to understand the data structure. You want the (purchase order line item) default value for vendor to be the same as the parent purchase order vendor.. What is the field on the purchase order line item record that relates it to the parent purchase order? (Sorry, I don't have these plugins activated at present.)



Is it parent? purchase_order?



Once you know that, you can either set it in the default value or use a before business rule to set it for you.



E.g.



Name: Save parent Vendor


Table: Purchase Order Line Item


Active: true


Insert: true


Update: false


Advanced: true


Condition: (empty)


Script;




(function executeRule(current, previous /*null when async*/) {


      current.vendor = current.parent.vendor; // Assuming 'parent' is the field with the purchase order value


})(current, previous);


Hello Chuck,



The field on the line item form is 'purchase_order'.



The only issue we have with using a before business rule is that we don't want the field to be blank when the new PO Line item is opened - we need to to be pre-populated to prevent the customers from selecting the wrong one. As the before BR will only populate this when the new item is saved, it will not work for us.



Thanks again for your help.



Ch