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
SIVASASNKARSIVASASNKAR 

Query to Identify all records owned by a User

Hi All,

I would like to query all SObject recrods owned by specific user, can you please guide me how can we do that?

My requirement:
We would like to deactivate the more Users in our org, hence instead of transfering the records for each user manually, require to implement through apex that all records will transfer from one User  to another on button click. any alternative suggestion would be appriciated..

Thanks
Sivasankar. 
ManojjenaManojjena
Hi SIVASASNKAR,

You can do one thing populate all sobject in one picklist and click one button to call a method and in the method you can construct a dynamic query by passing selected picklist as sobject type in query and ownerid as your desired userid ,

Itetrate if the return record size is grater then zero then assign the new userid to the ownerid field and update that list .

Let me know if it helps .

Thanks 
Manoj

 
Harpreet On CloudHarpreet On Cloud
Updating multiple records - you need to take care of the Governer limits, so cannot update more than 10,000 records in one context and hence may have to use Batch process. But yes, there are many options to implement it

Provide a custom button on Detail page - "Update owner". Clicking on button takes you to a page asking to select a new Owner. Selecting the new Owner and clicking button on custom page, updates all records of that sObject type with older Owner Id to the new Owner Id.

This implementation can be generalised to use for each and every sObject.
SIVASASNKARSIVASASNKAR
Hi Manoj, Harpreet,

Thank you very much for your answers.

Manoj,

can you provide me code for building the dynamic query by passing the list of objects to get more clear on your suggestion. Are you suggesting SOSL query or SOQL query?

Thank you angain both.
Sivasankar.
ManojjenaManojjena
Hi Sivasankar,

Try with below code .You will get the list of records in search method after that you can do the manipulation what ever you want .
 
public class QueryController {
	public string selectedSobject{get;set;}
	public List<Sobject> lstQuery{get;set;}
	public List <SelectOption> objectNames {get; set;}
    public QueryController(){
        lstQuery=new List<Sobject>();
        objectNames = initObjNames();
    }
    
    public Void doSearch(){
        string Query='';
        Query='SELECT id,Name FROM '+ selectedSobject;
        lstQuery=Database.query(Query);
    }
     public  List<SelectOption> initObjNames() {
        List<SelectOption> objNames = new List<SelectOption>();
        List<Schema.SobjectType> obj = Schema.getGlobalDescribe().Values();
        for(Schema.SobjectType ss:obj) {
            objNames.add(new SelectOption(ss.getDescribe().getName(), ss.getDescribe().getName()));
            objNames.sort();
        }
        return objNames;
    }
}

<apex:page controller="QueryController">
    <apex:form >
       <apex:selectList value="{!selectedSobject}" size="1">
            <apex:selectOptions value="{!objectNames }"/>
        </apex:selectList>
        <apex:commandButton value="Search" action="{!doSearch}"/>
    </apex:form>
</apex:page>

Please let me know if it helps .

Thanks 
Manoj
SIVASASNKARSIVASASNKAR
Hi Manoj,

Thank you very much for your time and much approciated. We have similar standard functionality to Mass transfer the records by individual objects. But Users are not willing to do this activity for every object they are expecting that when User hits the Deactivate button page will open to prompt the new user to replace and transfer the recrods to new owner. After selecting the new owner and hit the OK button, mass transfer should be happen to new user irrespective of SObject Type.

Hence I have queried to retrieve all the records owned by User irrespective of  SObject. If we get all records then we can chage the Owner of records. I hope you got th point, please let me know if you require any more information.

Thank you again for you time on this. 

Thanks
Sivasankar.
7J7J
Hi Siva Shankar,

Have you got any update for your request?

Even i'm looking for the same logic for my requirement, which you have mentioned above.

Thanks for your help in advance.

Regards,
7J
cjpradcjprad
@Manoj How are you handling Governer limits?
Pooja9426Pooja9426
Hi Siva Shankar,

Have you got any solution for your request?

I'm looking for the same requirement, which you have mentioned above.

Thanks,
Pooja
yogananda pathiyogananda pathi
Hi Siva Shankar,

Have you got any solution for your request?

 I am looking for related requirement 
Requirement:We need to create a Lightning where you will get all the records related to that particular User. And you can replace or Change the name for that records.