• ShowerHammer
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 1
    Replies
So this one has me stumped. I have created a Trigger and Apex Class to take the data category tied to a Knowledge Article and map it to a "Case Category" field. The trigger functions correctly by mapping the data category to the case category, however the problem is when I am running it with a user whose language is something other than English and it is putting the translated data category in the case category which is resulting in a lot of junk data.

The reason why it is running my query as the user logged in which results in the return of the translated labels.
This is the return in Japanese
<pre>
11:23:03.252 (252682000)|USER_DEBUG|[41]|DEBUG|DEBUG::: toReturn - (CategoryLevel:[label=All, level=0, name=All, parent=null], CategoryLevel:[label=アカウント/登録/サインイン, level=1, name=Accounts, parent=(already output)], CategoryLevel:[label=アカウントに関する質問, level=2, name=Account_Questions, parent=(already output)], CategoryLevel:[label=アクティベーションに関する問題, level=2, name=Activation, parent=(already output)], CategoryLevel:[label=一度も電子メールを受信していません, level=3, name=Email_Not_Received, parent=(already output)], CategoryLevel:[label=アクティベーションリンクが作動していません, level=3, name=Link_not_working, parent=(already output)], CategoryLevel:[label=ブラウザの問題, level=2, name=Browser1, parent=(already output)], CategoryLevel:[label=一般アカウント, level=2, name=FS_Account, parent=(already output)], CategoryLevel:[label=設定/プロフィール, level=3, name=FS_Account_Maintenance, parent=(already output)], CategoryLevel:[label=設定変更, level=4, name=Change_Settings, parent=(already output)], ...)
</pre>
And here it is in English
<pre>
11:22:06.641 (641524000)|USER_DEBUG|[41]|DEBUG|DEBUG::: toReturn - (CategoryLevel:[label=All, level=0, name=All, parent=null], CategoryLevel:[label=Account/Registration/Sign-in, level=1, name=Accounts, parent=(already output)], CategoryLevel:[label=Account Questions, level=2, name=Account_Questions, parent=(already output)], CategoryLevel:[label=Activation Issues, level=2, name=Activation, parent=(already output)], CategoryLevel:[label=Never Received Email, level=3, name=Email_Not_Received, parent=(already output)], CategoryLevel:[label=Activation link not working, level=3, name=Link_not_working, parent=(already output)], CategoryLevel:[label=Browser Problems, level=2, name=Browser1, parent=(already output)], CategoryLevel:[label=Public Account, level=2, name=FS_Account, parent=(already output)], CategoryLevel:[label=Settings/Profile, level=3, name=FS_Account_Maintenance, parent=(already output)], CategoryLevel:[label=Changing Settings, level=4, name=Change_Settings, parent=(already output)], ...)
</pre>


Below is the Apex Class code that finds the label to put into the case category.

<pre>
private List<CategoryLevel> getCategories(string categAPIName) {
        System.debug('DEBUG::: Category API Name - ' + categAPIName);
  List<CategoryLevel> toReturn = new List<CategoryLevel>();
  DataCategoryGroupSobjectTypePair pair = new DataCategoryGroupSobjectTypePair();
  pair.setSobject('KnowledgeArticleVersion');
  pair.setDataCategoryGroupName(categAPIName);
  for (DataCategory categ : Schema.describeDataCategoryGroupStructures(new Datacategorygroupsobjecttypepair[] { pair }, false)[0].getTopCategories()) {
   CategoryLevel lev1 = new CategoryLevel(0, categ.getLabel(), categ.getName(), null);
   toReturn.add(lev1);
   addSubcategories(lev1, toReturn, categ, 1);
  }
        System.debug('DEBUG::: toReturn - ' + toReturn);
  return toReturn;
}
</pre>
I currently an trying to replace all non-alphanumeric characters in a string and replacing everything but the space, would like that to be a hyphen, with a blank value to remove it and cannot figure out how to escape out commas, parenthesis, apostrophes, periods and colons.

The strings I am parsing could have any one of the characters below in them.

