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
Andrew TelfordAndrew Telford 

Dynamically choose field to Update

I am attempting to create a trigger that will update certain fields if the value in a multipicklist is present.

Rather than have 20 If statements to check if the value is in it, I want to be able to something like 

IF taskListValue is in picklistValue
THEN fieldToUpdate = fieldListValue.indexof( taskListValue.indexof( picklistValue )

It would then say something like ...

thisRecord.fieldToUpdate = TRUE

updateObject.add(thisRecord);

What I currently have is
 
//-- Some Varaibles to start with

STRING strRelated = thisTask.Campaign_Related__c;
STRING[] strRelated_split = strRelated.split(';');

LIST<STRING> lstOfTasks = NEW LIST<STRING>(); //-- Stores the values that can be in the multipicklist thisTask.Campaign_Related__c
LIST<STRING> lstOfFields = NEW LIST<STRING>(); //-- Stores the field names that we want to update if the values is passed. These are in the same sequence so getting the index of one will refer to the index of the other.

//-- Add some values to our lists
lstOfTasks.add('EDU - Business Insurance Session');
lstOfFields.add('Business_Insurance_Session_Completed__c');
lstOfTasks.add('EDU - CBA/CFP Branch Training Session');
lstOfFields.add('CBA_CFP_Branch_Training_Session_Complete__c');
lstOfTasks.add('EDU - Client Seminars');
lstOfFields.add('Client_Seminars_Completed__c');


FOR(intCount = 0; intCount < strRelated_split.size() ; intCount++)
{
    IF( strReleated_split[intCount] is in lstOfTasks)
    {
        intX = lstOfTasks.indexOf( strReleated_split[intCount] );
        theField = lstOfFields[intX]
        thisAMP.thisField = TRUE;
    }
}
Any help would be appreciated.
 
Best Answer chosen by Andrew Telford
Rakesh Kumar SainiRakesh Kumar Saini
Andrew, use below code instead of 
thisAMP.put(tasksToFieldMap.get( campaignRelated ), TRUE);
 
thisAMP.tasksToFieldMap.get( campaignRelated ) = TRUE;

All Answers

Rakesh Kumar SainiRakesh Kumar Saini
Hi Andrew,

Please have a look on below code and let me know if you are looking for the same 
//-- Some Varaibles to start with

STRING strRelated = thisTask.Campaign_Related__c;
STRING[] strRelated_split = strRelated.split(';');

Map<String, String> tasksToFieldMap = new Map<String, String>(); //-Stores Key-value pair of strRelated_split string to custom field name to be set true. We don't need to be worry about sequesnce in case of map :)


//-- Add some values to our map
tasksToFieldMap.put('EDU - Business Insurance Session', 'Business_Insurance_Session_Completed__c');
tasksToFieldMap.put('EDU - CBA/CFP Branch Training Session', 'CBA_CFP_Branch_Training_Session_Complete__c');
tasksToFieldMap.put('EDU - Client Seminars', 'Client_Seminars_Completed__c');
 
for(String campaianRelated : strRelated_split)
{
	if(tasksToFieldMap.containsKey(campaianRelated))
	{
		thisAMP.tasksToFieldMap.get(campaianRelated) = true;	
	}
}
Please mark this as answer it fullfills your requiremnent.

Thanks,
Rakesh Kumar Saini
 
Andrew TelfordAndrew Telford
Thanks for your response Rakesh

Unfortunately I am getting the following error
 
Invalid field taskstofieldmap for SObject CI_Adviser_Management_Plan__c

This is the current code
 
STRING strRelated = thisTask.Campaign_Related__c;
STRING[] strRelated_split = strRelated.split(';');
                    
MAP<STRING, STRING> tasksToFieldMap = NEW MAP<STRING, STRING>();	//--	Stores Key Value Pairs
LIST<STRING> lstOfTasks = NEW LIST<STRING>();
LIST<STRING> lstOfFields = NEW LIST<STRING>();

tasksToFieldMap.put('EDU - Business Insurance Session', 'Business_Insurance_Session_Completed__c');
tasksToFieldMap.put('EDU - CBA/CFP Branch Training Session', 'CBA_CFP_Branch_Training_Session_Complete__c');
tasksToFieldMap.put('EDU - Client Seminars', 'Client_Seminars_Completed__c');
tasksToFieldMap.put('EDU - Induction - CFP', 'Induction_CFP_Completed__c');

LIST<CI_ADVISER_MANAGEMENT_PLAN__C> ampsToUpdate = [SELECT Id FROM CI_ADVISER_MANAGEMENT_PLAN__C WHERE contact__c IN :contactIDs AND Time_Period__c = :ampTimePeriod];
SYSTEM.debug( thisPage + 'amps to update: ' + ampsToUpdate.size());
SYSTEM.debug( thisPage + 'timeperiod: ' + ampTimePeriod);

//--    Check the Related Campaign
FOR( CI_ADVISER_MANAGEMENT_PLAN__C thisAMP : ampsToUpdate )
{
    for(String campaignRelated : strRelated_split)
    {
        if( tasksToFieldMap.containsKey( campaignRelated ) )
        {
            //-- EROR Message HERE
            thisAMP.tasksToFieldMap.get( campaignRelated ) = TRUE;
            //-- EROR Message HERE
        }
    }
}



 
Rakesh Kumar SainiRakesh Kumar Saini
Andrew, use below code instead of 
thisAMP.put(tasksToFieldMap.get( campaignRelated ), TRUE);
 
thisAMP.tasksToFieldMap.get( campaignRelated ) = TRUE;
This was selected as the best answer
Andrew TelfordAndrew Telford
Thanks Rakesh

The final code looks like this for those that might come after ...
 
//-- Some Varaibles to start with

STRING strRelated = thisTask.Campaign_Related__c;
STRING[] strRelated_split = strRelated.split(';');

Map<String, String> tasksToFieldMap = new Map<String, String>(); //-Stores Key-value pair of strRelated_split string to custom field name to be set true. We don't need to be worry about sequesnce in case of map :)


//-- Add some values to our map
tasksToFieldMap.put('EDU - Business Insurance Session', 'Business_Insurance_Session_Completed__c');
tasksToFieldMap.put('EDU - CBA/CFP Branch Training Session', 'CBA_CFP_Branch_Training_Session_Complete__c');
tasksToFieldMap.put('EDU - Client Seminars', 'Client_Seminars_Completed__c');
 
for(String campaianRelated : strRelated_split)
{
	if(tasksToFieldMap.containsKey(campaianRelated))
	{
		thisAMP.put(tasksToFieldMap.get(campaianRelated), true);
	}
}