Implementing Reuse Library to perform similar validations on different Objects and raise the error messages

in my Project, there was a requirement to perform similar checks on different Transactions, especially Opportunity and Lead.

As a good programmer I have learnt, not to implement the same code multiple times, use a reuse method instead. That’s why I started using a Reuse Library in C4C. My requirement was that I had to raise an error message, when the check wasn’t successful. As I knew, it is impossible to raise a message from a reuse function, so I asked here on SCN for help.

With the provided help, I have achieved my requirements by creating a Reuse Library for performing checks and raise messages (indirectly 😉 )

Here are the steps that I have performed to achieve this:

Create a Reuse Library (CheckSomething.library)

Create a Function (CheckField)

As return Parameter I have checked the Checkbox “CollectionOf” and as Type I use “LANGUAGEINDEPENDENT_LONG_Text” (I need the possibility to create more than one Message, that’s why I used a collection. If you only need one Message, just leave the Checkbox blank)

(Optional) depending on your check, you can handover some Parameter. As we want to reuse it in different Transaction, you can handover for example the Value you want to do a check on. I have implemented 2 Parameters that I have to Import.

Activate the CheckSomething.library, the Function is created automatically. Then you can implement it.Here is some example coding:

importABSL;

importAP.Common.GDT;

vartext_collection : collectionofLANGUAGEINDEPENDENT_LONG_Text;

vartext : LANGUAGEINDEPENDENT_LONG_Text;

//Implement the Check on the value

if(<condition = true>)

{

//Fill the text collection with the needed error messages

text.content = “<Your Error Message>”;

text_collection.Add(text);

}

returntext_collection;

This is just a part of my code. I have also implemented a loop, to fill my Collection with more Texts. If you don’t have a collection, just return the text.

Implement a Message in the XBOs (Opportunity.xbo, Lead.xbo, …) Example:

[Extension] businessobject AP.CRM.Global:Opportunity

raises MsgInvolvedParties

{

message MsgFromReuse text“&1”:LANGUAGEINDEPENDENT_LONG_Text;

}

I am using “&1” to fill the message with any Text I want when raising the message.

I have implemented the OnSave-Validation (if you want to rise only the Incidents, Implement the AfterModify).Logic that I have implemented on On-Save was that when the message collection is filled, my check was successful to catch the error in User Inputs. So the Transaction shouldn’t be saved and the message should appear.