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
helaCHhelaCH 

OPPORTUNITYID

Hello!
I'm new to salesforce  and i need help ,i have a user logged to experience site how can i fetch opportunity id of that user 
Best Answer chosen by helaCH
SubratSubrat (Salesforce Developers) 
Hello ,

To fetch the Opportunity IDs of the user who is logged into the Experience Site, you can write a SOQL query on the Opportunity object with a filter on the OwnerId field, which represents the user who owns the opportunity.

Here is an example of how you can write a query to fetch the Opportunity IDs for the logged-in user:
Id loggedInUserId = UserInfo.getUserId();
List<Opportunity> oppList = [SELECT Id FROM Opportunity WHERE OwnerId =: loggedInUserId];

In this example, we first get the Id of the logged-in user using the UserInfo.getUserId() method. We then use this Id to query the Opportunity object to fetch all the opportunities owned by this user.

You can use this query in your Apex code or controller to get the Opportunity IDs for the logged-in user.

If it helps , please mark this as Best Answer.
Thank you.

All Answers

Richard Fiekowsky 3Richard Fiekowsky 3
1) get the record-ID of that user - it's part of the URL when you display that User record in your Salesforce UI. For instance , on my system it looks like 
https://mydomain.lightning.force.com/lightning/setup/ManageUsers/page?address=%2F005511000074XGN%3Fnoredirect%3D1%26isUserEntityOverride%3D1
and the record-ID  of that User record is 005511000074XGN , the part between %2F and %3F . 
2) Fire up your favorite querying tool such as Workbench or Fusekit. 
3) The opportunities that the user owns are returned by:this SOQL query
SELECT Id, Name FROM Opportunity WHERE OwnerId = '005500000074XGN' 
Instead of the OwnerID field one could use CreatedBy , it all depends on what exactly you want to find.
SubratSubrat (Salesforce Developers) 
Hello ,

To fetch the Opportunity IDs of the user who is logged into the Experience Site, you can write a SOQL query on the Opportunity object with a filter on the OwnerId field, which represents the user who owns the opportunity.

Here is an example of how you can write a query to fetch the Opportunity IDs for the logged-in user:
Id loggedInUserId = UserInfo.getUserId();
List<Opportunity> oppList = [SELECT Id FROM Opportunity WHERE OwnerId =: loggedInUserId];

In this example, we first get the Id of the logged-in user using the UserInfo.getUserId() method. We then use this Id to query the Opportunity object to fetch all the opportunities owned by this user.

You can use this query in your Apex code or controller to get the Opportunity IDs for the logged-in user.

If it helps , please mark this as Best Answer.
Thank you.
This was selected as the best answer
Prateek Prasoon 25Prateek Prasoon 25
// Get the current user's ID
Id userId = UserInfo.getUserId();
// Query the Opportunity object and filter by the current user's ID
List<Opportunity> opportunities = [SELECT Id FROM Opportunity WHERE OwnerId = :userId];
// Loop through the opportunities and get the IDs
List<Id> opportunityIds = new List<Id>();
for(Opportunity opp : opportunities) {
    opportunityIds.add(opp.Id);
}
// The opportunityIds list now contains the IDs of all the opportunities owned by the current user

This code retrieves the ID of the currently logged-in user using the UserInfo.getUserId() method. It then queries the Opportunity object and filters by the OwnerId field to only retrieve opportunities owned by the current user. Finally, it loops through the results and adds the opportunity IDs to a list.
Note that you may need to modify this code depending on your specific use case and how your experience site is set up. You may also want to add error handling and other checks to ensure that the code behaves correctly in all situations.

If you find my answer helpful, please mark it as the best answer. Thanks!