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
raseshtcsraseshtcs 

Save error in class @future

Public class utility{
    @future
    public static void updatehmt(List<customobject__c> colist){
        Update colist;
    }
}

 When I try to save the above class it doesnt save and gives error Unsupported parameter type LIST<customobject__c> at line 5 column 24

If I change the parameter to a set of ids the class gets saved. Any idea what am I doing wrong??

Best Answer chosen by Admin (Salesforce Developers) 
soofsoof

Here's what you need to do. Inside your userUpdate method, create the following List:

List<String> hmtFieldValues = new List<String>();

 

Replace the following line in your code

HMT_Intermediate__c hmtinter = new HMT_Intermediate__c(id = hi.Id,Region__c = regionName,Profile__c=userProfile.get(hi.CLM_Name__c));

 

with the following one:

hmtFieldValues.add(hi.Id + '~' + regionName + '~' + userProfile.get(hi.CLM_Name__c));

 

 

Then where you call your future method, pass the hmtFieldValues List to it as argument.

updatehmt(hmtFieldValues);

 

The code for the future method is below:

@future
public static void updatehmt(List<String> hmtFieldValues) {
	List<HMT_Intermediate__c> hmtis = new List<HMT_Intermediate__c>();
	for ( String hmtVals : hmtFieldValues ) {
		String fieldVals = hmtVals.split('~');
		HMT_Intermediate__c hmti = 
			new HMT_Intermediate__c(
				Id = fieldVals[0],
				Region__c = fieldVals.size() > 1 ? fieldVals[0] : null,
				Profile__c = fieldVals.size() > 2 ? fieldVals[1] : null);
		hmtis.add(hmti)
	}
	update hmtis;
}

 

I've used a tilde (~) as separator, but oviously, you can replace it with something else if required.  Also, I haven't executed this code, so there might be some minor errors.

 

Thanks.

All Answers

Navatar_DbSupNavatar_DbSup

Hi,
Try the below code as reference for using @future:

 

