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
RedSalesRedSales 

How to Update a User & Set a Reference ID to empty

Hi,

 

Within ym user object I have reference fields such as  "Approval Manager ". This field is a refence to the user object & by entereing a user id it displays the user's name as the manager.

I wish to set this value to empty using APEX code if data I retrieve (from a source external to SFDC) indicates no Approval Manager is provided.  I receive an error saying I'm assigning an invalid ID

 

my 2 lines of code are

 

usr.Approval_Manager__c='';

update usr;  

 

Does anyone know if I can set this to blank threfore?

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
hemantgarghemantgarg

no need to put assign blank '' into manager field , just assign null to it like following :-

 

usr.Approval_Manager__c=null;

update usr;  

 

it should work, the issue with your code is the manager is a lookup to user not a text field so it will not allow to assign string in the id field.

 

Cheers

Hemant



All Answers

Rahul SharmaRahul Sharma

put a try catch block:

 

try{
	update usr;  
	usr.Approval_Manager__c='';}
Catch(Exception e){
	// Either show the error or process it.
}

 

hemantgarghemantgarg

no need to put assign blank '' into manager field , just assign null to it like following :-

 

usr.Approval_Manager__c=null;

update usr;  

 

it should work, the issue with your code is the manager is a lookup to user not a text field so it will not allow to assign string in the id field.

 

Cheers

Hemant



This was selected as the best answer