I am looking for some advice on how to approach reaching the goal described below.
I need to get the logic of the approach figured out, so I know how to approach writing my CRM 2011 Plugin code.
Given
When a new payment is entered, my post-operation plugin need to do the following when the user saves the new payment:
(1)Search all the payments that have same Forfeit Number as the current payment that is being entered, and (2) determine what the greatest whole number value is in the Payment Number field, among all these records. Then return that whole number, add 1 to it, and place that in the current record's Payment Number field.
Looking at the "steps of the process" shown above as (1) and (2) -- how might these actually be implemented from a process standpoint?
I can't write code to do this until I understand exactly what I need to do, and determining the logical steps to do what's listed above is where I am having difficulty.
If I know what I am looking for, I can use JQUERY within my plugin to obtain the GUID of the record, and have done that type of thing before. However in this particular situation, I don't know what I am looking for because the Payment Number field may be a 1 or it may be a 10938474 and I will never know that ahead of time. That's what I need to use this plugin to "find".
Any recommendations on the "logic of the approach" would be greatly appreciated.
*This post is locked for comments
Jim, you are very welcome. This is what the community is all about...
Aric,
Thanks so much for the jumpstart. You are a miracle worker! That works exactly as I needed.
I've never done anything with conditional expression fetch style code before so this type of approach adds a whole world of possibilities to my arsenal.
Your clear presentation allows me to understand what's happening and apply it to other things, in addition to building onto it and changing it around to accomplish different things.
Thanks so much!
Or you could use "orderby" on the payment number and "count=1" when creating fetchXml. Add a condition for the ForfeitNumber (equals the value being entered), and you'll get your max number.
I'm not sure how is JQUERY involved, though. JQUERY works on the client side (javascript library). Plugins work on the server side. Are you trying to do this on the client?
Hi Jim,
That is too much work. If you have to retrieve the entire entity and do a comparison on each record.
That is the purpose of the max attribute.
See example:
ConditionExpression expr = new Condition
public int GetMaxValue(string entityName, string attributeName, string filterAttributeName, Guid filterAttributeId)
{
int rc = 0;
StringBuilder sb = new StringBuilder();
sb.AppendLine("<fetch distinct='false' mapping='logical' aggregate='true'> ");
sb.AppendLine(" <entity name='" + entityName + "'> ");
sb.AppendLine(" <attribute name='" + attributeName + "' alias='" + attributeName + "_max' aggregate='max'/> ");
sb.AppendLine(" <filter type='and'>");
sb.AppendLine(" <condition attribute='" + filterAttributeName + "' operator='eq' value='" + filterAttributeId.ToString() + "' /> ");
sb.AppendLine(" </filter> ");
sb.AppendLine(" </entity> ");
sb.AppendLine("</fetch>");
EntityCollection totalCount = service.RetrieveMultiple(new FetchExpression(sb.ToString()));
if (totalCount.Entities.Count > 0)
{
foreach (var t in totalCount.Entities)
{
rc = (int)((AliasedValue)t[attributeName + "_max"]).Value;
break;
}
}
return rc;
}
I'll take a look at your suggestion when time permits.
However, since I already started going down the road of JQUERY, I think I am onto something that will work, and I'll stay the course for the time being.
Here is what I ended up doing.
I think you've got the right steps, but you'll need to do a FetchXML to get the 'max' value of the payment number. There are some examples of aggregations in FetchXML in this article.
msdn.microsoft.com/.../gg328122.aspx
Good luck!
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,228 Super User 2024 Season 2
Martin Dráb 230,056 Most Valuable Professional
nmaenpaa 101,156