Sentry Plugin - Find value in userdefined field?

lock
push_pin
done
Answered
9

Hi, 

I'm working on a SentryPlugin which fill set the tablerights to FULL if a specific userdefined checkbox field is set.

My problem is that I'm unable to get the value of the userdefined checkbox. The udefValue in this example is allways NULL:

        public void ModifyTableRights(TableRights rights)
        {
            try
            {
                using (_sentry.Lookups.BeginIgnoreSentryCheck())
                {
                    var udcontactSmall = TablesInfo.GetUDContactSmallTableInfo();
                    if (_sentry.IsTableCovered(udcontactSmall))
                    {
                        //If a userdefined checkbox is set, then give full access.
                        var udefValue = _sentry.Lookups.GetFieldValue(udcontactSmall.Long07); //Is always NULL?
                        if (udefValue.ToString() == "1")
                        {
                            rights[TablesInfo.GetContactTableInfo()] = RightsFactory.Get(ETableRight.FULL, "Full access given by Sentry Plugin");
                            rights[TablesInfo.GetUDContactSmallTableInfo()] = RightsFactory.Get(ETableRight.FULL, "Full access given by Sentry Plugin");
                            rights[TablesInfo.GetUDContactLargeTableInfo()] = RightsFactory.Get(ETableRight.FULL, "Full access given by Sentry Plugin");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                SoLogger.LogError(ex);
            }
        }

Is this not the right way to get the value of the udeffield?

9 Jan 2018 | 12:00 AM

All Replies (9)

Hi Frode,
I may be on thin ice here but I am going to give it a try based on my work with UDef fields in the C# code of BulkUpdate..
I think you need to pass it a ProgId, something like "SuperOffice:7". The UDef systems needs to lookup the actual Field definition in order to be able to know how to Get/Set the relevant data..
Conrad

9 Jan 2018 | 12:00 AM

But how do I do that?

This is the line I'm struggling with: 

var udefValue = _sentry.Lookups.GetFieldValue(udcontactSmall.Long07); //Is always NULL?

If I try to do something like this instead, then it really gets mad and throws a nullreference.

var udefValue = _sentry.Lookups.GetFieldValue(udcontactSmall.FindField("SuperOffice:3")); //Crashes with NullReference
10 Jan 2018 | 12:00 AM

I tried this with a plugin test I had.
Your approach is correct.
_sentry.Lookups is collection of loaded FieldInfo data.
I had expected a UdefHelper would be available since after all, the db field per se is not a key that always remains correct throughout the live of a udef definition. The ProgId is.
I can reproduce your issue and a.f.a.i.c.s there is a issue deep down in the lookup code where 2 TableInfos are compared. One is your FieldInfo.Parent and the other is the UDContactSmallTableInfo that is part of the collection. Though they have identical contents, they are not the same instances and consequently then the == operator fails.
Coming from C++ where the == operator is a major fundemental consept, I have a hard time getting my head around these issues in C#..
I will log this with my findings. Some C# & Sentry expert will need to look into it.
/conrad 

 

tfs 56042: Sentry.Lookups.GetFieldValue( FieldInfo ) fails for all Udef fields in a Sentry Plugin

10 Jan 2018 | 12:00 AM

Thanks for looking into this!

10 Jan 2018 | 12:00 AM

Hi Conrad,

do you know if the problem is limited to "Userdefined fields", or will the same problem be for any table which is not the same as the Parent table? So using, f.instance, Company Interests would have the same problem?

16 Jan 2018 | 12:00 AM

I believe it is only for Udef fields but I havn't verified all nested tables.
The following works and any field directly of the MainTable.; 

     GetFieldValue( QueryInfo.MainTable.Heading )
(My test is a combined Win & Netserver Sale sentry)

I wonder whether a work-around could be to load the Entrities.Contact (again) with Entities.Contact.GetFromIdx..., turning OFF sentry first, and then use its UDefHelper...

/conrad

16 Jan 2018 | 12:00 AM

Hi I'm sitting with the exact same problem, did you find a solution Frode?

If i try to load the entity with getfrom idx, it just crashes SuperOffice - im try ing to make an appointment sentry, that needs to trigger if a checkbox on the more page has been ticked.

Also if this is a bug, is it present in 8.0 sr4, and fixed in later versions?

5 Sep 2018 | 12:00 AM

nope, nothing has happened with 56042.
The Web/Netserver folks are very busy....

/conrad

5 Sep 2018 | 12:00 AM

Hi Conrad and Frode, i actually think i found the solution in the meantime :)

I stumbled upon it by accident, and it might not be the right way, but have a look.

I used Frode's code and got the exact same issue as he did. i was pulling my hair out, and i ended up trying to 

do a List<FieldInfo> fields = _sentry.Lookups.GetKnownFields(); and add a watch on the fields variable. I noticed that there were no udef fields amongst the known fields, which is why frodes code returns null.

 

But after some digging on community, i found an article that Tony posted a while ago about joins, and i've previously tried to screw around with the SentryPluginTableUpdater section of the example in the documentation, to see if i could include the field i needed, without any luck - until i found the part about adding a join, which made the solution dead simple:

[SentryPluginQueryTableUpdater("appointment")]
public class SentryPluginQueryTableUpdaterAppointment : ISentryPluginQueryTableUpdater
{


public void ModifySelect(Select sql, TableInfo tableInfo)
{
//Get table info for Appointment
AppointmentTableInfo table = (AppointmentTableInfo)tableInfo;

//Get table info for udef appointment fields
UDAppntSmallTableInfo udti = TablesInfo.GetUDAppntSmallTableInfo();

//Add a join restriction on userdefid = udappointmentsmallid
sql.JoinRestriction.LeftOuterJoin(table.UserdefId.Equal(udti.UdappntsmallId));

//add the field to be returned by the archive. In my case it was Long05
sql.ReturnFields.Add(udti.Long05);

}

}

 

After that, long05 now returns 0 or 1 :D

 

var udappSmall = TablesInfo.GetUDAppntSmallTableInfo();
var value = GetFieldValue(udappSmall.Long05); //Returns 0 or 1 for a checkbox!

Please let me know if it helped you as well, or if you have any comments :)

6 Sep 2018 | 12:00 AM

Add reply