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
suryaaSarveshsuryaaSarvesh 

Trigger on user object

Hello,

I am a newbie to Apex triggers and need an advice with the following requirement.

Requirement:

We need to have a report generated out of UserPackageLicense object to track number of managed package licenses being used. With my reasearch, I figured out reporting on UserPackageLicense object is not possible since it is not exposed in reporting.

Hence I decided to have a custom pick list at user object and whenever there is an update to a user object, I tried to fire a trigger to go and check UserPackageLicense object for that user and update the custom pick list in user object. I started with the below code to test

trigger UPackageLicense on User (before update) {
    List<UserPackageLicense> up =new List<UserPackageLicense>();
    for(User u : Trigger.new){
        u.Id=up.UserId;
        u.Trigger__c=up.UserId;
       Update u;
    }
}

But getting a below error
AfterUpdate caused by: System.FinalException: Record is read-only

Any help is much appreciated!

Thanks,
Sathish LoganathanSathish Loganathan
Hi,

Based on the code you posted, it looks like you are trying to upadate the user id , which cannot be updated(ID of any object in Salesforce cannot be updated).

trigger UPackageLicense on User (before update) {
    List<UserPackageLicense> up =new List<UserPackageLicense>();
    for(User u : Trigger.new){
        u.Id=up.UserId; - > ID cannot be updated on any Salesforce objects
        u.Trigger__c=up.UserId;
       Update u;
    }
}
 
Ajay K DubediAjay K Dubedi
Hi suryaa,

Id field of any record can't be changable.Every Record, regardless of entity type, has a globally unique identification value in its ID field which is generated at the time of record creation. That Record ID value will never change, even if the record is deleted and then undeleted.
For more details go to with below link:
https://help.salesforce.com/articleView?id=000324087&type=1&mode=1

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi