How to give default value to lookup field

Hi All,

I created a lookup for a Email string control in sales table form to get all the emails related to a particular customer based on delivery or invoice adrdress when I open a salesorder(Using Event handler).
In the lookup I am able to get the all the mail id’s related to particular customer from logistics postal address.

I also wrote display method to get the primary mail Id when I open the salesOrder

But I am unable to use both of them same time

I want to create a lookup where customer able select and when not selecting it should automatically show the primary Mail id when selected it should show the selected one.Is it possible. Please guide me

Below are the codes I have written.


  public LogisticsLocationRecId tecDeliveryLocation()
     {
          return LogisticsPostalAddress::getLocation(this.DeliveryPostalAddress);
     }
 display Email deliveryEmail()
     {
         Email                       deliveryemail;
          LogisticsLocationRecId      locationRecId;
          LogisticsLocation           locationChild;
          LogisticsElectronicAddress  logisticsElectronicAddress;
          LogisticsLocation        Location_Inv = LogisticsLocationDefault::findSimpleDefault(CustTable::find(this.InvoiceAccount), LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Invoice));
          // See if a phone number associated with the delivery location is available
          locationRecId = this.tecDeliveryLocation();
         
         
               if (locationRecId)
               {
                    // Find a phone number associated with the postal delivery location
                    select firstonly TableId from locationChild
                where locationChild.ParentLocation  == locationRecId
                    join Location_Inv
                     where Location_Inv.RecId == locationChild.RecId
                    //&& locationChild.IsPostalAddress == NoYes::No
                    join Locator from logisticsElectronicAddress
                where logisticsElectronicAddress.Location   == locationChild.RecId
                    &&   logisticsElectronicAddress.Type       == LogisticsElectronicAddressMethodType::Email
                    &&   logisticsElectronicAddress.Locator    != '';

                    deliveryemail = logisticsElectronicAddress.Locator;
               
               }


          return deliveryemail;
     }

EventHandler for Lookup

internal final class TECSalesTableForm_EventHandler
{
     
     /// <summary>
     ///
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
     [FormControlEventHandler(formControlStr(SalesTable, DeliveryContactname_Email), FormControlEventType::Lookup)]
     public static void DeliveryContactname_Email_OnLookup(FormControl sender, FormControlEventArgs e)
     {
          Query                       lookupQuery;
          QueryBuildDataSource        queryBuildDataSource;
          QueryBuildRange             queryBuildRange;
          LogisticsLocation           Dlv_logisticsLocation;
          LogisticsElectronicAddress  Dlv_ElectronicAddress;
         //SysReferenceTableLookup    sysTableLookup;
         // FormReferenceControl       _formReferenceControl;
          SysTableLookup               sysTableLookup;
          SalesTable                  salesTable = sender.formRun().dataSource(1).cursor();

          // sysTableLookup = SysReferenceTableLookup::newParameters(tableNum(LogisticsElectronicAddress),sender , true);
          sysTableLookup = SysTableLookup::newParameters(tableNum(LogisticsElectronicAddress),sender , true);
          sysTableLookup.addLookupfield(fieldNum(LogisticsElectronicAddress, Locator));
          sysTableLookup.addLookupfield(fieldNum(LogisticsElectronicAddress, Description));

          select Dlv_logisticsLocation
    where Dlv_logisticsLocation.ParentLocation == LogisticsPostalAddress::findRecId(salesTable.DeliveryPostalAddress).Location;

          lookupQuery = new Query();
          queryBuildDataSource = lookupQuery.addDataSource(tableNum(LogisticsElectronicAddress));
          queryBuildRange = queryBuildDataSource.addRange(fieldNum(LogisticsElectronicAddress, Location));
          queryBuildRange.value(strFmt("%1",Dlv_logisticsLocation.RecId));
          queryBuildRange = queryBuildDataSource.addRange(fieldNum(LogisticsElectronicAddress, Type));
          queryBuildRange.value(enum2str(LogisticsElectronicAddressMethodType::Email));

          /////////////////////Start :Primary validation is removed on 14/07/2015 by BSIL RJ /////////
          //queryBuildRange = queryBuildDataSource.addRange(fieldNum(LogisticsElectronicAddress, IsPrimary));
          //queryBuildRange.value(enum2str(NoYes::Yes));
          ///////////////////END : 14/07/2015 //////////////////
          sysTableLookup.parmQuery(lookupQuery);
          /*Dlv_ElectronicAddress =*/ sysTableLookup.performFormLookup();
          //return Dlv_ElectronicAddress;

     }

}

Thankyou all.

What do you do with the value selected in the lookup? Do you store it in a table field? I would expect so; then you should simply show the value of the field. To initialize it, assign a value of this field (e.g. salesTable.MyField = aReference.deliveryEmail()).

Hi Martin,

Thankyou for response Martin, I am unable to understand it could you please explain it more. And I am not storing selected values in table, after selecting any value from that lookup it allowing me to save it but when I refresh the form the selected value is disappearing.

What’s your intention here? Are you saying that you want to save the value in a table field but you don’t? If so, then we’ve found the key problem. Do you already know where you want to save it?

Previously I used unbound controls now I have created fields in sales table to store the selected values.
my Intention is I have created Phone and Email fields in SalesOrder>>Header>>Address tab for both delivery address and invoice address(custom).I want to create lookup for both fields Phone and Email where it should show current customer phone and emails I am able to customize that through above code.

I am unable to show primary phone no or mail Id as a default value in the lookup without selecting when opening salesorder if customer selecting any other phone it should show or save that phone no.

Forget a lookup for a moment.

You have a table field and you want to show it with a value to in a form. What is needed to do it? The answer is: putting a value to the field. That’s it. It’s irrelevant whether the form control has a lookup or not - it’s just showing a value of the field.

See an example of a field assignment in my first reply.

A lookup is just something that helps users to set a value, instead of typing it in or pasting it from clipboard. It’s not involved in showing the current value of the field.

Hi Martin,

It is my mistake I am unable to explain it to you clearly,I am sharing the UI image of that customization below.

In the picture I selected drop down of Phone field in Invoice Address but before selecting the lookup it is showing default value which Primary phone no of that customer.If customer wants to give another number apart from his primary phone no then he can select it from the lookup. this lookup shows all the phone no related to that particular customer based on Delivery or Invoice Address

Hi Martin,

Is above customization possible in D365f&o.

You still seem to be saying that all you want to do is assigning a value of a field in code and then displaying this field in a form. That’s possible, of course - you can see many examples in the form. For example, you select a customer, which initializes several fields, such as payment terms. You see their default values, but you can change them (either by using a lookup form or by another other way, as mentioned before).

1 Like

I checked the customization from AX2012 they are using a reference group control and they are passing a field which custom created in salesTable. In that fields they are recid’s from LogisticsElectrnicAddress table. they created displaymethod and relationships using these fields in sales table.