How can I auto populate a field based on other field value

Hi All,

I have created two tables “fo_Customer” and “fO_CustomerDetails” in both tables I have created two fields “CustId” and “Name”

And created lookup in fo_CustomerDetails table using CustId field from fo_Customer.

I am trying to auto populate value in fo_CustomerDetails in Name field when I select CustId in lookup(Name related to that CustId from fo_Customer table)

but in select statement I am not getting any records selected and I wrote this code in fo_CustomerDetails table.

I would really be appreciated if anyone can help me with this.

public void modifiedField(FieldId _fieldId)
{
super(_fieldId);
FO_Customer fO_Customer;
FO_CustomerDetails fO_CustomerDetails;

    if(_fieldId == fieldNum(FO_CustomerDetails,CustID)) 

      {  
          select * from fO_Customer  
          where fO_Customer.CustID == fO_CustomerDetails.CustID;   

          if(fO_Customer)         
          {              
              this.Name = fO_Customer.Name;   
              fO_CustomerDetails.insert();       
          } 

       } 

}

First of all, consider keeping your database normalized and not to duplicate the field. If you do it, you’ll need extra development to keep the values consistent.

You should have used the debugger to see where your code failed. You’re trying to find the customer based on fO_CustomerDetails.CustID, but debugging would show you that the field never has any value. It’s because you never put any value to fO_CustomerDetails variable. Instead, use this.CustID.

The fact that you’re calling insert() in modifiedField() is a bug too. Just set the field value and leave to the user to decide whether the record should be saved or not. The standard logic will also call validateWrite(), which is needed to ensure data correctness.

By the way, implement find() method on fO_CustomerDetails table. Then, if you decide that you have to duplicate the value, your code will look like this:

public void modifiedField(FieldId _fieldId)
{
	super(_fieldId);
	
    switch (_fieldId)
	{
		case fieldNum(FO_CustomerDetails, CustID):
			FO_Customer fO_Customer = FO_Customer::find(this.CustID);
			this.Name = fO_Customer.Name;
			break;
	}
}

Hi MartinDrab,

As you said when I debugged it doesn’t showed any value in the field
but we can use table buffer to access the fields in the table right
And I didn’t understand when you said" It’s because you never put any value to fO_CustomerDetails variable." Could you please elaborate why it is not showing any value.

I created “find method” and used the code it is working perfectly
Thank you.

Yes, we use table buffers to access fields, but it doesn’t mean that all buffers in the world contain the data you need.

As with any other variable, a table buffer doesn’t contain any data if no value was assigned to it. It’s the case of your fO_CustomerDetails variable. You declared it, but you never populated with any data, either by an assignment (= operator) or a select statement. Therefore this table buffer never contains any data.

The buffer you need (the one that the method is related to) is in this variable.

1 Like

It means in the same select statement “fo_customer” table buffer is working fine but for fO_CustomerDetails I have to use “this” to get the value which is related to that particular method instead of table buffer. As a beginner in D365f&o it is little confusing to me.

But these are two completely different things. Let’s look at your code and explain what it actually does:

select * from fO_Customer  
    where fO_Customer.CustID == fO_CustomerDetails.CustID;

Here you’re saying:

  • I want a to select a record from database and put it to to fO_Customer variable.
  • I want to find the record with a given value in CustID field.
  • The ID to use is in fO_CustomerDetails.CustID. Which, In your case, is empty, so your code is an equivalent of this:
select * from fO_Customer  
    where fO_Customer.CustID == '';

To summarize it, your select statement gets a value for fO_Customer variable only. fO_CustomerDetails is used just for getting the value to filter by. The statement doesn’t put any values to fO_CustomerDetails variable.

Also, you’re wrong in suggesting that this isn’t a table buffer. It is. Of course it’s a buffer of FO_CustomerDetails table, otherwise you’d be able to get the right field for it. But it’s the buffer with the right data, while your fO_CustomerDetails variable is empty. If you wanted, you could assign this to fO_CustomerDetails (fO_CustomerDetails = this;), which proves again that their type is the same.

If you’ve never heard of this variable, you’re missing basics of object-oriented programming and you should do something about it.

1 Like