0

I can only Edit a custom field when I before edit it by hand with the user administrator.

What's wrong with my code and what I should do to solve this??

Exactly, I'm trying to assign a value to a User custom attribute when It logs in the portal. And I'm not able to get ExpandoColumn in the conditions specified.

The problem is that ExpandoValue is null.

 public class LoginAction extends Action {

        public void run(HttpServletRequest req, HttpServletResponse res) {
            User currentUser;
            try {
                currentUser = PortalUtil.getUser(req);
                long userId = PrincipalThreadLocal.getUserId();
                long companyId = currentUser.getCompanyId();            
                long groupId = GroupLocalServiceUtil.getGroup(companyId, "Guest").getGroupId();

                /* Get de CustomField Segmentation */
                ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getDefaultTable(companyId, User.class.getName());

                ExpandoColumn expandoColumn = ExpandoColumnLocalServiceUtil.getColumn(companyId, User.class.getName(), expandoTable.getName(), "Segmentation");           


          if (expandoColumn != null) {
                 ExpandoValue expandoValue = ExpandoValueLocalServiceUtil.getValue(expandoTable.getTableId(), expandoColumn.getColumnId(), userId);

          if (expandoValue != null) {
                 expandoValue.setData(finalsegment);


                 ExpandoValueLocalServiceUtil.updateExpandoValue(expandoValue);

          }
   }

}

Summary: My problem is that

ExpandoValue expandoValue = ExpandoValueLocalServiceUtil.getValue(expandoTable.getTableId(), expandoColumn.getColumnId(), classPK);

is Null when I access the Value of Custom Attribute. If I edit by hand this customattribute and then execute the same Code it works!!! I don't know why and I dont know how to solve this.

4

3 回答 3

1

编辑(在您更新问题后)

查看ExpandoValueLocalService javadoc:您会发现有一个createExpandoValue方法。现在猜测场景“您根本没有手动设置值,并且您null以 ExpandoValue 的形式返回”与“您已设置一次并取回可以更新的值...

另一种选择也是为您的 expando 值指定一个默认值——这样你肯定会有一个值在那里,你可以无条件地更新它(至少在它被故意删除之前——为了稳健性,你仍然应该满足这种可能性)

原答案:

除了在这种if情况下,你还会去哪里?您是否尝试过某个else条件或者您之前是否遇到过任何异常?例如,您可能需要先创建“默认”表,然后才能获取它。

有关如何访问 Expando 表/列的示例,请参阅此代码。

我没有运行您的代码,但当然也可能更早发生异常。或者您可能在配置 时犯了一个错误LoginAction,因此它根本无法运行。

于 2013-07-02T14:09:07.147 回答
1

默认情况下,普通用户角色无权访问 Expando 值。

无论如何,最好用

User user = UserLocalService.getUserById(userId);
user.getExpandoBridge().setAttribute("attributeName", "attributeValue");

如果要使用任何权限修改值,请使用

user.getExpandoBridge().setAttribute("attributeName", "attributeValue", false);
于 2013-07-04T08:20:51.723 回答
0

在这里我找到了问题的答案..

您可以使用建议的代码管理解决方案。

http://www.liferay.com/es/community/forums/-/message_boards/message/26154530

于 2013-07-03T09:58:01.883 回答