This is what I am using:
foo = foo.replaceAll('[\\,"\\:~|!|@|#|$|%|^|&|*|_|+|=|<|>|?\\(\\)\\{\\}\\;\\\']', '');
foo = foo.replaceAl(' ','-');
 
I have tried using but cannot get this to work either.
foo = foo.replaceAll('![a-zA-Z0-9]', '');
Any help would be appreciated.

Our implementation company created this trigger to search for accounts and tie a case to. It works, however if there is asingle quote in the from header such as "email@email (Name's)" then it throws an error since it is not escaping out the single quote. Any help in regards to fixing the code would be greatly appreciated. I figure I can use "string.escapeSingleQuote(xx)" but am not sure exactly where I need to put it. I have tried:

//Escape out the single quotes
emailList = String.escapeSingleQuote(emailList);
phoneList = String.escapeSingleQuote(phoneList);
cisList = String.escapeSingleQuote(cisList);

List<Account> accSearchList = getAccList(emailList, phoneList, nameList, cisList);

 But get: Error: Compile Error: Variable does not exist: string at line 38 column 21

 

The error that I receive is below:

CaseAll: execution of BeforeInsert

caused by: System.QueryException: unexpected token: t

Trigger.CaseAll: line 130, column 1
Trigger.CaseAll: line 37, column 1
Trigger.CaseAll: line 10, column 1

 

trigger CaseAll on Case (before insert, before update) {
	
	if (trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) {
		UpdateListViewLink();
		UpdateCaseTransferCount();
		updateContact();
	}
	
	if (trigger.isInsert && trigger.isBefore) {
		associatePatronToCase();
	}
	
	private void associatePatronToCase() {
		List<String> emailList = new List<String>();
		List<String> phoneList = new List<String>();
		List<String> nameList = new List<String>();
		List<String> cisList = new List<String>();
		for (Case c : Trigger.new) {
			if (c.SuppliedEmail != null && c.SuppliedEmail != '') {
				emailList.add(c.SuppliedEmail);
			}
			if (c.SuppliedPhone != null && c.SuppliedPhone != '') {
				phoneList.add(c.SuppliedPhone);
			}
			if (c.SuppliedName != null && c.SuppliedName != '') {
				nameList.add(c.SuppliedName);
			}
			if (c.CIS_ID__c  != null && c.CIS_ID__c  != '') {
				cisList.add(c.CIS_ID__c );
			}
		}
		System.debug('emailList:::' + emailList);
		System.debug('phoneList:::' + phoneList);
		System.debug('nameList:::' + nameList);
		System.debug('cisList:::' + cisList);
		
		List<Account> accSearchList = getAccList(emailList, phoneList, nameList, cisList);
		List<Account> accList = [SELECT Id, PersonEmail, Phone, Name, CIS_ID__pc, CreatedDate FROM Account 
								 WHERE Id in :accSearchList AND isPersonAccount = true AND (PersonEmail IN :emailList OR Phone IN :phoneList OR Name IN :nameList OR CIS_ID__pc IN :cisList)];
		Map<String, Account> phoneMap = new Map<String, Account>();
		Map<String, Account> emailMap = new Map<String, Account>();
		Map<String, Account> nameMap = new Map<String, Account>();
		Map<String, Account> cisMap = new Map<String, Account>();
		for (Account acc : accList) {
			if (acc.Phone != null && acc.Phone != '') {
				if (phoneMap.containsKey(acc.Phone)) {
					if (acc.CreatedDate < phoneMap.get(acc.Phone).CreatedDate) {
						phoneMap.put(acc.Phone, acc);
					}
				} else {
					phoneMap.put(acc.Phone, acc);
				}
			}
			if (acc.PersonEmail != null && acc.PersonEmail != '') {
				if (!emailMap.containsKey(acc.PersonEmail)) {
					emailMap.put(acc.PersonEmail, acc);
				}
			}
			if (acc.CIS_ID__pc != null && acc.CIS_ID__pc != '') {
				if (!cisMap.containsKey(acc.CIS_ID__pc)) {
					cisMap.put(acc.CIS_ID__pc, acc);
				}
			}
			if (acc.Name != null && acc.Name != '') {
				if (nameMap.containsKey(acc.Name)) {
					if (acc.CreatedDate < nameMap.get(acc.Name).CreatedDate) {
						nameMap.put(acc.Name, acc);
					}
				} else {
					nameMap.put(acc.Name, acc);
				}
			} 
		}
		
		for (Case c : Trigger.new) {
			if (cisMap.containsKey(c.CIS_ID__c)) {
				c.AccountId = cisMap.get(c.CIS_ID__c).Id;
			} else if (phoneMap.containsKey(c.SuppliedPhone)) {
				c.AccountId = phoneMap.get(c.SuppliedPhone).Id;
			} else if (emailMap.containsKey(c.SuppliedEmail)) {
				c.AccountId = emailMap.get(c.SuppliedEmail).Id;
			} else if (nameMap.containsKey(c.SuppliedName)) {
				c.AccountId = nameMap.get(c.SuppliedName).Id;
			} else if ((c.SuppliedName != null && c.SuppliedName != '') || (c.SuppliedEmail != null && c.SuppliedEmail != '')) {
				Account acc = new Account();
				acc.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
				//acc.FirstName = c.SuppliedName;
				if (c.SuppliedName != null && c.SuppliedName != '') {
					acc.LastName = c.SuppliedName;
				} else {
					acc.LastName = c.SuppliedEmail;
				}
				acc.Phone = c.SuppliedPhone;
				acc.PersonEmail = c.SuppliedEmail;
				if (c.CIS_ID__c != null && c.CIS_ID__c != '') {
					acc.CIS_ID__pc = c.CIS_ID__c;
				}
				try {
					insert acc;
				} catch (DMLException e) {
					Trigger.new[0].addError(e.getMessage());
				}
				c.AccountId = acc.Id;
			}
		}
	}
	
	private List<Account> getAccList(List<String> emailList, List<String> phoneList, List<String> nameList, List<String> cisList) {
		String searchString = '';
		for (String s : emailList) {
			searchString += s;
			searchString += ' OR ';
		}
		for (String s : phoneList) {
			searchString += s;
			searchString += ' OR ';
		}
		for (String s : nameList) {
			searchString += s;
			searchString += ' OR ';
		}
		for (String s : cisList) {
			searchString += s;
			searchString += ' OR ';
		}
		List<Account> accList = new List<Account>();
		if (searchString.length() > 0) {
			searchString = searchString.substring(0,searchString.length()-4);
			String sosl = 'FIND \'' + searchString + '\' IN ALL FIELDS RETURNING Account (Id, PersonEmail, Phone, Name, CIS_ID__pc, CreatedDate)';
			accList = Search.query(sosl)[0];
		}
		return accList;
	}
	
	private void updateContact() {
		Set<Id> accountIds = new Set<Id>();
		Set<Id> ContactIds = new Set<Id>();
	
		for (Case c : Trigger.new) {
			if (c.AccountId != null) {
				AccountIds.add(c.AccountId);
			}
			if (c.ContactId != null) {
				ContactIds.add(c.ContactId);
			}
		}
	
		Map<Id, Account> AccountEntries = new Map<Id, Account>(
			[select PersonContactId from Account where id in :accountIds]
		);
		System.debug('AccountEntries:::' + AccountEntries);
		
		Map<Id, Contact> ContactEntries = new Map<Id, Contact>(
			[select AccountId from Contact where id in :ContactIds]
		);
		System.debug('ContactEntries:::' + ContactEntries);
		
		Account a;
		Contact con;
		for (Case c : Trigger.new) {
			if (AccountEntries.containsKey(c.AccountId)) {
				c.ContactId  = AccountEntries.get(c.AccountId).PersonContactId;
			}
			if (ContactEntries.containsKey(c.ContactId)) {
				c.AccountId  = ContactEntries.get(c.ContactId).AccountId;
			}
			System.debug('c.ContactId:::' + c.ContactId);
			System.debug('c.AccountId:::' + c.AccountId);
		}
	}
	private void UpdateListViewLink() {
		List<Queue_ListView_Link__c> queueListViewLinks = [SELECT Name, ListViewId__c, QueueId__c FROM Queue_ListView_Link__c];
		Map<String,Queue_ListView_Link__c> listViewLinksMap = new Map<String,Queue_ListView_Link__c>();
		for (Queue_ListView_Link__c q : queueListViewLinks) {
			listViewLinksMap.put(q.QueueId__c.left(15), q);
		}
		System.debug('listViewLinksMap:::' + listViewLinksMap);
		for (Case c : trigger.new) {
			if (c.OwnerId != null && listViewLinksMap.containsKey(String.valueOf(c.OwnerId).left(15))) {
				c.ListView_Link__c = 'https://' + System.URL.getSalesforceBaseURL().getHost() + '/500?fcf=' + listViewLinksMap.get(String.valueOf(c.OwnerId).left(15)).ListViewId__c;
				c.ListView_Name__c = listViewLinksMap.get(String.valueOf(c.OwnerId).left(15)).Name;
			}
		}
	}
	
	private void UpdateCaseTransferCount() {
		System.debug('updatedCases0:::' + CaseUtil.updatedCases);
		if (trigger.isInsert) {
			for (Case c : trigger.new) {
				if (!CaseUtil.updatedCases.contains(c.Id)) {
					c.Case_Transfer_Count__c = 0;
					CaseUtil.updatedCases.add(c.Id);
				}
			}
		} else if (trigger.isUpdate) {
			for (Case c : trigger.new) {
				System.debug('c.OwnerId:::' + c.OwnerId);
				System.debug('Trigger.oldMap.get(c.Id).OwnerId::::' + Trigger.oldMap.get(c.Id).OwnerId);
				if (c.OwnerId != null && Trigger.oldMap.get(c.Id).OwnerId != null && c.OwnerId != Trigger.oldMap.get(c.Id).OwnerId) {
					if (!CaseUtil.updatedCases.contains(c.Id)) {
						if (c.Case_Transfer_Count__c != null) {
							c.Case_Transfer_Count__c += 1;
						} else {
							c.Case_Transfer_Count__c = 1;
						}
						CaseUtil.updatedCases.add(c.Id);
					}
				}
			}
		}
		System.debug('updatedCases:::' + CaseUtil.updatedCases);
	}
}

 

