web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Can string manipulation function produce a CLR issue ?

(1) ShareShare
ReportReport
Posted on by 397
Hi all,
 
I wondering whether some string function, for example strRFix() can cause a CLR error in D365 FO ?
 
Reason is my F&O currently crashing and have this error /
/An exception occurred when invoking the operation - Common Language Runtime detected an invalid program.
 
And the least I can remember of some changes in my code is I'm adding this kind of statement in my code ->
 
System.DateTime localDateTime = DateTimeUtil::applyTimeZoneOffset(System.DateTime::Now, DateTimeUtil::getUserPreferredTimeZone());
TimeInMS        timeMS = System.DateTimeOffset::Now.Millisecond;
myString = strFmt(/%1%2/,localDateTime.ToString('yyyyMMdd_HHmmss'), strRFix(int2str(timeMS), 4, /0/))
 
Thanks
I have the same question (0)
  • Layan Jwei Profile Picture
    8,124 Super User 2025 Season 2 on at
    Hi Teevo,
     
    On what line is it failing, did u debug?
     
    And in general you can use this to catch most of exceptions
    system.Exception ex;
    Try
    {
    }
    catch(ex)
    {
    }
     
    Also what version is your Visual studio? 
     
    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future
  • Teevo Profile Picture
    397 on at
    Hi Layan,
     
    This is actually relate to my other post about custom API failing. https://community.dynamics.com/forums/thread/details/?threadid=7e89dfde-0206-ef11-9f89-0022482005ec
     
    So basically, I have the mentioned error and what I did was add those lines in my custom API. 
     
    However I'm not too sure that this statement is the cause, because I tried this code in my local DevBox, as well as changing the DB to UAT and it is fine.
     
    My Visual Studio is 2019 version. I debug this and found no issue actually. but since the error message not giving me any clue what is the error, I tried to think from many aspect and thinking whether the CLR error because of my addition.
  • Suggested answer
    Waed Ayyad Profile Picture
    9,042 Super User 2025 Season 2 on at
    Hi,
     
    Try to add this code and debug it. Also add it to your API and try to call it.
     
    System.Exception e;
    InteropPermission interopPermission;
    
    try
    {
        interopPermission = new InteropPermission(InteropKind::ComInterop);
        interopPermission.assert();
        //Your code here
    }
    catch (Exception::CLRError)
    {
        e = CLRInterop::getLastException();
        if (e != null)
        {
            e = e.InnerException;
            throw error(e.ToString());
            //…or whatever you want to do with that error text
        }
        else
        {
            throw error("null CLR error");
        }
    }
    catch
    {
        //other error handling
    }
    finally
    {
        CodeAccessPermission::revertAssert();
    }
    Thanks,
    Waed Ayyad
     
    If this helped, please mark it as "Verified" for others facing the same issue
     
  • Teevo Profile Picture
    397 on at
    Hi,
     
    I can confirm now, the strRFix() function is the root cause of my CLR error.
    And this only happened in my custom API running in Multitier environment. 
     
    Anyone know why this string function cannot run in Multitier ?
     
    Thanks
  • Martin Dráb Profile Picture
    238,000 Most Valuable Professional on at
    What values did you pass to strRFix() and what exception did you get?
  • Teevo Profile Picture
    397 on at
    Hi Martin,
     
    The strRFix is in this code :
     
    System.DateTime localDateTime = DateTimeUtil::applyTimeZoneOffset(System.DateTime::Now, DateTimeUtil::getUserPreferredTimeZone());
    TimeInMS        timeMS = System.DateTimeOffset::Now.Millisecond;
    myString = strFmt(/%1%2/,localDateTime.ToString('yyyyMMdd_HHmmss'), strRFix(int2str(timeMS), 4, /0/))
     
    So when I remove that strRFix, the error is gone.
     
    As mentioned, this only happened when I run it against my Tier-2 environment. It does not happen in my DevBox.
     
     
    I tried to add Try..Catch() and put the error in my log table, however it looks like it won't catch as well, so not sure whether it is CLR error, although postman says so.
     
            try
            {
                System.DateTime localDateTime = DateTimeUtil::applyTimeZoneOffset(System.DateTime::Now, DateTimeUtil::getUserPreferredTimeZone());
                TimeInMS        timeMS = System.DateTimeOffset::Now.Millisecond;
    
                ttsbegin;
                    
                logTable.clear();
                logTable.ID = strFmt("%1%2",localDateTime.ToString('yyyyMMdd_HHmmss'), strRFix(int2str(timeMS), 4, "0")); 
                str logId = logTable.ID;
                .
                .
           
                logTable.insert();
            
                ttscommit;
            }
            catch (Exception::CLRError)
            {
                System.Exception e;
                e = CLRInterop::getLastException();
                if (e != null)
                {                
                    e = e.InnerException;
                    ttsbegin;                
                    logTable.clear();
                    logTable.ID = strFmt("%1%2",systemDateGet(), "0000");
                    logTable.Response =  e.ToString();
                    logTable.insert();
                    ttscommit;
                }
     
    Hope my explanation is clear.
    Thanks.
  • Suggested answer
    Martin Dráb Profile Picture
    238,000 Most Valuable Professional on at
    The fact that you're getting an exception when using strRFix() doesn't necessarily mean that it's thrown by strRFix(). You do many things at the single line and then it's not clear where exactly it failed.
     
    Instead of trying to identify the exact reason (without having enough information about the exception and the data), let me suggest more sensible code:
    System.DateTime utcTimeNow = System.DateTime::Now;
    System.DateTime localDateTime = DateTimeUtil::applyTimeZoneOffset(utcTimeNow, DateTimeUtil::getUserPreferredTimeZone());
    int timeMS = utcTimeNow.Millisecond;
    localDateTime.AddMilliseconds(timeMS);
    myString = localDateTime.ToString('yyyyMMdd_HHmmssffff');
    By the way, there is an easier way of catching CLR exceptions:
    System.Exception ex;
    
    try
    {
        ...
    }
    catch (ex)
    {
        ...
    }
  • Teevo Profile Picture
    397 on at
    Hi Martin,
     
    Actually that strRFix() is a new addition. Before this addition, the code is already running well, like this  ->
     
    myString = strFmt(/%1%2/,localDateTime.ToString('yyyyMMdd_HHmmss'), timeMS)
     
    It is only that I don't need the full timeMS which if I remembered correctly it is 7 digit, so I cut into 4, but I want it to be aligned.
     
    Come to think maybe I try it in separation first.
     
    But if there is some more suggestion, I'm happy to know.
     
    Thanks.
  • Martin Dráb Profile Picture
    238,000 Most Valuable Professional on at
    My suggestion is in my previous reply. Instead of formatting the date time by one way and the milliseconds in another way, I simply added milliseconds to the string format (yyyyMMdd_HHmmssffff instead of yyyyMMdd_HHmmss). It's more straightforward and it doesn't use any int2str() and strRFix() at all.
  • Teevo Profile Picture
    397 on at
    Hi Martin,
     
    If I remember correctly that format yyyyMMdd_HHmmssffff is not working. Someone also mentioned that D365 function DateTimeUtil is not support until millisecond. 
     
    Let me try again. Since the splitting is not working
     
    May I know also how exactly the catch, because I follow what stated here, just a few modification because I want to catch the error in log table.
     
    Here is the code:
            System.Exception        ex;
    
            try
            {
                System.DateTime localDateTime = DateTimeUtil::applyTimeZoneOffset(System.DateTime::Now, DateTimeUtil::getUserPreferredTimeZone());
                TimeInMS        timeMS = System.DateTimeOffset::Now.Millisecond;
    
                ttsbegin;
                    
                logTable.clear();
                str s1_timeMS = int2Str(timeMS);
                str s2_timeMS = subStr(s1_timeMS,strLen(s1_timeMS)-4, strLen(s1_timeMS));
                str s3_timeMS = strRFix(s2_timeMS, 4, "0");
                
                logTable.ID = strFmt("%1%2",localDateTime.ToString('yyyyMMdd_HHmmss'), s3_timeMS); 
                
                logTable.insert();
            
                ttscommit;
            }
            catch(ex)
            {
                str exMessage = ex.Message;
                ttsbegin;
                logTable.clear();
                logTable.ID = strFmt("%1%2",systemDateGet(), "0000");
                logTable.Response =  exMessage;
                logTable.insert();
                ttscommit;
            }
     
     
    May I know how exactly to catch this error in multi-tier ?
    Because with that code, turns out it is not catch (not insert into my log)
     
    Actually the intention here is I want to have ID, and to eliminate the chance to have duplicate ID, I would like to have that numbers up to millisecond, because this is a batchjob, can be fast so if the ID only up to second, there is chance to have same ID.
     
    Thanks

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 677 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 421 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 317 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans