You need to sign in to do that
Don't have an account?
Aakash Saluja 7
trigger to create multiple records based on the single field update
Hi,
i am new to salesforce and i have a scenario where i have an object Search_Filter__c where i want to insert records based on the account.
if any account has 10 address it should create multiple records with the unique city value populated in that in the Search_Filter__c. like if there are two cties with the same name, it should create 1 record else two
i have written the code as:
trigger RcordInsert on Account (after insert, after update) {
map<id,account> mapAcc= new map<id,account>();
set<string> cities = new set<string>();
for(account acc: trigger.new){
if(acc.KOL_Set_Search_flag__c != null){
mapAcc.put(acc.id,acc);
system.debug('id is'+mapAcc);
}
}
list<account> listSearch = new list<account>([ SELECT name,
KOL_Set_Search_flag__c,
Middle_vod__c,
LastName,
(select City_vod__c,State_vod__c,Country_olr__c,Zip_vod__c from Address_vod__r )
FROM account
WHERE id in : mapAcc.keyset()
]);
list<Search_Filter__c> searchUpdateList = new list<GBI_Search_Filter__c>();
for(account c : listSearch ){
if(c.KOL_Set_Search_flag__c == true){
KOL_GBI_Search_Filter__c searchFilter = new KOL_GBI_Search_Filter__c();
searchFilter.KOL_FIRSTNAME__c = c.name;
searchFilter.KOL_ID__c = c.id;
searchFilter.KOL_MIDDLENAME__c = c.Middle_vod__c;
searchFilter.KOL_LASTNAME__c = c.LastName;
// For(Integer I = 0; I < c.Address_vod__r.size(); I++){
for(Address_vod__c addr : c.Address_vod__r ){
searchFilter.KOL_CITY__c = addr.City_vod__c;
searchFilter.KOL_STATE__c = addr.State_vod__c;
searchFilter.KOL_POSTAL_ZIP_CODE__c = addr.Zip_vod__c;
searchFilter.KOL_COUNTRY__c = addr.Country_olr__c;
}
searchUpdateList.add(searchFilter);
}
}
insert searchUpdateList;
}
Thanks in advance
i am new to salesforce and i have a scenario where i have an object Search_Filter__c where i want to insert records based on the account.
if any account has 10 address it should create multiple records with the unique city value populated in that in the Search_Filter__c. like if there are two cties with the same name, it should create 1 record else two
i have written the code as:
trigger RcordInsert on Account (after insert, after update) {
map<id,account> mapAcc= new map<id,account>();
set<string> cities = new set<string>();
for(account acc: trigger.new){
if(acc.KOL_Set_Search_flag__c != null){
mapAcc.put(acc.id,acc);
system.debug('id is'+mapAcc);
}
}
list<account> listSearch = new list<account>([ SELECT name,
KOL_Set_Search_flag__c,
Middle_vod__c,
LastName,
(select City_vod__c,State_vod__c,Country_olr__c,Zip_vod__c from Address_vod__r )
FROM account
WHERE id in : mapAcc.keyset()
]);
list<Search_Filter__c> searchUpdateList = new list<GBI_Search_Filter__c>();
for(account c : listSearch ){
if(c.KOL_Set_Search_flag__c == true){
KOL_GBI_Search_Filter__c searchFilter = new KOL_GBI_Search_Filter__c();
searchFilter.KOL_FIRSTNAME__c = c.name;
searchFilter.KOL_ID__c = c.id;
searchFilter.KOL_MIDDLENAME__c = c.Middle_vod__c;
searchFilter.KOL_LASTNAME__c = c.LastName;
// For(Integer I = 0; I < c.Address_vod__r.size(); I++){
for(Address_vod__c addr : c.Address_vod__r ){
searchFilter.KOL_CITY__c = addr.City_vod__c;
searchFilter.KOL_STATE__c = addr.State_vod__c;
searchFilter.KOL_POSTAL_ZIP_CODE__c = addr.Zip_vod__c;
searchFilter.KOL_COUNTRY__c = addr.Country_olr__c;
}
searchUpdateList.add(searchFilter);
}
}
insert searchUpdateList;
}
Thanks in advance
All Answers
Example ,if there are three address with the cities as mumbai, Delhi and mumbai, then it should create only two records 1 for each mumbai and delhi
I have done the changes but currently i am getting case senstive values also in the set. is there any way we can remove that.
trigger RcordInsertAakash on Account (after insert, after update) {
map<id,account> mapAcc= new map<id,account>();
set<Address_vod__c> cities = new set<Address_vod__c>([select City_vod__c from Address_vod__c where id in: mapAcc.keyset() ]);
set<String> citiesSet = new set<String>();
List<String> citiesuni = new List<String>();
Boolean addFnd = false;
for(account acc: trigger.new){
if(acc.KOL_Set_Search_flag__c != null){
mapAcc.put(acc.id,acc);
system.debug('id is'+mapAcc);
}
}
list<account> listSearch = new list<account>([ SELECT name,
firstname,
KOL_Set_Search_flag__c,
Middle_vod__c,
LastName,
(select City_vod__c,State_vod__c,Country_olr__c,Zip_vod__c from Address_vod__r )
FROM account
WHERE id in : mapAcc.keyset()
]);
list<Search_Filter__c> searchUpdateList = new list<Search_Filter__c>();
for(account c : listSearch ){
if(c.KOL_Set_Search_flag__c == true){
for(Address_vod__c addr : c.Address_vod__r ){
citiesSet.add(addr.City_vod__c);
system.debug('citiesSet'+citiesSet);
}
List<String> listStrings = new List<String>(citiesSet);
for(String st : listStrings){
addFnd = false;
for(Address_vod__c addr : c.Address_vod__r ){
//for(integer i=0; i < addr.size(); i++){
if(st.Equals(addr.City_vod__c)){
if(addFnd == false){
addFnd = True;
Search_Filter__c searchFilter = new Search_Filter__c();
searchFilter.KOL_FIRSTNAME__c = c.firstname;
searchFilter.KOL_ID__c = c.id;
searchFilter.KOL_MIDDLENAME__c = c.Middle_vod__c;
searchFilter.KOL_LASTNAME__c = c.LastName;
// For(Integer I = 0; I < c.Address_vod__r.size(); I++){
// for(Address_vod__c addr : c.Address_vod__r ){
searchFilter.KOL_CITY__c = st;
searchFilter.KOL_STATE__c = addr.State_vod__c;
searchFilter.KOL_POSTAL_ZIP_CODE__c = addr.Zip_vod__c;
searchFilter.KOL_COUNTRY__c = addr.Country_olr__c;
searchUpdateList.add(searchFilter);
break;
}
}
//}
}
}
}
}
insert searchUpdateList;
}
you can do is, just before adding City in Set, lowercase or Uppercase it so that it can be identified, I hope it will solve your problem.
Thanks
Can you please tell us here that how u solved the problem or if the problem is solved by above answers then please mark it as a best answer
Thanks
Saket Sharma