I wrote a basic APEX Class that helps create a case from a VisualForce page and need to write a test class for it. I am far from being a developer and managed to get the class uop and running but I do not have the foggiest idea where to start for a test class

public class SubmitCaseController {
    public Case c { get; set; }
    public SubmitCaseController() {
        c = new Case();
    }
    public PageReference submitCase() {
            try {   
                        
                // Specify DML options to ensure the assignment rules are executed
                Database.DMLOptions dmlOpts = new Database.DMLOptions();
                c.Created_By_User__c = UserInfo.getUserId();
                c.OwnerId = '00G30000003El5v';
                c.RecordTypeId = '012n00000004I1r';
                c.Origin = 'Internal Ticketing';
                c.setOptions(dmlOpts);

                // Insert the case
                INSERT c;
                return new PageReference('/apex/thank_you_case_submission');
            } catch (Exception e) {
                ApexPages.addMessages(e);
                return null;
            }
        
    }
 }

 

So this one has me stumped. I have created a Trigger and Apex Class to take the data category tied to a Knowledge Article and map it to a "Case Category" field. The trigger functions correctly by mapping the data category to the case category, however the problem is when I am running it with a user whose language is something other than English and it is putting the translated data category in the case category which is resulting in a lot of junk data.

The reason why it is running my query as the user logged in which results in the return of the translated labels.
This is the return in Japanese
<pre>
11:23:03.252 (252682000)|USER_DEBUG|[41]|DEBUG|DEBUG::: toReturn - (CategoryLevel:[label=All, level=0, name=All, parent=null], CategoryLevel:[label=アカウント/登録/サインイン, level=1, name=Accounts, parent=(already output)], CategoryLevel:[label=アカウントに関する質問, level=2, name=Account_Questions, parent=(already output)], CategoryLevel:[label=アクティベーションに関する問題, level=2, name=Activation, parent=(already output)], CategoryLevel:[label=一度も電子メールを受信していません, level=3, name=Email_Not_Received, parent=(already output)], CategoryLevel:[label=アクティベーションリンクが作動していません, level=3, name=Link_not_working, parent=(already output)], CategoryLevel:[label=ブラウザの問題, level=2, name=Browser1, parent=(already output)], CategoryLevel:[label=一般アカウント, level=2, name=FS_Account, parent=(already output)], CategoryLevel:[label=設定/プロフィール, level=3, name=FS_Account_Maintenance, parent=(already output)], CategoryLevel:[label=設定変更, level=4, name=Change_Settings, parent=(already output)], ...)
</pre>
And here it is in English
<pre>
11:22:06.641 (641524000)|USER_DEBUG|[41]|DEBUG|DEBUG::: toReturn - (CategoryLevel:[label=All, level=0, name=All, parent=null], CategoryLevel:[label=Account/Registration/Sign-in, level=1, name=Accounts, parent=(already output)], CategoryLevel:[label=Account Questions, level=2, name=Account_Questions, parent=(already output)], CategoryLevel:[label=Activation Issues, level=2, name=Activation, parent=(already output)], CategoryLevel:[label=Never Received Email, level=3, name=Email_Not_Received, parent=(already output)], CategoryLevel:[label=Activation link not working, level=3, name=Link_not_working, parent=(already output)], CategoryLevel:[label=Browser Problems, level=2, name=Browser1, parent=(already output)], CategoryLevel:[label=Public Account, level=2, name=FS_Account, parent=(already output)], CategoryLevel:[label=Settings/Profile, level=3, name=FS_Account_Maintenance, parent=(already output)], CategoryLevel:[label=Changing Settings, level=4, name=Change_Settings, parent=(already output)], ...)
</pre>


