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
Mitchell McLaughlin 1Mitchell McLaughlin 1 

Set Lookup by Salesforce ID

Hi -

I'm trying to set the Corresponding_Sales_VP__c trigger to a specific user record by using their ID. How do i do it?
 
trigger Set_Corresponding_VP on Compensation_Request__c(after insert) {
	List<Compensation_Request__c> requestsToUpdate = new List<Compensation_Request__c>();
    for(Compensation_Request__c reqs : Trigger.new){
        reqs.Corresponding_Sales_VP__c = //User Record with id 005j000000FmuFu
            requestsToUpdate.add(reqs);
    }
    update requestsToUpdate;
}

 
Tanner RussellTanner Russell
User u = [Select id from user where id = '005j000000FmuFu'];
 
trigger Set_Corresponding_VP on Compensation_Request__c(after insert) {
	List<Compensation_Request__c> requestsToUpdate = new List<Compensation_Request__c>();
User u = [select id from user where id ='005j000000FmuFu'];
    for(Compensation_Request__c reqs : Trigger.new){
        reqs.Corresponding_Sales_VP__c = u.id;
            requestsToUpdate.add(reqs);
    }
    update requestsToUpdate;
}
Arvind KumarArvind Kumar
Hi M,

Use It, It will work for you.
 
trigger Set_Corresponding_VP on Compensation_Request__c(before Insert , before update)
 {

    
    User u = [select id from user where id ='005280000025QXx'];
    for(Compensation_Request__c reqs : Trigger.new)
    {
        reqs.Corresponding_Sales_VP__c = u.id;
            
    }
    
}

Thanks
Prateek BhattPrateek Bhatt
HI Mitchell,

You can directly put the userid in that field.Also you can user before insert trigger instead of after insert. It doesn't require the DML statement to update the record 
 
trigger Set_Corresponding_VP on Compensation_Request__c(before insert) {
    for(Compensation_Request__c reqs : Trigger.new){
        reqs.Corresponding_Sales_VP__c = '005j000000FmuFu';       
    }   
}

Thanks,