+ Start a Discussion
oceanofstarsoceanofstars 

Text value with ID need to be updated in lookup field

Hi All

I have an Text field on user object which contains ID value of another object. I am trying to get that Text field in to an lookup field in account object. below is my trigger

trigger UpdateAccountUpsell 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, Commissionable_Upsell_Rep__c , of_Closed_Won_Opportunities__c from Account where Id IN :closedOpp]) {

if(acc.Commissionable_Upsell_Rep__c == ''){
acc.Commissionable_Upsell_Rep__c = acc.Owner.Split_Sales_Rep_ID__c;
}
update acc;
}

I am able to save the trigger but it is not updating the lookup field.

 

SeAlVaSeAlVa

Try changing 

if(acc.Commissionable_Upsell_Rep__c == ''){
  acc.Commissionable_Upsell_Rep__c = acc.Owner.Split_Sales_Rep_ID__c;
}

 to

if(acc.Commissionable_Upsell_Rep__c == null){
  acc.Commissionable_Upsell_Rep__c = acc.Owner.Split_Sales_Rep_ID__c;
  // I'm not sure if it needs casting, just in case...
  // acc.Commissionable_Upsell_Rep__c = ((ID) acc.Owner.Split_Sales_Rep_ID__c);
}