public class UpdateNonSetupObjects
{
@future
public static void updateContact(list<String> usercontactid )
{
CustomObject__c[] updateimp = [SELECT id from CustomObject __c
Database.update(updateimp);
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

soofsoof

The parameters for a future method must be primitive dataypes, arrays of primitive datatypes, or collections of primitive datatypes.  That's the issue with your method.

 

If you could send a few details around what you're trying to accomplish, I might be able to come up with a work-around.

 

Thanks.

raseshtcsraseshtcs

On update of user object I want to update an entry in a custom object which is correspoding to the user.What I am doing is creating the list of the custom object which i need to update. Then call the future method to do the actual update. This i am doing to prevent the mixed dml operation error.But the list becomes empty inside the future method.

 

Below is the code

Public class utility{

    List<HMT_Intermediate__c> HMTIntercountry = new List<HMT_Intermediate__c>();  
    Public Static List<HMT_Intermediate__c> HMTInterregion = new List<HMT_Intermediate__c>();
    List<HMT_Intermediate__c> HMTNew = new List<HMT_Intermediate__c>();
    
    List<string> set_profile = new list<String>{'GCG Director-VP','GCG Client Manager','MSA GCG Client Manager','MSA GCG Director-VP','National Client Manager','National Director-VP','Regional Client Manager','Regional Director-VP','District Client Manager','District Director-VP','District Coordinator','Regional/District Leadership','MSA GCG Leadership','MSA GCG Coordinator','National Coordinator','Regional Coordinator','National Leadership','Business Team Support User', 'Canada CLM District Coordinator', 'CLM Business Team Support User', 'CLM Field Support & Initiaves','CLM Field Support & Strat Dev', 'Client Mgmt Initiative User', 'Client Mgmt Marketing User', 'Virtual Client Manager- Tier 4'};
    Map<Id,Profile> pro_Id = new Map<Id,Profile>([Select ID,Name from Profile where Name IN : set_profile]); 
    
    Map<Id,User> countryMap = new Map<Id,User>();
    Map<Id,User> regionMap = new Map<Id,User>();
    Map<Id,String> userProfile = new Map<Id,String>();
    
    Set<String> usa = new Set<String>{'Client Mgmt Initiative User','Client Mgmt Marketing User','CLM Business Team Support User','CLM Field Support & Initiaves','CLM Field Support & Strat Dev','District Client Manager','District Coordinator','District Director-VP','GCG ASA/Coordinator','GCG Client Manager','GCG Director-VP','GCG Leadership','National Client Manager','National Coordinator','National Director-VP','National Leadership','Regional Client Manager','Regional/District Leadership','System Administrator-GCG','Virtual Client Manager- Tier 4'};
    Set<String> usaLAC = new Set<String>{'MSA GCG Client Manager','MSA GCG Coordinator','MSA GCG Director-VP','MSA GCG Leadership'};
    Set<String> emea = new Set<String>{'~INTL-EMEA CLM User','~INTL-EMEA CLM/GCG User','~INTL-EMEA MA/CLM User'};
    Set<String> emeaJapa = new Set<String>{'~INTL MA/CLM User','~INTL-CLM User','~INTL-CLM-Global Client Mgr'};
    Set<String> italy = new Set<String>{'~INTL-ITALY CLM user','~INTL-LAC CLM User'};
    
    Public void userUpdate(List<User> newTrigger,Map<Id,User> oldTriggermap,Boolean ifInsert,Boolean ifUpdate){
        if(ifUpdate){
            for(User u : newTrigger){
                //Taking trigger.old to compare the old values
                User uold = oldTriggermap.get(u.id);
                system.debug('debug id'+u.Profile.Name+'----'+uold.Profile.Name);
                if(pro_Id.containsKey(u.ProfileId)){            
                    //check if there is a change in the country of the user
                    if(u.country != uold.country){
                        countryMap.put(u.Id,u);
                    }
                    if(u.profileId != uold.profileId){
                        regionMap.put(u.Id,u);
                    }
               }
            }
            system.debug('regionMap'+regionMap);
            if(regionMap!=null && regionMap.size()>0){
                for(User u : [Select Id,Profile.Name from User where Id IN :regionMap.keyset()]){
                    userProfile.put(u.Id,u.Profile.Name);
                }
            }
            system.debug('userProfile'+userProfile);        
            if(countryMap != null && countryMap.size() > 0){
                for(HMT_Intermediate__c hi : [Select Id,Country__c,CLM_Name__c from HMT_Intermediate__c where CLM_Name__c IN :countryMap.keyset()]){
                    if(countryMap.get(hi.CLM_Name__c) != null){
                        hi.Country__c = countryMap.get(hi.CLM_Name__c).Country;
                    }
                    HMTIntercountry.add(hi);
                }
            }
            if(regionMap != null && regionMap.size() > 0){        
                for(HMT_Intermediate__c hi : [Select Id,Country__c,CLM_Name__c,Region__c from HMT_Intermediate__c where CLM_Name__c IN :regionMap.keyset()]){
                    String regionName;
                    if(regionMap.get(hi.CLM_Name__c) != null){
                        if(usa.contains(userProfile.get(hi.CLM_Name__c))){
                            regionName = 'USA';
                        }else if(usaLAC.contains(userProfile.get(hi.CLM_Name__c))){
                            regionName = 'USA/LAC';
                        }else if(emea.contains(userProfile.get(hi.CLM_Name__c))){
                            regionName = 'EMEA';
                        }else if(emeaJapa.contains(userProfile.get(hi.CLM_Name__c))){
                            regionName = 'EMEA/JAPA';            
                        }else if(italy.contains(userProfile.get(hi.CLM_Name__c))){
                            regionName = 'Italy';
                        }else if(userProfile.get(hi.CLM_Name__c) == 'Canada CLM District Coordinator'){
                            regionName = 'CA';            
                        }else if(userProfile.get(hi.CLM_Name__c) == 'JAPA CLM MA Telesales ~INTL'){
                            regionName = 'JAPA';
                        }    
                        hi.Region__c = regionName;
                    }
                    HMT_Intermediate__c hmtinter = new HMT_Intermediate__c(id = hi.Id,Region__c = regionName,Profile__c=userProfile.get(hi.CLM_Name__c));
                    HMTInterregion.add(hmtinter);
                }
            }
            try{
                if(HMTIntercountry != null && HMTIntercountry.size()>0){
                    Update HMTIntercountry;
                }
                system.debug('HMTInterregion'+HMTInterregion);
                if(HMTInterregion != null && HMTInterregion.size()>0){
                        updatehmt();
                }
            }
            catch(exception e){
                system.debug('HMT Update Exception'+e);
            }            
        }
        else if(ifInsert){
            for(User u : newTrigger){
                if(pro_Id.containsKey(u.Profile.Id)){
                    HMT_Intermediate__c hi = new HMT_Intermediate__c(
                                                                    CLM_Name__c = u.Id,
                                                                    Region__c = u.Profile.Name,
                                                                    Country__c = u.Country
                                                                    );
                    HMTNew.add(hi);                                                                
                }
            }
            if(HMTNew.size()>0){
                //Insert HMTNew;
            }        
        }
    }
    
    @future
    public static void updatehmt(){
        system.debug('common1234future'+HMTInterregion);
        Update HMTInterregion;
    }
}

Thanks in advance

 

soofsoof

Here's what you need to do. Inside your userUpdate method, create the following List:

List<String> hmtFieldValues = new List<String>();

 

Replace the following line in your code

HMT_Intermediate__c hmtinter = new HMT_Intermediate__c(id = hi.Id,Region__c = regionName,Profile__c=userProfile.get(hi.CLM_Name__c));

 

with the following one:

hmtFieldValues.add(hi.Id + '~' + regionName + '~' + userProfile.get(hi.CLM_Name__c));

 

 

Then where you call your future method, pass the hmtFieldValues List to it as argument.

updatehmt(hmtFieldValues);

 

The code for the future method is below:

@future
public static void updatehmt(List<String> hmtFieldValues) {
	List<HMT_Intermediate__c> hmtis = new List<HMT_Intermediate__c>();
	for ( String hmtVals : hmtFieldValues ) {
		String fieldVals = hmtVals.split('~');
		HMT_Intermediate__c hmti = 
			new HMT_Intermediate__c(
				Id = fieldVals[0],
				Region__c = fieldVals.size() > 1 ? fieldVals[0] : null,
				Profile__c = fieldVals.size() > 2 ? fieldVals[1] : null);
		hmtis.add(hmti)
	}
	update hmtis;
}

 

I've used a tilde (~) as separator, but oviously, you can replace it with something else if required.  Also, I haven't executed this code, so there might be some minor errors.

 

Thanks.

This was selected as the best answer
raseshtcsraseshtcs

Worked like magic.. here is the code which worked after small errors!!! Changed fieldVals from String to List of string

@future
public static void updatehmt(List<String> hmtFieldValues) {
	List<HMT_Intermediate__c> hmtis = new List<HMT_Intermediate__c>();
	for ( String hmtVals : hmtFieldValues ) {
		List<String> fieldVals = hmtVals.split('~');
		HMT_Intermediate__c hmti = 
			new HMT_Intermediate__c(
				Id = fieldVals[0],
				Region__c = fieldVals.size() > 1 ? fieldVals[0] : null,
				Profile__c = fieldVals.size() > 2 ? fieldVals[1] : null);
		hmtis.add(hmti)
	}
	update hmtis;
}