Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Unanswered

Issue with Fetching Email for a Site ID in D365 F&O

(1) ShareShare
ReportReport
Posted on by 161

Hi Everyone,

I have been working on a requirement to fetch the email and phone number associated with a specific site ID. To test my joins and logic, I created a runnable class.

Problem Statement:
While I am able to retrieve the correct phone number associated with the site ID, I am unable to fetch the email. Upon reviewing the data in SQL, I verified that the email record does exist. However, I noticed discrepancies in the RecId and Location values between the results in my job and those in the database. for Email  and don't know the relationship beside this which i can add if needed .

Could you help me understand why the email is not being fetched and guide me on the necessary corrections to retrieve both the phone number and email successfully in D365 F&O?

internal final class HappyJob
{
    public static void testContactLogic(str _siteId)
    {
        InventSite inventSite = InventSite::find(_siteId); 
        InventLocation inventLocation;
        LogisticsElectronicAddress electronicAddressPhone, electronicAddressEmail;

       
        inventLocation = InventLocation::find(inventSite.SiteId);
        if (inventLocation)
        {
            electronicAddressPhone = HappyJob::getValidElectronicAddress(inventLocation, LogisticsElectronicAddressMethodType::Phone);

            if (electronicAddressPhone)
            {
               
                info(strFmt("Site ID: %1", inventSite.SiteId));
                info(strFmt("Primary contact phone: %1", electronicAddressPhone.Locator));
            }
                      
            electronicAddressEmail = HappyJob::getValidElectronicAddress(inventLocation, LogisticsElectronicAddressMethodType::Email);

            if (electronicAddressEmail)
            {
             
                info(strFmt("Primary contact email: %1", electronicAddressEmail.Locator));
            }
          
        }
        
    }

    public static LogisticsElectronicAddress getValidElectronicAddress(InventLocation _inventLocation, LogisticsElectronicAddressMethodType _type)
    {
        LogisticsLocation logisticsLocation;
        InventLocationLogisticsLocation inventLocationLogisticsLocation;
        LogisticsPostalAddress logisticsPostalAddress;
        LogisticsElectronicAddress electronicAddress;

        utcdatetime  today = systemDateGet();

       
        select firstOnly inventLocationLogisticsLocation
            where inventLocationLogisticsLocation.InventLocation == _inventLocation.RecId
        join logisticsLocation
            where logisticsLocation.RecId == inventLocationLogisticsLocation.Location;

        if (logisticsLocation)
        {
           
            select firstOnly logisticsPostalAddress
                where logisticsPostalAddress.Location == logisticsLocation.RecId
                    && logisticsPostalAddress.ValidFrom <= today
                    && logisticsPostalAddress.ValidTo >= today;

            if (logisticsPostalAddress)
            {
               
                select firstOnly electronicAddress
                    where electronicAddress.RecId == logisticsPostalAddress.RecId
                        && electronicAddress.Type == _type;

                if (electronicAddress)
                {
                    return LogisticsElectronicAddress::findByElectronicAddressId(electronicAddress.ElectronicAddressId, true);
                }
            }
        }

        return null;
    }

    public static void main(Args _args)
    {
        HappyJob::testContactLogic("AHD");
    }
}
 I went through most of the resources available online but  can't pin point where i am doing wrong. 
 

