自动创建社区用户

时间:2013-12-26 15:52:51

标签: salesforce apex-code

是否可以自动化在Salesforce中创建社区用户的过程?我在想联系人的触发器,然后用社区许可证创建用户记录。这有可能吗?

感谢。

1 个答案:

答案 0 :(得分:0)

是的,它应该是这样的

Contact con = [select id,email,firstName,lastname,accountId from Contact where Id =:contactId];         

Database.DMLOptions dmo = new Database.DMLOptions();
dmo.EmailHeader.triggerUserEmail = false;       
dmo.EmailHeader.triggerOtherEmail = false;
dmo.EmailHeader.triggerAutoResponseEmail = false;       
dmo.optAllOrNone = false;

// create portal user
string nick = con.email!=null?con.email.substring(0, con.email.indexOf('@')):'';
nick += Datetime.now().getTime();
User newUser = new User(
                    alias = createAlias(con.firstName, con.lastName), 
                    email = con.email, 
                    emailencodingkey = 'UTF-8', 
                    firstname = con.firstName, 
                    lastname = con.lastname, 
                    languagelocalekey = 'en_US', 
                    localesidkey = 'en_US', 
                    contactId = con.Id,
                    timezonesidkey = 'Asia/Dubai', 
                    username = con.email,
                    CommunityNickname = nick,
                    ProfileId = .......,
                    IsActive = true);

newUser.setOptions(dmo);
insert newUser;