CRM Script: getting a userdefined field before save

Hi all,

A few month ago you helped me with part of a CRM script to be able to send an email with the old OrgNr and the new OrgNr. See part of this script below:

#setLanguageLevel 3;

EventData ed = getEventData();

// Current ID of the contact
String contactId = ed.getInputValue("ContactEntity.ContactId");

// New Org.Nr of the contact
String contactOrgNr = ed.getInputValue("ContactEntity.OrgNr").substitute(" ", "").stripLeadingAndTrailing(" ");
String originalOrgNr;

// Gets the org.nr before save
SearchEngine seContact;
seContact.addField("contact.orgNr");
seContact.addCriteria("contact.contact_id", "Equals", contactId);
if(seContact.select() > 0)
originalOrgNr = seContact.getField(0);

 

I want to do the same for one of our UserDefined Fields, so I thought I could change the script as follows:

#setLanguageLevel 3;


EventData ed = getEventData();

// Current ID of the contact
String contactId = ed.getInputValue("ContactEntity.ContactId");

// New Org.Nr of the contact
//String contactOrgNr = ed.getInputValue("ContactEntity.OrgNr").substitute(" ", "").stripLeadingAndTrailing(" ");
String VATnr = ed.getInputValue("ContactEntity.UserDefinedFields.SuperOffice:26").substitute(" ", "").stripLeadingAndTrailing(" ");
//String originalOrgNr;
String originalVATNr;

// Gets the org.nr before save
SearchEngine seContact;
seContact.addField("ContactEntity.UserDefinedFields.SuperOffice:26");
seContact.addCriteria("contact.contact_id", "Equals", contactId);
if(seContact.select() > 0)
originalVATNr = seContact.getField(0);

 

Unfortunately, this does not work: the script itself does not give an error, but when I change the Userdefined field in the Application, I get the following error:

EjSript: RunTimeErrorException: An exception ccurred in script: 'Sales: Voordat bedrijf wordt opgeslagen: BTW-nummer gewijizgd (voordat bedrijf wordt opgeslagen)"at line 19, char 14

Original exception: General EXception:

SearchEngine:useFielf() unknon table: ContactEntity at line 19, char 14 called from select(), declared in (unknown location).

Can you advise me how I have to change this part to be able to run the script when changing this UserDefined Field?

Thanks,

Edith Baijens

 

 

 

RE: CRM Script: getting a userdefined field before save

Hi Edith,

when using SearchEngine, you can't get your User Defined fields in the same way, you'll need to look for the template variable name (looks like cl05 or cs05 etc)

When you know what it is (example cs05), you can fetch it by using this code:

String udefValue;
SearchEngine seContact;
seContact.addField("contact.userdef_id.string05");
seContact.addCriteria("contact.contact_id", "Equals", contactId);
if(seContact.select() > 0)
  udefValue = seContact.getField(0);

If you need further help, please provide me the template variable names for the User Defined Fields and I can probably make the correct SearchEngine for you.

Av: Simen Mostuen Iversen 2. sep 2021

RE: CRM Script: getting a userdefined field before save

Hi Simen,

I tried to write the trigger with your advise, but it is, unfortunately, not yet working;

The name of the template variable is cs11

I tink I need your help. Part of the script as I changed it now: 

#setLanguageLevel 3;

EventData ed = getEventData();

// Current ID of the contact
String contactId = ed.getInputValue("ContactEntity.ContactId");

// New VAT-Nr of the contact
String VATnr = ed.getInputValue("Contact.userdef_id.string11").substitute(" ", "").stripLeadingAndTrailing(" ");
String originalVATNr;

// Gets the org.nr before save
SearchEngine seContact;
seContact.addField("contact.userdef_id.string11");
seContact.addCriteria("contact.contact_id", "Equals", contactId);
if(seContact.select() > 0)
originalVATNr = seContact.getField(0);

//other values
String companyName = ed.getInputValue("ContactEntity.Name");
String companyAdmNumber = "";
String companyAdministration = ed.getInputValue("ContactEntity.Business.Value"); //Of moet dit de volgende waarde zijn -> "003";
String companyDebNumber = ed.getInputValue("ContactEntity.Number2");
String username = getActiveUser().getValue("name");
String klantStatus = ed.getInputValue("ContactEntity.Category.Value");

And can you explain when I have to use ContactEntity and when Contact as tablenames.

Thanks in advance for your help!

Greetings,

Edith Baijens

 

 

Av: Edith Baijens 9. sep 2021

RE: CRM Script: getting a userdefined field before save

Hi Edith,

first off, when you use EventData to fetch data, you'll need to use ContactEntity.

If you are uncertain of which fields you're able to fetch, try to trace it, and go through line by line to see if you can find the values and fields you need.

When using EventData, you'll need to get the udef by using the  SuperOffice:<number> field, such as:

EventData ed = getEventData();

String vatNr = ed.getInputValue("ContactEntity.UserDefinedFields.SuperOffice:26").substitute(" ","").stripLeadingAndTrailing(" ");

But then you won't get the value as it were before save, then you'll need to use SearchEngine.

String udefValue;
SearchEngine seContact;
seContact.addField("contact.userdef_id.string11");
seContact.addCriteria("contact.contact_id", "Equals", contactId);
if(seContact.select() > 0)
  udefValue = seContact.getField(0);

Remember that when you use SearchEngine, the contact needs to be in lowercase.

Av: Simen Mostuen Iversen 10. sep 2021

RE: CRM Script: getting a userdefined field before save

Hi SImen,

Thanks very much! With this I got the script working.

Greetings,

Edith

Av: Edith Baijens 13. sep 2021