Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / Can string manipulatio...
Finance forum
Suggested answer

Can string manipulation function produce a CLR issue ?

editSubscribe (1) ShareShare
ReportReport
Posted on by 338
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
  • Teevo Profile Picture
    Teevo 338 on at
    Can string manipulation function produce a CLR issue ?
    Hi Martin,
     
    I'm sorry, since the "yyyyMMdd_HHmmssffff" was in other thread I forgot to put that again into this one.
     
    Anyway, I tried to changed my code to follow exactly what your lines is, like below:
    System.DateTime utcTimeNow = System.DateTime::Now;
    System.DateTime localDateTime = DateTimeUtil::applyTimeZoneOffset(utcTimeNow, DateTimeUtil::getUserPreferredTimeZone());
                
    int timeMS = utcTimeNow.Millisecond;
    localDateTime.AddMilliseconds(timeMS);
    str myString = localDateTime.ToString('yyyyMMdd_HHmmssffff');
    
    ttsbegin;                
    logTable.clear();         
                
    logTable.ID = myString;
     
    And the result is just like I mentioned, the millisecond is not captured, it was simply "0000" like this:
     
    Please ignore the double number (2 rows each) because it was meant to be, but you can see (in descending order) when the time is changed from 03:44:34 to 03:44:53 all is end with "0000" just like the other rows.
     
    About to finding the line, this is what I been trying and mentioning, because in my local DevBox also when I switch the DB to my Tier-2 environment, there is no such error, but happened in the "real" Tier-2 environment. And that is why I've been asking whether my Try..Catch is correct or not (by inserting to log table in catch section).
     
    Hope it is clear out the confusion.
     
    Thanks again.
     
     
     
     
  • Martin Dráb Profile Picture
    Martin Dráb 223,308 Super User on at
    Can string manipulation function produce a CLR issue ?
    Please never describe a problem by mere "it's not working", because that may mean anything.
     
    Please tell us more about " yyyyMMdd_HHmmssffff is not working". If you've really found a bug in such a basic function of .NET framework (which I doubt, to be honest), we should report it to Microsoft and get it fixed.
     
    Regarding "DateTimeUtil is not support until millisecond", you seemed to misunderstand my code. Notice the call of AddMilliseconds.
     
    "splitting is not working" is also not a good problem description. Do you mean that you failed to even find the line number where the error was thrown?
  • Teevo Profile Picture
    Teevo 338 on at
    Can string manipulation function produce a CLR issue ?
    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
  • Martin Dráb Profile Picture
    Martin Dráb 223,308 Super User on at
    Can string manipulation function produce a CLR issue ?
    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
    Teevo 338 on at
    Can string manipulation function produce a CLR issue ?
    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.
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 223,308 Super User on at
    Can string manipulation function produce a CLR issue ?
    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
    Teevo 338 on at
    Can string manipulation function produce a CLR issue ?
    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.
  • Martin Dráb Profile Picture
    Martin Dráb 223,308 Super User on at
    Can string manipulation function produce a CLR issue ?
    What values did you pass to strRFix() and what exception did you get?
  • Teevo Profile Picture
    Teevo 338 on at
    Can string manipulation function produce a CLR issue ?
    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
  • Suggested answer
    Waed Ayyad Profile Picture
    Waed Ayyad 2,526 on at
    Can string manipulation function produce a CLR issue ?
    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
     

Helpful resources

Quick Links

Take the Community feedback survey!

Answer this brief 15-question survey about your Community experience…

Demystifying Copilot: Service Edition with Sundar Raghavan

Sundar answers more questions about Copilot for Service...

Dynamics 365 Business Central vs Finance and SCM

Take a look at the key differences between Business Central and…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,375 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 223,308 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,140

Featured topics

Product updates

Dynamics 365 release plans