Activity › Forums › Salesforce® Discussions › How can we separate the ID’s based on the record type?
Tagged: Record ID, Record Type, Salesforce Records
-
How can we separate the ID’s based on the record type?
Posted by Mohit on August 10, 2016 at 2:49 PMHi All,
How can we separate the ID’s based on the record type?
I have all account id’s in set object, so now I have to filter those id’s based on recordtype.
Please give Suggestion
PRANAV replied 8 years, 3 months ago 3 Members · 2 Replies -
2 Replies
-
Hey Mohit,
you will have to create number of sets equal to the record type that you have for eg: if there are two record types then create 2 sets and add values in the set on the basis of record types.
map<id,String>mapidvsRecTypeId = new map<id,String>();
for (Account acc : [select id , recordTypeId from Account]){
mapidvsRecTypeId.put(acc.Id, acc.recordTypeId);
}
set<Id>set1 = new set<Id>();
set<Id>set2 = new set<Id>();
for(Id accIds : set containing account id’s){
if(mapidvsRecTypeId.get(accIds) = reocrdTypeId1 ){
set1.add(accIds);
}
if(mapidvsRecTypeId.get(accIds) = reocrdTypeId2 ){
set2.add(accIds);
}
}
- [adinserter block='9']
-
Hi Mohit,
Please try this one, it will allow you to separate ID’s based on Record Type, no matter how many record types are there, this code will run perfectly for any number of record types.
String objectAPIName = ‘Account’ ; //any object api
Set<Id> setOfAccountIds = new Set<Id>{‘0012800000v57PC’,’0012800001HtmNq’,’0012800000nAxd7′,’0012800000hVdfO’};
Map<Id,String> mapofRecordTypeIdandName = new Map<Id,String>();
Map<Id,Set<Id>> mapOfRecTypeIdVsAccIds = new map<Id,Set<Id>>();Schema.DescribeSObjectResult sobjectResult = Schema.getGlobalDescribe().get(objectAPIName).getDescribe();
List<Schema.RecordTypeInfo> recordTypeInfo = sobjectResult.getRecordTypeInfos();
for(Schema.RecordTypeInfo info : recordTypeInfo){
mapofRecordTypeIdandName.put(info.getRecordTypeId(),info.getName());
}
system.debug(‘***mapofRecordTypeIdandName***’+mapofRecordTypeIdandName);
for(Account accObj: [Select id,recordTypeId from Account where id in:setOfAccountIds]){
if(mapOfRecTypeIdVsAccIds.containsKey(accObj.recordTypeId)){
set<id> setofid = new set<id>(mapOfRecTypeIdVsAccIds.get(accObj.recordTypeId));
setofid.add(accObj.id);
mapOfRecTypeIdVsAccIds.put(accObj.recordTypeId,setofid);
}
else{
mapOfRecTypeIdVsAccIds.put(accObj.recordTypeId,new set<Id>{accObj.id});
}
}
system.debug(‘***mapOfRecTypeIdVsAccIds***’+mapOfRecTypeIdVsAccIds);Hope this helps you.
Log In to reply.