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
Gunwinder 11Gunwinder 11 

Fetching values from one list to another

Experts, 

I am a novice to Apex and having trouble to build a class wherein I want to fetch values from one list to another. 
 
List<User> UsersList = [select id,contactid,Gold_Partner_User__c,Customer_Portal_User__c FROM USER  WHERE isActive=TRUE AND Customer_Portal_User__c = TRUE];	
        
List<contact> ContactList =[Select id,Customer_Portal_Lic_assigned__c 
FROM contact WHERE ID IN : UsersList AND Customer_Portal_Lic_assigned__c=FALSE];
background: 

On USER object, Gold_Partner_User__c and Customer_Portal_User__c are formula fields which identify the type of license assigned. 

Our customers who do not have access to USER object, I have created Customer_Portal_Lic_assigned__c on contact object so that a contact list view / report could be created for them.
 
I want to build a scheduled apex job that identifies contacts where users who are mapped with a customer portal license are identified and the related contact object is updated with the checkbox. 

Kindly suggest how should be best done.
mukesh guptamukesh gupta
Hi Gunvinder, 

Please use below query:-
 
Set<Id>conIds = new Set<Id>();
List<User> UsersList = [select id,contactid,Gold_Partner_User__c,Customer_Portal_User__c FROM USER  WHERE isActive=TRUE AND Customer_Portal_User__c = TRUE];	
        
for(user u: UsersList){
	conIds.add(u.contactid);
}		
		
List<contact> ContactList =[Select id,Customer_Portal_Lic_assigned__c 
FROM contact WHERE ID IN : conIds AND Customer_Portal_Lic_assigned__c=FALSE];

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
Gunwinder 11Gunwinder 11
Thank you Mukesh for your prompt response. I attempted to create test class and had to go through several iterations. The apex classes look as under 
 
global class UpdateCPortalContacts implements Schedulable {
    global void execute(SchedulableContext ctx) {

        Set<Id>conIds = new Set<Id>();
        
        List<User> UsersList = [select id,contactid,Gold_Partner_User__c,Customer_Portal_User__c 
                                FROM USER 
                                WHERE isActive=TRUE AND Customer_Portal_User__c = TRUE]; 
                                
       for(user u: UsersList){
            conIds.add(u.contactid);
        }                            
        
       List<contact> ContactList =[Select id,Customer_Portal_Lic_assigned__c 
                                   FROM contact 
                                   WHERE ID IN : conIds AND Customer_Portal_Lic_assigned__c=FALSE]; 
        if(!ContactList.isempty()){
                   for(contact c:ContactList){
           			 c.Customer_Portal_Lic_assigned__c=TRUE;            
        			}
        UPDATE ContactList;  
        }      
    }
}

Test class : 
 
@isTest
public class UpdateCPortalContactsTest {
public static string uId;    
@testsetup
    static void setup(){ // dummy
         contact c = new contact();
         c.LastName='gunwinder';
         c.email='goolied@gmail.com';
             
        insert c;     
       // https://help.salesforce.com/s/articleView?id=000330124&type=1
       // This error is generated because the user who running the code does not have a role.
       // Ensure that the user running the code below has a role assigned (eg. Sys Admin user does not have a role assigned by default).
       // To resolve the error, assign the User a Role. 
       
       UserRole userrole = [Select Id, DeveloperName From UserRole Where DeveloperName = 'System Administrator' Limit 1];

       User adminUser = [Select Id, UserRoleId From User Where Profile.Name='System Administrator' Limit 1];

	   adminUser.UserRoleId = userRole.Id;
	   update adminUser;
        
       System.runAs(adminUser){ 
       list <user> listofUsers = new list<user>();
       for (integer i=0;i<2;i++){
            user u = new user ( 
            username='goolied'+i+'@gmail.com',
            Email='goolied'+i+'@gmail.com',
            alias='gr'+ i,
            LastName='test user',
            TimeZoneSidKey='America/Los_Angeles',
            LocaleSidKey='en_US',           
            EmailEncodingKey='UTF-8',    
            ProfileId='00e0W0000026rzK',
            LanguageLocaleKey='en_US',    
            contactid=c.Id   
            );
        insert u; 
        uId = u.Id;
       }
       }
    }
        
    static testMethod void testDailySheduledJob(){
        string sch ='0 5 12 * * ?';
        Test.startTest();
        string jobjId = System.schedule('UpdateCPortalContacts', '0 0 14 * * ? *' , new UpdateCPortalContacts());
        list<user> userList = [select id from USER where id =: uId];
        integer ucheck=userList.size();
      //  system.asssetEquals(1,userList.size());
        Test.stopTest();
    }
}

Is it a best practise to use :
  • System.runAs(adminUser) or is it due to the limitation of how records are inserted. Since I am learning APEX I want to make sure If I should use it as rule of thumb (kind of boiler plate code) and always use in creating test classes.
  • system.asssetEquals();
Currenty I had commented it because it was throwing an error :  Method does not exist or incorrect signature: void asssetEquals(Integer, Integer) from the type System

Please suggest as why is it causing an issue or if there is a better way to do it.