How to populate Primary Address mail ID and Phone Number of customer

Hi All,
I am trying to auto populate Primary Address mail ID and Phone Number based on Delivery or Invoice(role type).
I have created two fields Email and Phone in salesorder Header >> Address. when I am opening the sales Order it should automatically populate the fields based on Primary address.
I wrote the below code it is not working properly I am able to get the mail Id but it is not based on primary address and Delivery or Invoice
below is the code I wrote to get the mail ID based on Primary address and (Role)Delivery. Please guide me

[Extensionof(formDataSourceStr(SalesTable,SalesTable))]
internal final class SalesTableFrm_SalestableDS_Extension
{
    // public  Salestable salestable;
    
     int active()
     {
          next active();
          int                 ret;
          FormDataSource      salestable_ds;
          FormRun             formRun;
          FormControl         formControl;
          Salestable  salestable = this.cursor();

        
               

               if(!salestable.TECDeliveryEmail)
               {
                    // Email                       deliveryemail;
                    LogisticsLocationRecId      locationRecId;
                    LogisticsLocation           locationChild;
                    LogisticsElectronicAddress  logisticsElectronicAddress;
               LogisticsLocation        Location_Inv = LogisticsLocationDefault::findSimpleDefault(CustTable::find(SalesTable.InvoiceAccount), LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Delivery));
                    // See if a phone number associated with the delivery location is available
              // LogisticsPostalAddress logisticsPostalAddress =LogisticsPostalAddress::getLocation(salestable.DeliveryPostalAddress);
               locationRecId = LogisticsPostalAddress::getLocation(salestable.DeliveryPostalAddress);


                    if (locationRecId)
                    {
                         // Find a phone number associated with the postal delivery location
                         select firstonly  TableId,RecId from locationChild
                where locationChild.ParentLocation  == locationRecId
                    join Location_Inv
                     where Location_Inv.RecId == locationChild.RecId
                         && locationChild.IsPostalAddress == NoYes::Yes
                    join Locator from logisticsElectronicAddress
                where logisticsElectronicAddress.Location   == locationChild.RecId
                    &&   logisticsElectronicAddress.Type       == LogisticsElectronicAddressMethodType::Email
                    &&   logisticsElectronicAddress.IsPrimary  ==NoYes::Yes
                    &&   logisticsElectronicAddress.Locator    != '';

                         salestable.TECDeliveryEmail = logisticsElectronicAddress.Locator;
                    }
               }
              
            return ret;
          }

First of all, let me make your code easier to read by fixing indentation and throwing away all the code without any effect:

[Extensionof(formDataSourceStr(SalesTable,SalesTable))]
internal final class SalesTableFrm_SalestableDS_Extension
{
	int active()
    {
        next active();
		  
		SalesTable salestable = this.cursor();

		if (!salestable.TECDeliveryEmail)
		{
			// See if a phone number associated with the delivery location is available
			LogisticsLocationRecId locationRecId = LogisticsPostalAddress::getLocation(salestable.DeliveryPostalAddress);

			if (locationRecId)
			{
				LogisticsLocation locationChild;
				LogisticsLocation location_Inv;
				LogisticsElectronicAddress logisticsElectronicAddress;
			
				// Find a phone number associated with the postal delivery location
				select firstonly RecId from locationChild
					where locationChild.ParentLocation  == locationRecId
					   && locationChild.IsPostalAddress == NoYes::Yes
					join RecId from location_Inv
						where location_Inv.RecId == locationChild.RecId
					join Locator from logisticsElectronicAddress
						where logisticsElectronicAddress.Location   == locationChild.RecId
						   && logisticsElectronicAddress.Type       == LogisticsElectronicAddressMethodType::Email
						   && logisticsElectronicAddress.IsPrimary  == NoYes::Yes
						   && logisticsElectronicAddress.Locator    != '';

				 salestable.TECDeliveryEmail = logisticsElectronicAddress.Locator;
		}
	}
  
	return ret;
}

I guess you mean primary addresses of the ordering customer, but you don’t have that in your code. you seem to be using the order delivery address instead.

1 Like

Hi Martin,

Yes, Primary address of ordering customer but I have Delivery address group and invoice address group in both groups I have created Email and phone. The mail ID which I am trying to get should be primary and related to delivery address. One ordering customer may have two different addresses for delivery and invoice this is where I am facing difficulty. Please guide me.

As I said, the main problem is that you want customer’s address but you have nothing like that in your code. Instead, you’re using the delivery address of the sales order.

To get the ordering customer ID from the sales order, use salesTable.CustAccount.

1 Like

Hi Martin, below is the image where delivery address and invoice address will be displayed of a customer. In Delivery address I am trying to get mail ID of that particular customer related to primary address same as in Invoice address.
I have to differentiate based on Primary address and delivery address or invoice address (based on address role also). if I am getting mail id in Delivery address it should be related to primary address of that customer and address role type should be Delivery.

I am using below code but when I opening the salesorder it throwing error like manager field should be filles few other fields

 CustTable custTable;
              LogisticsElectronicAddress  logisticsElectronicAddress;
              Phone phoneNum;
              Email emailid;

              custTable    = custTable::find(salestable.CustAccount); // Use your customer Account number
              phoneNum = DirParty::primaryElectronicAddress(custTable.Party, LogisticsElectronicAddressMethodType::Phone).Locator;
              emailid         = DirParty::primaryElectronicAddress(custTable.Party,LogisticsElectronicAddressMethodType::Email).Locator;
              salestable.TECDeliveryEmail = emailid;//logisticsElectronicAddress.Locator;

Please be more specific - tell us which line of code is throwing the error and what exactly the error message says. But first test your code in isolation and integrate it to the form only when you know it works. As you see, you have no idea about what’s wrong if you do too many steps at once.

For example:

CustTable custTable	= CustTable::find('Cust001'); // Hard-coded for testing
Email emailId       = DirParty::primaryElectronicAddress(custTable.Party, LogisticsElectronicAddressMethodType::Email).Locator;
info(emailId);

By the way, your should do a better job when pasting your code here, e.g. by fixing the indentation. It’s in your interest not to create obstacles for people willing to help you.
You should also write cleaner code, which will help both your and anyone reading your code (such as your colleagues). Notice how I simplified your code by using local variable declarations and removing code unrelated to the discussed scenario.

1 Like

Sure Martin I will do that.