+ Start a Discussion
Nirmal ChristopherNirmal Christopher 

Help with String Methods


Attempting to map exact spring string to the API name of the queues using 'split by character camel case. Currently the process is not picking up the API names. 

for Example I have variable like CDFiles_3pTalk as the API name.

CDFiles- will be a constant string which will be concatenated always

The text field I have should be render aotomatically text automatically to the above format no matter what the Imput is 

for Instance if the user give the input as 3pTalk it should render me the name CDFiles_3pTalk but it gives me CDFIles_3_p_Talk which is incorect. Copied my code below  Any suggerstions?
 
/******

Description: This Trigger will update the Owner in the Customer downloads object based on the field SO_Name__c. The field "SO_Name__c"
needs to match exactly with the pre-existing Account Name. Queue name picks the right record based on the SO_Name__C and matching Account Name and updates the "OwnerId" field 
and "Downloads_Account_Name__c" Field

*******/

trigger UpdateOwnerinCD on Customer_Download__c (before insert,before update) {
    set<string>collectaccnames=new set<string>();
    set<string>collectaccnames1=new set<string>();
        for(Customer_Download__c cd:trigger.new){
        //Collecting and processing the list of variable for query clauses 
            if(cd.SO_Name__c!=null){
                string clipoff=cd.SO_Name__c;
                 clipoff=clipoff.replaceall('[^a-z^A-Z^0-9]','_');
                    system.debug(clipoff);
                    List<String> splitname = clipoff.splitByCharacterTypecamelcase();
                    System.debug(splitname);
                    string clipoff1='';
                    for (string s:splitname){
                    if(!s.contains('_'))
                        {clipoff1+=s;}   
                            clipoff1+='_';
                        }
                    clipoff1=clipoff1.replace('__','_');
                    clipoff1=clipoff1.removeend('_');
                    System.debug(clipoff1);
      
                collectaccnames.add('CDfiles_'+clipoff1);
                collectaccnames1.add(cd.SO_Name__c);
            }
          }
System.debug('collectaccnames'+collectaccnames);
    //Build Lists to select Records
    list<account>acc=[select id,name from account where name IN:collectaccnames1 limit 9999];
    list<group>g=[select id,developername,type from group where developername IN:collectaccnames AND  type='queue' ];
    system.debug('g'+g);
    Map<string,group>groupmap=new map<string,group>();
    Map<string,account>accountmap=new map<string,account>();
        for(group g1:g){
            groupmap.put(g1.developername,g1);
        }
        for(account acc1:acc){
            accountmap.put(acc1.name,acc1);
        }
    system.debug('groupmap'+groupmap);
    system.debug('accountmap'+accountmap);
        for(Customer_Download__c cd1:trigger.new){
            if(cd1.SO_Name__c!=null){
            //Get the values from the Maps and update the Owner field and Downloads account name field.
                string clipoff=cd1.SO_Name__c;
                clipoff=clipoff.replaceall('[^a-z^A-Z^0-9]','_');
                system.debug(clipoff);
                List<String> splitname = clipoff.splitByCharacterTypecamelcase();
                System.debug(splitname);
                string clipoff1='';
                for (string s:splitname){
                    if(!s.contains('_'))
                        {clipoff1+=s;}   
                        clipoff1+='_';
                }
                clipoff1=clipoff1.replace('__','_');
                clipoff1=clipoff1.removeend('_');
                System.debug(clipoff1);               
                System.debug('clipoff1'+clipoff1);
                clipoff1 ='CDfiles_'+clipoff1;
                system.debug('clipoff1'+clipoff1 );
                group g12 = groupmap.get(clipoff1);
                system.debug(g12+'g12');
                account acco=accountmap.get(cd1.SO_Name__c);
                    if(!test.isrunningtest()){
                    if(g12 != null && g12.id!=null){
                     cd1.Ownerid=g12.id;
                     }else{
                        cd1.Ownerid=userinfo.getuserid();
                    }
                if(acco!=null){
                cd1.Downloads_Account_Name__c=acco.id;
                }
                }
            }
        } 
   }