You need to sign in to do that
Don't have an account?
johnc82
Update Lookup field with ID from Text field
When a user is created, we have a trigger that creates a record in another object with some user info. The ID of that new record created is also updated back to the user record in a custom text field to hold for reference. I'm trying to do a trigger to update a lookup field with that ID that I'm pulling from the text field on the user record. The trigger is running but the field is not updating. I assume it's because I'm trying to place a text value in an ID field?
Anyone have ideas?
trigger UpdateAccountNewLookupField on Opportunity (after update) { List<Account> accountList= new List<Account>(); List<User> userList= new List<User>(); Set<Id> closedOpp= new set<Id>(); for (Opportunity opp: Trigger.new) { if(Trigger.oldMap.get(opp.Id).StageName != 'Closed Won' && opp.StageName == 'Closed Won') { closedOpp.add(opp.AccountId); } } for (Account acc: [select Id, Owner.Textfield__c, NewLookupFieldToPopulate__c from Account where Id IN :closedOpp]) { if(acc.NewLookupFieldToPopulate__c == ''){ acc.NewLookupFieldToPopulate__c = acc.Owner.Split_Sales_Rep_ID__c; } update acc; } }
try this . add the updated record to the Account List and then upate the list.
for (Account acc: [select Id, Owner.Textfield__c, NewLookupFieldToPopulate__c from Account where Id IN :closedOpp]) {
if(acc.NewLookupFieldToPopulate__c == ''){
acc.NewLookupFieldToPopulate__c = acc.Owner.Split_Sales_Rep_ID__c;
accountList.add(acc);
}
update accountList;
}
}
Thanks
Tried that but still not working.
try this . change your if statement to something like this
if(acc.NewLookupFieldToPopulate__c == '' || acc.NewLookupFieldToPopulate__c == null
Thanks)
Still not populating. It just doesn't make sense.
i used the below code and tested it in my instance and i was able to update the text field on account.
i belive Split_Sales_Rep_ID__c is a custom text field on User object right???
List<Account> accountList= new List<Account>();
List<User> userList= new List<User>();
Set<Id> closedOpp= new set<Id>();
for (Opportunity opp: Trigger.new) {
if(Trigger.oldMap.get(opp.Id).StageName != 'Closed Won' && opp.StageName == 'Closed Won') {
closedOpp.add(opp.AccountId);
}
}
for (Account acc: [select Id, Owner.Split_Sales_Rep_ID__c, NewLookupFieldToPopulate__c from Account where Id IN :closedOpp]) {
if(acc.NewLookupFieldToPopulate__c == '' || acc.NewLookupFieldToPopulate__c==null){
acc.NewLookupFieldToPopulate__c = acc.Owner.Split_Sales_Rep_ID__c;
accountList.add(acc);
}
update accountList;
}
}
Yes, Split_Sales_Rep_ID__c is a custom text field on the User object. However, the NewLookupFieldToPopulate__c on account is a lookup field. So I'm trying to place the ID of the Split Sales Rep in the Lookup field to that object on the account called NewLookupFieldToPopulate__c
Just make sure your look up field NewLookupFieldToPopulate__c on account is looked up to the particular object whose id is stored in the text field on the User object.
Yeah I've checked that several times and it is.
Thats wierd!!!!! it seems to be updating correctly when i tested it in my instance. Just make sure you are testing it correctly by changing the opportunity status to closed won
Try casting the value as an ID primitive:
If that doesn't work -- does system.debug show a value in the split_sales text field when you retrieve it? And is your second if statement actually executing?
BTW - I think it's better to use == null instead of == '' when testing an Id field.