Activity Forums Salesforce® Discussions How can we separate the ID's based on the record type?

  • Ajit

    Member
    August 11, 2016 at 6:53 am

    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);

    }

    }

     

  • PRANAV

    Member
    January 19, 2018 at 2:00 pm

    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.

Popular Salesforce Blogs

Popular Salesforce Videos