+ Start a Discussion
SFDC DummySFDC Dummy 

Data saved In respective Series.....

Hi all

Thanks In Advc..


I am facing small bit of problem like want to data catagory in the form of series like 
aaa,bbb,ccc,ddd---Series1
eee,fff,ggg,iii-----Series2......Like this

i have one object having some field .when i am entering value with field aaa and click on save it will be saved in series1 catagory means data will be saved with respective series based on value..
Is there any way to solve this 
Best Answer chosen by SFDC Dummy
Jigar.LakhaniJigar.Lakhani

Hello,

You need to auto populate category field based on one field of record which is going to be inserted/updated.
You can create a apex trigger on inserted/updated object. Lets suppose you have account object. Trigger and TriggerHandler class are below.

Need to update coode for your object and field api names, since here I have used "Account" object and "YOUR_FIELD__C" field.

Apex Trigger
Trigger AccountTrigger On Account(BEFORE INSERT,BEFORE UPDATE) {
	
	// BEFORE INSERT TRIGGER
	if (Trigger.IsBefore && Trigger.IsInsert) {
		
		List<Account> listAccount = new List<Account>();
		for (Account objAccount:Trigger.New) {
			if (!String.IsBlank(objAccount.YOUR_FIELD__C)) {
				listAccount.Add(objAccount);
			}
		}
		if (listAccount != null && listAccount.size() > 0) {
			AccountTriggerHandler.autoAssignCategory(listAccount);
		}
	}
	
	// BEFORE UPDATE TRIGGER
	if (Trigger.IsBefore && Trigger.IsUpdate) {
		
		List<Account> listAccount = new List<Account>();
		for (Account objAccount:Trigger.New) {
			if (objAccount.YOUR_FIELD__C != Trigger.OldMap.get(objAccount.Id).YOUR_FIELD__C) {
				listAccount.Add(objAccount);
			}
		}
		if (listAccount != null && listAccount.size() > 0) {
			AccountTriggerHandler.autoAssignCategory(listAccount);
		}
	}
	
}

Apex Class
 
public class AccountTriggerHandler{
	
	public static void autoAssignCategory(List<Account> listAccount){
		
		Map<String,Set<String> mapCategoryWithData = new Map<String,Set<String>>();
		
		// YOU CAN ASSIGN YOUR DATA TO THAT MAP, MAP EXIST FOR CATEGORY WITH ALL ITS VALUE
		// FOR EXAPMPLE I HAVE ADDED DATA FOR "SERIES 1" and "SERIES 2" CATEGORY
		
		// Assign Series 1 data
		Set<String> setSeries1Data = new Set<String>();
		setSeries1Data.Add('aaaaa');
		setSeries1Data.Add('bbbbb');
		setSeries1Data.Add('ccccc');
		mapCategoryWithData.put('SERIES 1',setSeries1Data);
		
		// Assign Series 2 data
		Set<String> setSeries2Data = new Set<String>();
		setSeries2Data.Add('ddddd');
		setSeries2Data.Add('eeeee');
		setSeries2Data.Add('fffff');
		mapCategoryWithData.put('SERIES 2',setSeries2Data);
		
		for (Account objAccount:listAccount) {
			if (mapCategoryWithData != null && mapCategoryWithData.size() > 0) {
				for (String strSeries:mapCategoryWithData.KeySet()) {
					Set<String> setSeriesData = mapCategoryWithData.Get(strSeries);
					if (setSeriesData.Contains(objAccount.YOUR_FIELD__C)) {
						objAccount.CATEGORY__C = strSeries;
						Break;
					}
				}
			}
		}
		
	}
	
}


Thanks & Cheers,
Jigar(pateljb90@gmail.com)

All Answers

Jigar.LakhaniJigar.Lakhani

Hello,

You need to auto populate category field based on one field of record which is going to be inserted/updated.
You can create a apex trigger on inserted/updated object. Lets suppose you have account object. Trigger and TriggerHandler class are below.

Need to update coode for your object and field api names, since here I have used "Account" object and "YOUR_FIELD__C" field.

Apex Trigger
Trigger AccountTrigger On Account(BEFORE INSERT,BEFORE UPDATE) {
	
	// BEFORE INSERT TRIGGER
	if (Trigger.IsBefore && Trigger.IsInsert) {
		
		List<Account> listAccount = new List<Account>();
		for (Account objAccount:Trigger.New) {
			if (!String.IsBlank(objAccount.YOUR_FIELD__C)) {
				listAccount.Add(objAccount);
			}
		}
		if (listAccount != null && listAccount.size() > 0) {
			AccountTriggerHandler.autoAssignCategory(listAccount);
		}
	}
	
	// BEFORE UPDATE TRIGGER
	if (Trigger.IsBefore && Trigger.IsUpdate) {
		
		List<Account> listAccount = new List<Account>();
		for (Account objAccount:Trigger.New) {
			if (objAccount.YOUR_FIELD__C != Trigger.OldMap.get(objAccount.Id).YOUR_FIELD__C) {
				listAccount.Add(objAccount);
			}
		}
		if (listAccount != null && listAccount.size() > 0) {
			AccountTriggerHandler.autoAssignCategory(listAccount);
		}
	}
	
}

Apex Class
 
public class AccountTriggerHandler{
	
	public static void autoAssignCategory(List<Account> listAccount){
		
		Map<String,Set<String> mapCategoryWithData = new Map<String,Set<String>>();
		
		// YOU CAN ASSIGN YOUR DATA TO THAT MAP, MAP EXIST FOR CATEGORY WITH ALL ITS VALUE
		// FOR EXAPMPLE I HAVE ADDED DATA FOR "SERIES 1" and "SERIES 2" CATEGORY
		
		// Assign Series 1 data
		Set<String> setSeries1Data = new Set<String>();
		setSeries1Data.Add('aaaaa');
		setSeries1Data.Add('bbbbb');
		setSeries1Data.Add('ccccc');
		mapCategoryWithData.put('SERIES 1',setSeries1Data);
		
		// Assign Series 2 data
		Set<String> setSeries2Data = new Set<String>();
		setSeries2Data.Add('ddddd');
		setSeries2Data.Add('eeeee');
		setSeries2Data.Add('fffff');
		mapCategoryWithData.put('SERIES 2',setSeries2Data);
		
		for (Account objAccount:listAccount) {
			if (mapCategoryWithData != null && mapCategoryWithData.size() > 0) {
				for (String strSeries:mapCategoryWithData.KeySet()) {
					Set<String> setSeriesData = mapCategoryWithData.Get(strSeries);
					if (setSeriesData.Contains(objAccount.YOUR_FIELD__C)) {
						objAccount.CATEGORY__C = strSeries;
						Break;
					}
				}
			}
		}
		
	}
	
}


Thanks & Cheers,
Jigar(pateljb90@gmail.com)
This was selected as the best answer
SFDC DummySFDC Dummy
client need to customize series and category as per requirement.so how its possible
Jigar.LakhaniJigar.Lakhani

Hello,

Ok, So for that you can create custom settings. Hope you aware with custom settings and create custom setting records.
Create custom setting like "CategoryDetails" with one custom field for series data like "Series Data"(Text Field)

And client can enter Category name as name field of that custom setting record, as well comma separated data in "Series Data" field.

Sample Records

(1) Category: SERIES 1, Series Data: ,aaaaa,bbbbb,ccccc,ddddd,
(2) Category: SERIES 2, Series Dat: ,eeeee,fffff,ggggg,hhhhh,

In Series data field, we must need to add ","(comma) at first and last character to exact matching in apex code.

Now we are using custom setting records, no need to prepare hardcode map in apex code.
In that case apex class will be like below.
 

public class AccountTriggerHandler{
	
	public static void autoAssignCategory(List<Account> listAccount){
		
		Map<String, CategoryDetails__c> mapCategoryDetails = CategoryDetails__c.getAll();
		
		for (Account objAccount:listAccount) {
			if (mapCategoryDetails != null && mapCategoryDetails.size() > 0) {
				for (String strCategory:mapCategoryDetails.KeySet()) {
					String strSeriesData = mapCategoryDetails.Get(strCategory).Series_Data__c;
					if (strSeriesData.Contains(',' + objAccount.YOUR_FIELD__C + ',')) {
						objAccount.CATEGORY__C = strCategory;
						Break;
					}
				}
			}
		}
		
	}
	
}

Thanks & Cheers,
Jigar(pateljb90@gmail.com)

 
SFDC DummySFDC Dummy
I have created a text field for Series_Data__c which is 255 size limit.but i want more number .then how can solve this
Jigar.LakhaniJigar.Lakhani

Hello,

The only way is that you need to create more text fields, and concate those fields in apex code before comparing it with category value
Below are the details.

Suppose you have created one more text field in custom settings "Series Data 1(Series_Data_1__c)"
 

Sample Records
(1) Category: SERIES 1, Series Data: ,aaaaa,bbbbb,ccccc,ddddd, , Series Data 1: ,1111,2222,3333,
(2) Category: SERIES 2, Series Dat: ,eeeee,fffff,ggggg,hhhhh, , Series Data 1: ,4444,5555,6666,

Below is apex code updates.
 

public class AccountTriggerHandler{
	
	public static void autoAssignCategory(List<Account> listAccount){
		
		Map<String, CategoryDetails__c> mapCategoryDetails = CategoryDetails__c.getAll();
		
		for (Account objAccount:listAccount) {
			if (mapCategoryDetails != null && mapCategoryDetails.size() > 0) {
				for (String strCategory:mapCategoryDetails.KeySet()) {
					
					String strSeriesData = '';
					
					// Assign series data details
					if (!String.IsBlank(mapCategoryDetails.Get(strCategory).Series_Data__c)) {
						strSeriesData += mapCategoryDetails.Get(strCategory).Series_Data__c;
					}
					
					// Assing more series data details from "Series_Data_1__c"
					if (!String.IsBlank(mapCategoryDetails.Get(strCategory).Series_Data_1__c)) {
						strSeriesData += mapCategoryDetails.Get(strCategory).Series_Data_1__c;
					}
					
					// Same way here you can create more fields and concate those field data here
					
					if (strSeriesData.Contains(',' + objAccount.YOUR_FIELD__C + ',')) {
						objAccount.CATEGORY__C = strCategory;
						Break;
					}
				}
			}
		}
		
	}
	
}

Thanks & Cheers,
Jigar(pateljb90@gmail.com)
SFDC DummySFDC Dummy
How to write test class for this code
public class AccountTriggerHandler{
	
	public static void autoAssignCategory(List<Account> listAccount){
		
		Map<String, CategoryDetails__c> mapCategoryDetails = CategoryDetails__c.getAll();
		
		for (Account objAccount:listAccount) {
			if (mapCategoryDetails != null && mapCategoryDetails.size() > 0) {
				for (String strCategory:mapCategoryDetails.KeySet()) {
					
					String strSeriesData = '';
					
					// Assign series data details
					if (!String.IsBlank(mapCategoryDetails.Get(strCategory).Series_Data__c)) {
						strSeriesData += mapCategoryDetails.Get(strCategory).Series_Data__c;
					}
					
					// Assing more series data details from "Series_Data_1__c"
					if (!String.IsBlank(mapCategoryDetails.Get(strCategory).Series_Data_1__c)) {
						strSeriesData += mapCategoryDetails.Get(strCategory).Series_Data_1__c;
					}
					
					// Same way here you can create more fields and concate those field data here
					
					if (strSeriesData.Contains(',' + objAccount.YOUR_FIELD__C + ',')) {
						objAccount.CATEGORY__C = strCategory;
						Break;
					}
				}
			}
		}
		
	}
	
}
 
Trigger AccountTrigger On Account(BEFORE INSERT,BEFORE UPDATE) {
	
	// BEFORE INSERT TRIGGER
	if (Trigger.IsBefore && Trigger.IsInsert) {
		
		List<Account> listAccount = new List<Account>();
		for (Account objAccount:Trigger.New) {
			if (!String.IsBlank(objAccount.YOUR_FIELD__C)) {
				listAccount.Add(objAccount);
			}
		}
		if (listAccount != null && listAccount.size() > 0) {
			AccountTriggerHandler.autoAssignCategory(listAccount);
		}
	}
	
	// BEFORE UPDATE TRIGGER
	if (Trigger.IsBefore && Trigger.IsUpdate) {
		
		List<Account> listAccount = new List<Account>();
		for (Account objAccount:Trigger.New) {
			if (objAccount.YOUR_FIELD__C != Trigger.OldMap.get(objAccount.Id).YOUR_FIELD__C) {
				listAccount.Add(objAccount);
			}
		}
		if (listAccount != null && listAccount.size() > 0) {
			AccountTriggerHandler.autoAssignCategory(listAccount);
		}
	}
	
}