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
niven sfniven sf 

How to check checkbox in contact when user email and contact emails are same and also checks users profile name equal to customer user

Best Answer chosen by niven sf
Prashant Pandey07Prashant Pandey07
Hi Niven,

I guess you just need to check the user.contactid should not be null...try below code and let me know if this works for you..
 
trigger updatecontactcheck on Contact (after update) {

    set<id> ids=trigger.newMap.keySet();
 
    List<contact>con = [select id,EmailSameAsContact__c,Email  from contact where id =: ids];
  
   user u=[select id,email,profile.Name,phone from user where profile.Name = 'YMCA Community User' Limit 1];
 
      list<contact> conlist=new List<contact>();
      for(Contact c:cont){
          if(c.email==u.email && c.EmailSameAsContact__c!=true && u.contactid!=null){
      
          c.EmailSameAsContact__c=true;
          conlist.add(c);
          
          }
      }
       if (!conlist.isEmpty()){

     update conlist;

}
}

--
Thanks,
Prashant

All Answers

Manj_SFDCManj_SFDC
You can make the checkbox field as a formula field and update else you can go for field update through workflow rule

please mark this as solved if this helps you
Prashant Pandey07Prashant Pandey07
Hi Niven,

You can use bleow trigger to solve your problem.. you can also use workflow or formula fields as Manj_SFDC suggested but the problem with workflow or formula field is you need to hardcode the profileID . or else you can try with Contact owner profile.
trigger updatecheckbox on Contact (after update) {

    set<id> ids=trigger.newMap.keySet();
 
    List<contact>con = [select id,fax,checkbox__c,email,phone from contact where id=:ids];
  
    user u=[select id,email,profile.Name,phone from user where id=:UserInfo.getUserId()];
 
      list<contact> conlist=new List<contact>();
      for(Contact c:con){
          if(c.email==u.email&& u.profile.Name=='Customer user' && c.checkbox__c!=true ){
      
          c.checkbox__c=true;
          conlist.add(c);
          
          }
      }
       if (!conlist.isEmpty()){

  update conlist;

}
}
--
Thanks,
Prashant
 
Manj_SFDCManj_SFDC
You don’t have to hard code the profile id if you wish to use the formula field , you can use the profile name 
Manj_SFDCManj_SFDC
do you mean you are trying to compare the contact email id and the logged in user email id/username
Manj_SFDCManj_SFDC
what is the issue are you facing, do you need to do it through code or formula field will do
Manj_SFDCManj_SFDC
can you please paste your code here
Manj_SFDCManj_SFDC
please let me know what does this query
user u=[select id,email,profile.Name,phone from user where profile.Name = 'YMCA Community User' Limit 1];
returning?
Manj_SFDCManj_SFDC
then where is the problem you facing ,please let me know
Manj_SFDCManj_SFDC
hope you are not using UserInfo.getUserId() in your query, if yes try by logging in as the community user and test
Prashant Pandey07Prashant Pandey07
Hi Niven,

I guess you just need to check the user.contactid should not be null...try below code and let me know if this works for you..
 
trigger updatecontactcheck on Contact (after update) {

    set<id> ids=trigger.newMap.keySet();
 
    List<contact>con = [select id,EmailSameAsContact__c,Email  from contact where id =: ids];
  
   user u=[select id,email,profile.Name,phone from user where profile.Name = 'YMCA Community User' Limit 1];
 
      list<contact> conlist=new List<contact>();
      for(Contact c:cont){
          if(c.email==u.email && c.EmailSameAsContact__c!=true && u.contactid!=null){
      
          c.EmailSameAsContact__c=true;
          conlist.add(c);
          
          }
      }
       if (!conlist.isEmpty()){

     update conlist;

}
}

--
Thanks,
Prashant
This was selected as the best answer
Manj_SFDCManj_SFDC
Hi Niven, change the line 07 from 
user u=[select id,email,profile.Name,phone from user where profile.Name = 'YMCA Community User'Limit 1];
to
user u=[select id,email,profile.Name,phone from user where profile.Name = 'YMCA Community User' and  id=:UserInfo.getUserId() ];
login with the user who has the YMCA Community User' profile

please mark the qustion as solved if this helps you
Good luck !
Prashant Pandey07Prashant Pandey07
Niven,

It should work..not sure how are you testing..just log in as a community user and try to update the contact record and check if the checkbox should be check..I have tested and its working for me..

--
Thanks,
Prashant