Auto Populate the Lookup Value

When creating a Sales Order and selecting a customer account, I’d like the corresponding sales agreement to populate automatically. This means that upon choosing the customer account from the dropdown menu, the associated sales agreement should automatically fill in the respective field without requiring additional manual input.

All right, so what do you need from us? Do you have a question?

1 Like

I need that dropdown to be filled automatically once i select the CustAccount

Are you saying that you neither have any idea how to do it, nor you know how to write any part of the code, e.g. you don’t know how to read a field value? Or do you have a particular problem but you forgot to explain what it is?

The first step is learning how to execute logic when the customer account number changes. Doing it just when a value is selected from a lookup would be a mistake, because the user can type in or paste the value as well. You should do it on modification, regardless of the way. Because the control is bound to a data source field, it will the best to do it there. For example, you can create an extension class for CustAccount field of SalesTable data source and create a CoC extension of modified() method. (An alternative is using an event handler.) When you know how to execute the logic, we can move into what we need to do there.

2 Likes

yes I did write a extension class for CustAccount field of salestable datasource as you said,But there are few errors. What I want is when I create a sales order and select CustAccount the salesAgrement should Autofill.

[ExtensionOf(formDataFieldStr(SalesCreateOrder,SalesTable,CustAccount))]
final class FormDataField_IBS_Extension
{
public void modified()
{
FormDataObject formDataObject = any2Object(this) as FormDataObject;
FormDataSource formDataSource = formDataObject.datasource();
SalesTable salesTable;

    next modified();

    salesTable = formDataSource.cursor();
    
    // Check if there is a valid CustAccount
    if (salesTable.CustAccount)
    {
        SalesAgreementHeader salesAgreementHeader;
        CustTable custTable;
        

        // Assuming there is a method to find the appropriate SalesAgreementHeader
        salesAgreementHeader.SalesNumberSequence = SalesAgreementHeader::existForCustomer(salesTable.CustAccount);

        // Assuming SalesAgreementId is a field in SalesAgreementHeader
        salesagreementId = salesAgreementHeader.SalesNumberSequence;
    }
    else
    {
        salesagreementId = ''; // Set SalesAgreementId to blank if there is no CustAccount
    }

}

}

Please tell us more about the problem than just “there are few errors”.

Also, shouldn’t you assign the value to the Sales agreeement ID field on SalesTable?

These are the errors, Yes I need to assign the value to the Sales agreeement ID field on SalesTable but there is no Sales agreeement ID field in SalesTable, the form control has a method named editSalesAgrementId.
|Error||Cannot implicitly convert from type ‘str’ to type
|Error||Cannot implicitly convert from type ‘boolean’ to type ‘str’.
|Error||‘SaleNumberSequence’ is not declared.

Look at where the messages came from. There is no SaleNumberSequence in the code above, therefore that’s a bug somewhere else.

1 Like

so how should the code for this look like? can you give me an example

First of all, fix the compilation errors, because even perfect code is useless if you can’t compile and run the application before of compilation errors in other code.

Regarding editSalesAgrementId(), you can simply execute it. The first parameter will be true and the other the agreement ID.

1 Like

I have no idea how to write a code for this so can you give me a framework please, so when I select CustAccount the SalesagreementId should Autofill

Try something like this. Notice not only the call of editSalesAgrementId(), but also how I simplified your code.
I skipped the part of finding the agreement ID because your current code doesn’t make sense to me.

[ExtensionOf(formDataFieldStr(SalesCreateOrder, SalesTable, CustAccount))]
internal final class SalesCreateOrder_CustAccount_My_Extension
{
    public void modified()
    {
        next modified();

        SalesTable salesTable = element.salesTable;
        SalesAgreementId salesAgreementId;
        
        if (salesTable.CustAccount)
        {
            salesAgreementId = ...
        }

        element.editSalesAgreementId(true, salesAgreementId);
    }
}

1 Like