Modifies the customField field for the given object with
    something related to the value of the given field.

    If the dataType == "USER_LINK" then instead of using the value
    of the CF, it will be a MambuUser object.

    Same if dataType == "CLIENT_LINK", but with a MambuClient.

    Default case: just uses the same value the CF already had.

    Returns the number of requests done to Mambu.

def setCustomField(mambuentity, customfield="", *args, **kwargs):
    """Modifies the customField field for the given object with
    something related to the value of the given field.

    If the dataType == "USER_LINK" then instead of using the value
    of the CF, it will be a MambuUser object.

    Same if dataType == "CLIENT_LINK", but with a MambuClient.

    Default case: just uses the same value the CF already had.

    Returns the number of requests done to Mambu.
    """
    from . import mambuuser
    from . import mambuclient
    try:
        customFieldValue = mambuentity[customfield]
        # find the dataType customfield by name or id
        datatype = [ l['customField']['dataType'] for l in mambuentity[mambuentity.customFieldName] if (l['name'] == customfield or l['id'] == customfield) ][0]
    except IndexError as ierr:
    # if no customfield found with the given name, assume it is a
    # grouped custom field, name must have an index suffix that must
    # be removed
        try:
            # find the dataType customfield by name or id
            datatype = [ l['customField']['dataType'] for l in mambuentity[mambuentity.customFieldName] if (l['name'] == customfield.split('_')[0] or l['id'] == customfield.split('_')[0]) ][0]
        except IndexError:
            err = MambuError("Object %s has no custom field '%s'" % (mambuentity['id'], customfield))
            raise err
    except AttributeError:
        err = MambuError("Object does not have a custom field to set")
        raise err

    if datatype == "USER_LINK":
        mambuentity[customfield] = mambuuser.MambuUser(entid=customFieldValue, *args, **kwargs)
    elif datatype == "CLIENT_LINK":
        mambuentity[customfield] = mambuclient.MambuClient(entid=customFieldValue, *args, **kwargs)
    else:
        mambuentity[customfield] = customFieldValue
        return 0

    return 1