I have a CRM 2011 application and am required by law to develop a very specific piece of functionality in a very specific way.
In an entity named new_s_edance, I have a text field named new_casenumber
The end user will enter a combination of numbers and letters into this new_casenumber text field.
I then need to somehow take whatever number the user enters into the new_casenumber field, and during the on-change event for that text field, put that number in a variable name NUMVAR.
I then need to take the contents of that NUMVAR variable and query a series of fields in a table named CCASETBL that exists in an EXTERNAL MSSQL DATABASE MSSQL in the following manner:
SELECT RIGHT('00'+CAST([DC] AS VARCHAR(2)),2)+RIGHT('00'+CAST([DY] AS VARCHAR(2)),2)+RIGHT('00000'+CAST([DS] AS VARCHAR(5)),5) AS [CASENUMBER]
WHERE NUMVAR = RIGHT('00'+CAST([DC] AS VARCHAR(2)),2)+RIGHT('00'+CAST([DY] AS VARCHAR(2)),2)+RIGHT('00000'+CAST([DS] AS VARCHAR(5)),5)
If the exact value in NUMVAR does not exist in the external database as determined via the query above, I need to send something back that will either display text on the user’s CRM screen or show an alert message that says “The ID you entered does not exist, please enter a Valid ID”.
If the value of NUMVAR does exist in the CCASETBL across the combined fields as shown in the query above of the external database, then I need to retrieve it, but also retrieve three additional values from related tables using several different sets of queries shown below.
The first of the three additional values I must retrieve would be accessible by querying the TOP 1 PNAME field from the RNAME table in the same external database using a left join between the CCASETBL table C and the RNAME table N on fields C.F1 = N.F1 AND C.F2 = R.F2 AND C.F3 AND R.F3 where RIGHT('00'+CAST([DC] AS VARCHAR(2)),2)+RIGHT('00'+CAST([DY] AS VARCHAR(2)),2)+RIGHT('00000'+CAST([DS] AS VARCHAR(5)),5)
= NUMVAR and PNAME = “TYPE A”.
If I was writing raw sql it would look like this:
SELECT TOP 1 PNAME FROM RNAME R
LEFT JOIN CCASETBL C ON C.F1 = N.F1 AND C.F2 = R.F2 AND C.F3 AND R.F3
WHERE NUMVAR = RIGHT('00'+CAST([DC] AS VARCHAR(2)),2)+RIGHT('00'+CAST([DY] AS VARCHAR(2)),2)+RIGHT('00000'+CAST([DS] AS VARCHAR(5)),5)
Then, I need to retrieve a third value from the TOP 1 PNAME field in the same RNAME table also using a left join between the CCASETBL table C and the RNAME table N on fields C.F1 = N.F1 AND C.F2 = N.F2 AND C.F3 AND R.F3 where RIGHT('00'+CAST([DC] AS VARCHAR(2)),2)+RIGHT('00'+CAST([DY] AS VARCHAR(2)),2)+RIGHT('00000'+CAST([DS] AS VARCHAR(5)),5)
= NUMVAR and PNAME = “TYPE B”.
If I was writing raw sql, it would look like this:
SELECT TOP 1 PNAME FROM RNAME N
LEFT JOIN CCASETBL C1 ON C1.F1 = N.F1 AND C1.F2 = N.F2 AND C.F3 AND N.F3 where RIGHT('00'+CAST([DC] AS VARCHAR(2)),2)+RIGHT('00'+CAST([DY] AS VARCHAR(2)),2)+RIGHT('00000'+CAST([DS] AS VARCHAR(5)),5)
= NUMVAR and PNAME = “TYPE B”.
Then, I need to retrieve a fourth value from the TOP 1 MUDGE field in the DJMDGT table also using a left join between the CCASETBL table C and the DJMDGT table D on fields C.F9 = D.F9
In each of the examples above, there may be multiple lines returned from the above query with the valid data, that is why I specified TOP 1. I only want one and they are all the same anyway or I can alter the where statement to ensure that what I want is the TOP 1 from the data that is returned.
Once all four of the values are returned, they can be placed in to an array or separate variables of v1, v2, v3, v4 and then ultimately need to be set via a function that fires off when the plug in ends. The code would look like this
Xrm.Page.getAttribute(new_f1).setValue(v1)
Xrm.Page.getAttribute(new_f2).setValue(v2)
Xrm.Page.getAttribute(new_f3).setValue(v3)
Xrm.Page.getAttribute(new_f4).setValue(v4)
Now as the user continues working he might realize he entered the wrong number in the new_s_edance field so changing that value should fire off the whole process on change again.
The other question I have is that based on my underatanding of plugins and the ones I have written in the past, they can either fire on form save or form update. They don’t fire on the change event of a field do they? How would that work?
Would a plugin even be involved in this?
How else would this even be done with a WCF service if no plugin was operating to manage and control the situation?
Any guidance, advice, or direction would be greatly appreciated.
The whole reason this must be done is that law requires that the end user to never be able to see the data in the other database unless he already has it. That’s why I cannot have partial data come back. Either the value is correct and the original value along with the related data comes back as explained, or it isn’t and the user knows the number entered is invalid. Now yes the user can guess and try and enter random numbers but after a specified number of invalid attempts, the other system that is being accessed shuts down access because it detects the fact that someone was most likely guessing to try and get information.