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
The new LearnerThe new Learner 

how to convert list into map --urgent pls

Hi Experts,

I have below code which is calling before update on account ,i have nested loops because of that i am getting cpu time out issue, can anyone convert my code into MAp please.

        Set<Id> Accids= New Set<Id>();
        Set<String>Fun= new set<string>();
        
        Id AccRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('RecordType').getRecordTypeId();
        
        
        if(AccRecordTypeId !=null)
        {
        FOR(Account A : newList)
        { 
            
            if(A.RecordTypeId == AccRecordTypeId)
            {
                Accids.Add(A.id);
                system.debug('%%%%%%'+Accids);
            }    
            
        }
        
       } 
        String samp = System.Label.User_ID; 
        List<Function__c> PR = [Select Id,Salesforce_ID__c,Function_Def__c,Person__c,Pernr__c from Function__c where Salesforce_ID__c IN : Accids and Pernr__c !=null ];
        system.debug('$$$$$$'+PR );
        
        for(Function__c pf: PR)
        {
            if( pf.Salesforce_ID__c != Null)
            {
                system.debug('^^^^^^'+pf);
                Fun.add(pf.Pernr__c);
                system.debug('#######'+Fun);
            }
        }
        
        list<Person__c> gp = [ select id , Employee_ID__c,User__c,OwnerId from Person__c where Employee_ID__c in : Fun];
        system.debug('@@@@@'+gp );
        
        if(gp.size()>0)
        {
            
            FOR(Account A : newList)
                
            {
                
                for(Person__c gpr: gp)
                {
                    for( Function__c pfr: PR)
                    {
                        if(gpr.User__c!=null && pfr.Function_Def__c!=null )
                        {
                            
                            if(pfr.Function_Def__c== 'ABC' )
                            {
                                
                                system.debug('&&&&&'+gpr);
                                A.OwnerId=gpr.User__c;
                                system.debug('!!!!!'+A.ownerid);
                                
                            }
                            else if(pfr.Function_Def__c== 'DEF' || pfr.Function_Def__c== 'GHI' || pfr.Function_Def__c== 'JKL')
                            {
                                A.OwnerId=gpr.User__c;
                                system.debug('88888'+A.ownerid);
                                
                            }
                        }
                        else 
                        {
                            A.OwnerId=samp;
                            system.debug('***'+A.ownerid);
                        }
                    }
                }
            }
            
        }
Best Answer chosen by The new Learner
GrumpyDevGrumpyDev
Hi The new Learner,

Have fun : 
 
Map<Id, Account> accMap = new Map<Id, Account>();
Set<String> funSet = new set<string>();

Id AccRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('RecordType').getRecordTypeId();

if(AccRecordTypeId !=null){
	for(Account acc : newList){
		if(acc.RecordTypeId == AccRecordTypeId){
			accMap.put(acc.Id, acc);
		}
	}
}

String samp = System.Label.User_ID;
List<Function__c> funcLst = [Select Id,Salesforce_ID__c,Function_Def__c,Person__c,Pernr__c from Function__c where Salesforce_ID__c IN : accMap.keySet() and Pernr__c !=null ];

if(!funcLst.isEmpty()){
	for(Function__c func : funcLst){
		if(func.Salesforce_ID__c != Null){
			funSet.add(func.Pernr__c);
		}
	}
}

list<Person__c> personLst = [ select id , Employee_ID__c,User__c,OwnerId from Person__c where Employee_ID__c in : funSet];

if(!personLst.isEmpty()){
	for(Person__c prsn: personLst){
		for( Function__c func: funcLst){
			if(prsn.User__c != null && func.Function_Def__c != null){
				if(func.Function_Def__c== 'ABC' || func.Function_Def__c== 'DEF' || func.Function_Def__c== 'GHI' || func.Function_Def__c== 'JKL' ){ // I made it as one its doing the same...
					accmap.get(func.Salesforce_ID__c).OwnerId = prsn.User__c;
				}
			}else{
				accmap.get(func.Salesforce_ID__c).OwnerId = samp;
			}
		}
	}

}

Please choose best answer if your problem has been resolved,
GrumpyDev.

All Answers

GrumpyDevGrumpyDev
Hi The new Learner,

Have fun : 
 
Map<Id, Account> accMap = new Map<Id, Account>();
Set<String> funSet = new set<string>();

Id AccRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('RecordType').getRecordTypeId();

if(AccRecordTypeId !=null){
	for(Account acc : newList){
		if(acc.RecordTypeId == AccRecordTypeId){
			accMap.put(acc.Id, acc);
		}
	}
}

String samp = System.Label.User_ID;
List<Function__c> funcLst = [Select Id,Salesforce_ID__c,Function_Def__c,Person__c,Pernr__c from Function__c where Salesforce_ID__c IN : accMap.keySet() and Pernr__c !=null ];

if(!funcLst.isEmpty()){
	for(Function__c func : funcLst){
		if(func.Salesforce_ID__c != Null){
			funSet.add(func.Pernr__c);
		}
	}
}

list<Person__c> personLst = [ select id , Employee_ID__c,User__c,OwnerId from Person__c where Employee_ID__c in : funSet];

if(!personLst.isEmpty()){
	for(Person__c prsn: personLst){
		for( Function__c func: funcLst){
			if(prsn.User__c != null && func.Function_Def__c != null){
				if(func.Function_Def__c== 'ABC' || func.Function_Def__c== 'DEF' || func.Function_Def__c== 'GHI' || func.Function_Def__c== 'JKL' ){ // I made it as one its doing the same...
					accmap.get(func.Salesforce_ID__c).OwnerId = prsn.User__c;
				}
			}else{
				accmap.get(func.Salesforce_ID__c).OwnerId = samp;
			}
		}
	}

}

Please choose best answer if your problem has been resolved,
GrumpyDev.
This was selected as the best answer
The new LearnerThe new Learner
Hi Grumpy,

dont we get CPU time out issue again if anyone do an data load we wrote two for loops here that is the reason
GrumpyDevGrumpyDev
Hi The new Learner,

I dont know what happens before and after the snippet you added.
But if nothing crazy goes before or after this should work even when using Data Loader.

If you will get a CPU time out after replacing your code with mine I can only guess this is not where it fails.

GrumpyDev.