Setting a contact Represents field

Discussions related to order tracking development with the ProForm module.

Moderator: Phil Barton

Post Reply
shawnregan
Posts: 22
Joined: Tue Dec 11, 2018 10:03 am

Setting a contact Represents field

Post by shawnregan »

Hello

I set the contact represents field to order.Buyers or order.Sellers without issue. But I run in the following error when I try to set it as order.AllBuyers.

{"Cannot implicitly convert type 'AllBuyers' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)"}

Any help would be great.
BobRichards
Posts: 1376
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Setting a contact Represents field

Post by BobRichards »

Please show your code for this issue. It seems that "order.AllBuyers" should work.
Bob Richards, Senior Software Developer, SoftPro
shawnregan
Posts: 22
Joined: Tue Dec 11, 2018 10:03 am

Re: Setting a contact Represents field

Post by shawnregan »

Lets start here

Code: Select all

dynamic dAttorney = TranslateAttorney(party, tState);
Action<dynamic> setClients = clients =>
{
	foreach (dynamic client in clients) { dAttorney.Represents = client; }
};
if (party.Type == Constants.PartyTypes.BUYERS_ATTORNEY)
	setClients(tState.SelectOrder.AllBuyers);

Code: Select all

protected virtual dynamic TranslateAttorney(Local.IContact lender, TranslateState tState)
        {
            try
            {
                dynamic dAttorney = TranslateContact(lender, tState, Constants.ObjectTypes.ATTORNEY);
                
                return dAttorney;
            }
            catch (Exception ex)
            {
                Logging.WriteLog("TranslateAttorney", ex);
                throw ex;
            }
        }

Code: Select all

protected virtual dynamic TranslateContact(Local.IContact contact, TranslateState tState, string contactType)
         {
            try
            {
                dynamic dContact = null;

                if (contact == null) return dContact;
                dContact = CreateNewObject(contactType, tState);
                tState.SelectOrder.Contacts.Add(dContact);
                dContact.Name = Truncate(contact.Company, 150);
                dContact.Email = contact.EMail;
                TranslateAddress(contact.Address, dContact.Address, tState);
                TranslatePhones(contact, dContact, tState);

                if (contact.Name == null || string.IsNullOrEmpty(contact.Name.First) && contactType != Constants.ObjectTypes.ATTORNEY) return dContact;

                dynamic person = CreateNewObject(Constants.ObjectTypes.PERSON, tState);
                dContact.People.Add(person);
                TranslateIndividual(contact, person, tState);
                TranslateAddress(contact.Address, dContact.Address, tState);
                return dContact;
            }
            catch (Exception ex)
            {
                Logging.WriteLog("TranslateContact", ex);
                throw;
            }
        }
BobRichards
Posts: 1376
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Setting a contact Represents field

Post by BobRichards »

Your code fails because "AllBuyers" is an instance of a "Contact". Since it is not a collection, it cannot be used in this context. This is why the compiler error was "Cannot implicitly convert type 'AllBuyers' to 'System.Collections.IEnumerable".

Code: Select all

// *****  FAILS  *****
foreach (dynamic client in tState.SelectOrder.AllBuyers)
{
    ...
}
The collection of all of the order buyers (implements IEnumerable) is order.Buyers.

Code: Select all

// *****  WORKS  *****
foreach (dynamic client in tState.SelectOrder.Buyers)
{
    ...
}
Bob Richards, Senior Software Developer, SoftPro
shawnregan
Posts: 22
Joined: Tue Dec 11, 2018 10:03 am

Re: Setting a contact Represents field

Post by shawnregan »

But my tState.SelectOrder.AllBuyers is an instance of the AllBuyers at the top level of the select Order.
2019-08-02_0645.png
2019-08-02_0645.png (6.38 KiB) Viewed 3447 times
SO how do I change the Represents to "All Buyers"
2019-08-02_0647.png
2019-08-02_0647.png (12.48 KiB) Viewed 3446 times
BobRichards
Posts: 1376
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Setting a contact Represents field

Post by BobRichards »

I agree that the "Represents" property can be set to "AllBuyers" since it is a Contact type.

Code: Select all

dAttorney.Represents = order.AllBuyers;
But your compiler error is with the "foreach" loop and has nothing to do with setting the property (look at the stack trace to get the line number for the actual error). Ignore the content in the body of the loop.

Code: Select all

foreach (dynamic client in clients)
Your code sets "clients" to "AllBuyers". The "foreach" keyword requires "clients" to be a collection (something it can iterate). A Contact is not a collection. Examples of a Contact are order.Buyers[0] or order.Abstractors[2]. Examples of a collections are order.Buyers or order.Abstractors.
Bob Richards, Senior Software Developer, SoftPro
shawnregan
Posts: 22
Joined: Tue Dec 11, 2018 10:03 am

Re: Setting a contact Represents field

Post by shawnregan »

Right, my bad. :) thanks.
Post Reply