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
sales4cesales4ce 

Trigger firing--help!

Hi,

 

I have a question in regards with when to fire a trigger.

 

Can i say on a Object , fire a trigger only when the Owner is "Some User".

 

I want to fire a trigger only when "user x" is the owner of the record.

 

Thanks in advance.

 

Sales4ce

Best Answer chosen by Admin (Salesforce Developers) 
John De SantiagoJohn De Santiago

Sure its possible. You just need to pick them out from the potential list of records that are batched as part of a trigger call.

 

example:

 

List<Account> listAccounts = new List<Accounts>();

 

for (Account acct : Trigger.new) {

if (acct.OwnerId == 'Some Owner Id') {

listAccounts.add(acct);

  }

}

 

if (listAccounts.isEmpty() == false) {

//Process the list of accounts identified by owner

}

 

My suggestion would be to use CustomSettings to store the user id you want to check for rather than hardcoding the owner id in your code. Then you can modify the code to either process for a specific owner or multiple owner ids if you select a list type custom setting.

 

Hope this helps gets you started.

 

 

All Answers

John De SantiagoJohn De Santiago

Sure its possible. You just need to pick them out from the potential list of records that are batched as part of a trigger call.

 

example:

 

List<Account> listAccounts = new List<Accounts>();

 

for (Account acct : Trigger.new) {

if (acct.OwnerId == 'Some Owner Id') {

listAccounts.add(acct);

  }

}

 

if (listAccounts.isEmpty() == false) {

//Process the list of accounts identified by owner

}

 

My suggestion would be to use CustomSettings to store the user id you want to check for rather than hardcoding the owner id in your code. Then you can modify the code to either process for a specific owner or multiple owner ids if you select a list type custom setting.

 

Hope this helps gets you started.

 

 

This was selected as the best answer
Pradeep_NavatarPradeep_Navatar

You can get owner id of that particular object as given in the example below :

 

Opportunity opp = [select owner.name,ownerid from opportunity where ownerid=:userinfo.getuserid()];

 

Hope this helps.