如何在 salesforce 中使用触发器更新用户的帐户字段?
How to Update Account field for User by using Trigger in salesforce?
创建新用户时,如果用户的账户名是'test'
,那么我想创建一个新账户,名称为用户公司名,
并将其替换为用户详细信息中以前的帐户名称。
trigger UpdateAccount on User (after insert,before update) {
Set<Id> ContactId=new Set<Id>();
for(User us:Trigger.New){
if(us.AccountId=='001N000000Z0W9R'){
ContactID.add(us.ContactId);
for(Contact c:[select id,AccountId from Contact where id in:ContactID]){
Account Acc = new Account(Name=us.CompanyName);
insert Acc;
c.AccountId=Acc.Id;
update c;
}
update us;
}
}
}
问题已解决
答案在这里
trigger UpdateAccount on User (after insert,after update)
{
Set<Id> contactIds = new Set<Id>();
List<User> UserList= new List<User>();
for (User u : Trigger.new) {
if (u.AccountId=='001N000000Z0W9R')
contactIds.add(u.ContactId);
UserList.add(u);
}
if (contactIds.size() > 0) {
List<Contact> contacts = [select id,AccountId from Contact where Id in :contactIds];
for (Contact c : contacts) {
for(User u :UserList){
Account acc= new Account(Name=u.CompanyName);
acc.OwnerId = '00590000003lP6Z';
insert acc;
c.AccountId=acc.id;
}
}
// update UserList;
update contacts;
} }
创建新用户时,如果用户的账户名是'test'
,那么我想创建一个新账户,名称为用户公司名,
并将其替换为用户详细信息中以前的帐户名称。
trigger UpdateAccount on User (after insert,before update) {
Set<Id> ContactId=new Set<Id>();
for(User us:Trigger.New){
if(us.AccountId=='001N000000Z0W9R'){
ContactID.add(us.ContactId);
for(Contact c:[select id,AccountId from Contact where id in:ContactID]){
Account Acc = new Account(Name=us.CompanyName);
insert Acc;
c.AccountId=Acc.Id;
update c;
}
update us;
}
}
}
问题已解决 答案在这里
trigger UpdateAccount on User (after insert,after update)
{
Set<Id> contactIds = new Set<Id>();
List<User> UserList= new List<User>();
for (User u : Trigger.new) {
if (u.AccountId=='001N000000Z0W9R')
contactIds.add(u.ContactId);
UserList.add(u);
}
if (contactIds.size() > 0) {
List<Contact> contacts = [select id,AccountId from Contact where Id in :contactIds];
for (Contact c : contacts) {
for(User u :UserList){
Account acc= new Account(Name=u.CompanyName);
acc.OwnerId = '00590000003lP6Z';
insert acc;
c.AccountId=acc.id;
}
}
// update UserList;
update contacts;
} }