function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
johnc82johnc82 

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;
    }
}

 

New_DeveloperNew_Developer

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

johnc82johnc82

Tried that but still not working.

New_DeveloperNew_Developer

try this . change your if statement to something like this

 

if(acc.NewLookupFieldToPopulate__c == '' ||  acc.NewLookupFieldToPopulate__c == null

 

Thanks)

johnc82johnc82

Still not populating.  It just doesn't make sense.

New_DeveloperNew_Developer

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???

 

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.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;
    }
}



johnc82johnc82

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

New_DeveloperNew_Developer

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.

johnc82johnc82

Yeah I've checked that several times and it is.

New_DeveloperNew_Developer

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

dmchengdmcheng

Try casting the value as an ID primitive:

 

acc.NewLookupFieldToPopulate__c = (Id)acc.Owner.Split_Sales_Rep_ID__c

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.