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
Thomas Gordon 20Thomas Gordon 20 

soql gone mad...

select id, name from account where name = :user.userrole.name

does not return anything due to an error in this query. the bind variable is used in a different query so it is valid as a value. This must be a relationship issue. Can anybody confirm or offer a fix please?
 
Mahesh DMahesh D
Hi Thomas,

Please look into the below SOQL:
 
String roleName = UserInfo.getUserName();
List<Account> accList = [select id, name from account where name = :roleName];
System.debug('==========accList:'+accList);

Please do let me know if it helps you.

Regards,
Mahesh
BHinnersBHinners
You are specifying that the Account Name field must be equal to the name of a Salesforce User Role.  Have your really got Roles set up for every different Account in your org?  Or should you be looking for the name to match something else?  Or maybe looking for the Account Owner to have a specific role?
Amit Chaudhary 8Amit Chaudhary 8
Hi Thomas,

If you want to fatch any information for login user then please check below post.
UserInfo Class
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_userinfo.htm

To Learn about SOQL please check below post.
From SQL to SOQL
1) https://developer.salesforce.com/page/From_SQL_to_SOQL

A Deeper look at SOQL and Relationship Queries on Force.com
1) https://developer.salesforce.com/page/A_Deeper_look_at_SOQL_and_Relationship_Queries_on_Force.com


Some use full link for you
1) https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL.htm

I recommend  you to please check below trailhead module
1) https://developer.salesforce.com/trailhead/en/apex_database/apex_database_soql


If you want to learn the Dynamic SOQL and variable binding please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm
2) http://www.sfdc99.com/2013/11/04/using-apex-variables-in-a-soql-query/

Example 1:-  Salesforce knows you’re using a bind variable when you precede your Apex variable with a colon (:) –
String myFamilyName = 'Liu';
List<Contact> myFamily = [SELECT FirstName, Best_Friend__c
                           FROM Contact
                           WHERE LastName = :myFamilyName];

Please let us know if this will help you

Thanks,
Amit Chaudhary
Thomas Gordon 20Thomas Gordon 20
trigger updateFieldwithMissionName on Contact (before insert, before update){ 
    Map <String, String> Users = new Map <String, String>();
    for (Contact Con : Trigger.new){
            Users.put(name,User.userrole.name);
         for (Account a: [select Id, name from Account where Name = :user.userrole.name]){
            Users.put ('user.userrole.name', a.name);
            for(User usr:[select Id, name,userrole.name from User]){
                 if(Usr!= null ||con.Account.Name != user.userrole.name){
                 update(con);
Mahesh,This is the entire trigger. ..not using Account obj as Accounts (BHinners). Whole thing is prob a train wreck. Supposed to update field on contacts from Userrole that owns the contact by mapping them...'put' lines are a mess too. ..my first trigger!..at least it has structure lol 
BHinnersBHinners
Thomas, what results do you anticipate seeing from the search Account a: [select Id, name from Account...?
Where are you setting values for fields on the Contacts?
Mahesh DMahesh D
Hi Thomas,

Please provide the actual requirement. What exactly you are trying to achieve.

You have written the code with all SOQLs inside the for loop which is not a best practice.

Regards,
Mahesh
Thomas Gordon 20Thomas Gordon 20
To replace a Process Builder function that has multiple ' if 'clauses. When a contact is added, the  mission name field is updated from the contact owner (User.userrole .name)
Thomas Gordon 20Thomas Gordon 20
BHinners, the search on 'a' is to provide the account name that matches the user role name. I update the field with 'update(con')
BHinnersBHinners
You say that you are looking for an Account with the same name as User Role? How many Roles does your org have? One for every Account? Also, update(con) is only going to update records if you've changed values on the Contact fields with assignments somewhere in your trigger. I don't see any assignments for Coontact fields. There are a lot of great resources posted above, which you should definitely review.
Mahesh DMahesh D
Hi Thomas,

Please copy your full trigger which you have written by using the above Panel (< >) so that we can able to read it properly.

Also where is the mission name and what is its API Name.

Regards,
Mahesh
Thomas Gordon 20Thomas Gordon 20
trigger updateFieldwithMissionName on Contact (before insert, before update){ 
    integer mycount =0;
    //String roleName = UserInfo.getUserRoleId();
    
    for (Contact Con : Trigger.new){
            
        List <Account> accList = [select Id, name from Account];
            
        List <User> usr = [select Id, name, userrole.name from User];

        Map<Id, Account> Users = new Map<Id, Account>();
        for(Account acc : accList){
            Users.put(acc.name, acc);
        }
        for(User u : usr){
            if(Users.containsKey(u.userrole.name)){
                update (con);               
            }            
        }
    } 
}

Im taking the mission name from the User that owns the contact. It is the user.userrole.name field. Above is a new approach using 2 Lists and Map. Still need to update the field on the Contact which is actually c.account.name