Below is the Apex Class code that finds the label to put into the case category.

<pre>
private List<CategoryLevel> getCategories(string categAPIName) {
        System.debug('DEBUG::: Category API Name - ' + categAPIName);
  List<CategoryLevel> toReturn = new List<CategoryLevel>();
  DataCategoryGroupSobjectTypePair pair = new DataCategoryGroupSobjectTypePair();
  pair.setSobject('KnowledgeArticleVersion');
  pair.setDataCategoryGroupName(categAPIName);
  for (DataCategory categ : Schema.describeDataCategoryGroupStructures(new Datacategorygroupsobjecttypepair[] { pair }, false)[0].getTopCategories()) {
   CategoryLevel lev1 = new CategoryLevel(0, categ.getLabel(), categ.getName(), null);
   toReturn.add(lev1);
   addSubcategories(lev1, toReturn, categ, 1);
  }
        System.debug('DEBUG::: toReturn - ' + toReturn);
  return toReturn;
}
</pre>

I wrote a basic APEX Class that helps create a case from a VisualForce page and need to write a test class for it. I am far from being a developer and managed to get the class uop and running but I do not have the foggiest idea where to start for a test class

public class SubmitCaseController {
    public Case c { get; set; }
    public SubmitCaseController() {
        c = new Case();
    }
    public PageReference submitCase() {
            try {   
                        
                // Specify DML options to ensure the assignment rules are executed
                Database.DMLOptions dmlOpts = new Database.DMLOptions();
                c.Created_By_User__c = UserInfo.getUserId();
                c.OwnerId = '00G30000003El5v';
                c.RecordTypeId = '012n00000004I1r';
                c.Origin = 'Internal Ticketing';
                c.setOptions(dmlOpts);

                // Insert the case
                INSERT c;
                return new PageReference('/apex/thank_you_case_submission');
            } catch (Exception e) {
                ApexPages.addMessages(e);
                return null;
            }
        
    }
 }