Thanks,
Ayushaman
  • Ayushaman Profile Picture
    Ayushaman 161 on at
    Issue with Fetching Email for a Site ID in D365 F&O
    Martin, 
     
    I debugged and checked each table values i am getting .I switched to using the location from the InventSiteLogisticsLocation table instead of relying on RecId. I am  fetching site details up to the InventSiteLogisticsLocation table Correctly. However, I am unable to retrieve records from the LogisticsElectronicAddress table for the associated email.
     
    I also backtracked the joins and reviewed the table relationships, but I couldn’t establish a clear connection between the record in LogisticsElectronicAddress has data and the one i am getting as both of them are different even when i used location instead of recid . Already did that 
     
    For date validity, I removed the related filters.
     
    I even tried using the existing findByLocation method from the LogisticsElectronicAddress table, but it is not returning any data for the given location.

    The main issue seems to be with establishing a proper join or mapping between the InventSiteLogisticsLocation and LogisticsElectronicAddress tables to fetch the required email data.

    Thanks for helping me out and guiding me i really appreciate it .
    Thanks ,
    Ayushaman

     
  • Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    Issue with Fetching Email for a Site ID in D365 F&O
    You need to learn how to use the debugger. You see that you have no idea what's going on if you write a lot of code and then check the final result only. The debugger will tell you where your code fails.
     
    Nevertheless I do see another obvious bug. This condition doesn't make sense:
    select firstOnly electronicAddress
        where electronicAddress.RecId == logisticsPostalAddress.RecId
    Finding an electronic address that (by coincidence) got the same RecId assigned and as the postal address can't give you anything useful. You should use the location to find a record in LogisticsElectronicAddress.
     
    Your requirement isn't about LogisticsPostalAddress, therefore you shouldn't use it in your code at all, but note that your query for ValidFrom and ValidTo is wrong too. In your case, you wouldn't need any conditions, because F&O gives you the currently valid record automatically. If you needed different dates, you'd have to use validTimeState keyword in the select statement. Look at Using date effective data patterns if you want to learn more.
  • Ayushaman Profile Picture
    Ayushaman 161 on at
    Issue with Fetching Email for a Site ID in D365 F&O

    Hi Martin!
     
    I am working on a requirement where, upon the insertion of data in the VendTrans table, I need to fetch related records and send an email notification to both the vendor and the site owner (not the warehouse owner). Specifically, I need to retrieve the email address and phone number for the site owner.

    Path: Inventory Management > Setup > Inventory Breakdown > Sites
    In the Address section of the selected site (inside the Advanced section), I need to fetch the contact details, particularly the email address and phone number.

    As per your earlier suggestion, I have used the InventSiteLogisticsLocation table to retrieve these details. However, I am still unable to fetch the email address from the electronic address.

     public static LogisticsElectronicAddress getValidElectronicAddress(InventSite _inventSite, LogisticsElectronicAddressMethodType _type)
     {
         LogisticsLocation logisticsLocation;
         InventSiteLogisticsLocation inventSiteLogisticsLocation;
         LogisticsPostalAddress logisticsPostalAddress;
         LogisticsElectronicAddress electronicAddress;
    
         utcdatetime  today = systemDateGet();
    
        
      
         select firstOnly inventSiteLogisticsLocation
             where inventSiteLogisticsLocation.Site == _inventSite.RecId
         join logisticsLocation
             where logisticsLocation.RecId == inventSiteLogisticsLocation.Location;
    
         if (logisticsLocation)
         {
            
             select firstOnly logisticsPostalAddress
                 where logisticsPostalAddress.Location == logisticsLocation.RecId
                     && logisticsPostalAddress.ValidFrom <= today
                     && logisticsPostalAddress.ValidTo >= today;
    
             if (logisticsPostalAddress)
             {
                
                 select firstOnly electronicAddress
                     where electronicAddress.RecId == logisticsPostalAddress.RecId
                         && electronicAddress.Type == _type;
    
                 if (electronicAddress)
                 {
                     return LogisticsElectronicAddress::findByElectronicAddressId(electronicAddress.ElectronicAddressId, true);
                 }
             }
         }
    
         return null;
     }
    

     
  • Martin Dráb Profile Picture
    Martin Dráb 230,445 Most Valuable Professional on at
    Issue with Fetching Email for a Site ID in D365 F&O
    I see a problem at the very beginning. You said you wanted contact details of a site, but you're actually working with a warehouse (InventLocation), which you also find by wrong logic (InventLocation::find(inventSite.SiteId) tries to use SiteID to find a warehouse). A single siter
     
    If it's true that the requirement is "to fetch the email and phone number associated with a specific site ID", then all your code working with InventLocation instead of InventSite is wrong. You should use InventSiteLogisticsLocation table. And if you actually want information about a warehouse, please give us your actual business requirement.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans