-
ChatterFeed
-
1Best Answers
-
0Likes Received
-
0Likes Given
-
0Questions
-
1Replies
Run Case Assignment rules from Apex
All,
Below is some existing code which assigns cases to SF queues. Please look at the code starting from : //get needed queue here and assign ownership of case.
I know have to expand this code for over 200 countries and 15 queues.
We already use the SF Case Assignment Rules for other purposes, I have read that you can run these rules from apex, but I am not sure how to incorporate this into the code below (which I did not write, I am an admin).
Can someone help me to amend my code?
https://help.salesforce.com/s/articleView?id=000338182&type=1
Below is some existing code which assigns cases to SF queues. Please look at the code starting from : //get needed queue here and assign ownership of case.
I know have to expand this code for over 200 countries and 15 queues.
We already use the SF Case Assignment Rules for other purposes, I have read that you can run these rules from apex, but I am not sure how to incorporate this into the code below (which I did not write, I am an admin).
Can someone help me to amend my code?
global class EmailToCaseHandler implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) { System.debug('handleInboundEmail'); List<WebToCaseSetting__mdt> caseSettings = [ Select Id, IsActive__c, WebCountry__c, WebOrigin__c FROM WebToCaseSetting__mdt WHERE IsActive__c = TRUE ]; Map<String, String> caseSettingsMap = new Map<String, String>(); for (WebToCaseSetting__mdt webToCaseSetting : caseSettings) { caseSettingsMap.put(webToCaseSetting.WebCountry__c, webToCaseSetting.WebOrigin__c); } Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); // // Remove HTML tags from email body String emailBodyString = email.htmlBody.stripHtmlTags(); System.debug('eBs -> ' + emailBodyString); System.debug('from -> ' + email.fromAddress); System.debug('to -> ' + email.toAddresses); // Split here email body by lines List<String> caseRawInfo = email.htmlBody.stripHtmlTags().split('\n'); Map<String, String> caseInfo = new Map<String, String>(); //populating map field name by value for (String s : caseRawInfo) { caseInfo.put(s.substringBefore(': '), s.substringAfter(': ')); /* string key = s.substringBefore(': '); string value= s.substringAfter(': '); system.debug('Key-->'+key+'--Value'+value); */ } for (String key : caseInfo.keySet()) { system.debug('caseInfo.' + key + ' => ' + caseInfo.get(key)); } System.debug('caseInfo --> ' + caseInfo); // Map Keys, values on left are Salesforce fields, values on right are field value from CMS form workflow //new String websiteOrigin; if(caseInfo.get('websiteOrigin') != null && caseInfo.get('websiteOrigin') != ''){ websiteOrigin = caseInfo.get('websiteOrigin').toLowerCase().normalizeSpace(); }else { websiteOrigin = 'abc.' + caseInfo.get('webCountry').toLowerCase().normalizeSpace(); } System.debug('websiteOrigin after -> ' + websiteOrigin); // Case caseToCreate = new Case( SuppliedName = caseInfo.get('name'), SuppliedFirstname__c = caseInfo.get('webFirstname'), SuppliedLastname__c = caseInfo.get('webLastname'), SuppliedCompany = caseInfo.get('company'), WebStreet__c = caseInfo.get('webStreet'), WebStreetNumber__c = caseInfo.get('webStreetNumber'), WebStreetNumberSuffix__c = caseInfo.get('webStreetNumberSuffix'), WebPostcode__c = caseInfo.get('webPostcode'), ShippingDistrict__c = caseInfo.get('webDistrict'), SuppliedPhone = caseInfo.get('phone'), WebCity__c = caseInfo.get('webCity'), SuppliedEmail = caseInfo.get('email'), WebDistributor__c = caseInfo.get('webDistributor'), WebDistributorDE__c = caseInfo.get('webDistributorDE'), WebCountry__c = caseInfo.get('webCountry'), OptInPerson__c = Boolean.valueOf(caseInfo.get('optinperson').replaceAll('\\s+', '')), WebsiteOrigin__c = websiteOrigin, WebProduct__c = caseInfo.get('webProduct'), WebDistributorCity__c = caseInfo.get('webDistributorCity'), ProductLotcode__c = caseInfo.get('productLotcode'), PackagingAvailable__c = Boolean.valueOf(caseInfo.get('packagingAvailable').replaceAll('\\s+', '')), BestBeforeDateString__c = caseInfo.get('bestBeforeDateString'), //for description order is important in umbraco workflow, it should be before optinperson Description = emailBodyString.substringAfter('description: ').substringBefore('optinperson'), Subject = caseInfo.get('recordType') + ' - ' + caseInfo.get('webProduct'), Type = caseInfo.get('type'), Priority = 'Medium', Status = 'New', Stage__c = 'New', Origin = 'Web' ); //get needed queue here and assign ownership of case Map<String, Id> mapQueueNameId = new Map<String, Id>(); List<Group> queues = [SELECT Id, DeveloperName FROM Group WHERE Type = 'Queue']; for (Group queue : queues) { mapQueueNameId.put(queue.DeveloperName, queue.Id); } if (caseInfo.get('webCountry').normalizeSpace() == 'RO') { setCaseOwner(caseToCreate, mapQueueNameId, 'Balkans'); } if (caseInfo.get('webCountry').normalizeSpace() == 'HU') { setCaseOwner(caseToCreate, mapQueueNameId, 'Hungary'); } if (caseInfo.get('webCountry').normalizeSpace() == 'DE') { setCaseOwner(caseToCreate, mapQueueNameId, 'Germany'); } if (caseInfo.get('webCountry').normalizeSpace() == 'PL') { setCaseOwner(caseToCreate, mapQueueNameId, 'Trade_CSC_Aviko_Poland'); } if (caseInfo.get('webCountry').normalizeSpace() == 'CZ') { setCaseOwner(caseToCreate, mapQueueNameId, 'Czech_Slovakia'); } if (caseInfo.get('webCountry').normalizeSpace() == 'RU') { setCaseOwner(caseToCreate, mapQueueNameId, 'Russia_CIS'); } if (caseInfo.get('webCountry').normalizeSpace() == 'US') { setCaseOwner(caseToCreate, mapQueueNameId, 'North_America'); } if (caseInfo.get('webCountry').normalizeSpace() == 'NL') { if (websiteOrigin == 'abc.nl') { setCaseOwner(caseToCreate, mapQueueNameId, 'Nederland'); } } String recordType = caseInfo.get('recordType').replaceAll('\\s+', ''); if (recordType != null) { //get record type Id if it was defined in email body Id recordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get(recordType).getRecordTypeId(); if (recordTypeId != null) { caseToCreate.RecordTypeId = recordTypeId; } } String contactEmail = caseInfo.get('email').replaceAll('\\s+', ''); if (contactEmail != null) { // looking for a matching contact here List<Contact> contactWithSameEmail = [Select Id From Contact Where Email = :contactEmail Limit 1]; if (!contactWithSameEmail.isEmpty()) { caseToCreate.ContactId = contactWithSameEmail[0].Id; } } system.debug('caseToCreate => ' + caseToCreate); insert caseToCreate; // checking if email contains attachments if (email.binaryAttachments != null && !email.binaryAttachments.isEmpty()) { ContentVersion cv = new ContentVersion( Title = email.binaryAttachments[0].fileName, PathOnClient = email.binaryAttachments[0].fileName, VersionData = email.binaryAttachments[0].body, IsMajorVersion = false ); // Create attachment with adding all needed info from email insert cv; // Creating document Link to connect this attachment with our case ContentDocumentLink docLink = new ContentDocumentLink(); docLink.ContentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id LIMIT 1].ContentDocumentId; docLink.LinkedEntityId = caseToCreate.Id; docLink.ShareType = 'V'; insert docLink; } result.success = true; return result; } private static void setCaseOwner(Case caseToCreate, Map<String, Id> mapQueueNameId, String queueName) { if (mapQueueNameId.containsKey(queueName)) { caseToCreate.OwnerId = mapQueueNameId.get(queueName); } } }Googled this answer:
https://help.salesforce.com/s/articleView?id=000338182&type=1
- Dave The Rave
- December 17, 2021
- Like
- 0
Run Case Assignment rules from Apex
All,
Below is some existing code which assigns cases to SF queues. Please look at the code starting from : //get needed queue here and assign ownership of case.
I know have to expand this code for over 200 countries and 15 queues.
We already use the SF Case Assignment Rules for other purposes, I have read that you can run these rules from apex, but I am not sure how to incorporate this into the code below (which I did not write, I am an admin).
Can someone help me to amend my code?
https://help.salesforce.com/s/articleView?id=000338182&type=1
Below is some existing code which assigns cases to SF queues. Please look at the code starting from : //get needed queue here and assign ownership of case.
I know have to expand this code for over 200 countries and 15 queues.
We already use the SF Case Assignment Rules for other purposes, I have read that you can run these rules from apex, but I am not sure how to incorporate this into the code below (which I did not write, I am an admin).
Can someone help me to amend my code?
global class EmailToCaseHandler implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) { System.debug('handleInboundEmail'); List<WebToCaseSetting__mdt> caseSettings = [ Select Id, IsActive__c, WebCountry__c, WebOrigin__c FROM WebToCaseSetting__mdt WHERE IsActive__c = TRUE ]; Map<String, String> caseSettingsMap = new Map<String, String>(); for (WebToCaseSetting__mdt webToCaseSetting : caseSettings) { caseSettingsMap.put(webToCaseSetting.WebCountry__c, webToCaseSetting.WebOrigin__c); } Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); // // Remove HTML tags from email body String emailBodyString = email.htmlBody.stripHtmlTags(); System.debug('eBs -> ' + emailBodyString); System.debug('from -> ' + email.fromAddress); System.debug('to -> ' + email.toAddresses); // Split here email body by lines List<String> caseRawInfo = email.htmlBody.stripHtmlTags().split('\n'); Map<String, String> caseInfo = new Map<String, String>(); //populating map field name by value for (String s : caseRawInfo) { caseInfo.put(s.substringBefore(': '), s.substringAfter(': ')); /* string key = s.substringBefore(': '); string value= s.substringAfter(': '); system.debug('Key-->'+key+'--Value'+value); */ } for (String key : caseInfo.keySet()) { system.debug('caseInfo.' + key + ' => ' + caseInfo.get(key)); } System.debug('caseInfo --> ' + caseInfo); // Map Keys, values on left are Salesforce fields, values on right are field value from CMS form workflow //new String websiteOrigin; if(caseInfo.get('websiteOrigin') != null && caseInfo.get('websiteOrigin') != ''){ websiteOrigin = caseInfo.get('websiteOrigin').toLowerCase().normalizeSpace(); }else { websiteOrigin = 'abc.' + caseInfo.get('webCountry').toLowerCase().normalizeSpace(); } System.debug('websiteOrigin after -> ' + websiteOrigin); // Case caseToCreate = new Case( SuppliedName = caseInfo.get('name'), SuppliedFirstname__c = caseInfo.get('webFirstname'), SuppliedLastname__c = caseInfo.get('webLastname'), SuppliedCompany = caseInfo.get('company'), WebStreet__c = caseInfo.get('webStreet'), WebStreetNumber__c = caseInfo.get('webStreetNumber'), WebStreetNumberSuffix__c = caseInfo.get('webStreetNumberSuffix'), WebPostcode__c = caseInfo.get('webPostcode'), ShippingDistrict__c = caseInfo.get('webDistrict'), SuppliedPhone = caseInfo.get('phone'), WebCity__c = caseInfo.get('webCity'), SuppliedEmail = caseInfo.get('email'), WebDistributor__c = caseInfo.get('webDistributor'), WebDistributorDE__c = caseInfo.get('webDistributorDE'), WebCountry__c = caseInfo.get('webCountry'), OptInPerson__c = Boolean.valueOf(caseInfo.get('optinperson').replaceAll('\\s+', '')), WebsiteOrigin__c = websiteOrigin, WebProduct__c = caseInfo.get('webProduct'), WebDistributorCity__c = caseInfo.get('webDistributorCity'), ProductLotcode__c = caseInfo.get('productLotcode'), PackagingAvailable__c = Boolean.valueOf(caseInfo.get('packagingAvailable').replaceAll('\\s+', '')), BestBeforeDateString__c = caseInfo.get('bestBeforeDateString'), //for description order is important in umbraco workflow, it should be before optinperson Description = emailBodyString.substringAfter('description: ').substringBefore('optinperson'), Subject = caseInfo.get('recordType') + ' - ' + caseInfo.get('webProduct'), Type = caseInfo.get('type'), Priority = 'Medium', Status = 'New', Stage__c = 'New', Origin = 'Web' ); //get needed queue here and assign ownership of case Map<String, Id> mapQueueNameId = new Map<String, Id>(); List<Group> queues = [SELECT Id, DeveloperName FROM Group WHERE Type = 'Queue']; for (Group queue : queues) { mapQueueNameId.put(queue.DeveloperName, queue.Id); } if (caseInfo.get('webCountry').normalizeSpace() == 'RO') { setCaseOwner(caseToCreate, mapQueueNameId, 'Balkans'); } if (caseInfo.get('webCountry').normalizeSpace() == 'HU') { setCaseOwner(caseToCreate, mapQueueNameId, 'Hungary'); } if (caseInfo.get('webCountry').normalizeSpace() == 'DE') { setCaseOwner(caseToCreate, mapQueueNameId, 'Germany'); } if (caseInfo.get('webCountry').normalizeSpace() == 'PL') { setCaseOwner(caseToCreate, mapQueueNameId, 'Trade_CSC_Aviko_Poland'); } if (caseInfo.get('webCountry').normalizeSpace() == 'CZ') { setCaseOwner(caseToCreate, mapQueueNameId, 'Czech_Slovakia'); } if (caseInfo.get('webCountry').normalizeSpace() == 'RU') { setCaseOwner(caseToCreate, mapQueueNameId, 'Russia_CIS'); } if (caseInfo.get('webCountry').normalizeSpace() == 'US') { setCaseOwner(caseToCreate, mapQueueNameId, 'North_America'); } if (caseInfo.get('webCountry').normalizeSpace() == 'NL') { if (websiteOrigin == 'abc.nl') { setCaseOwner(caseToCreate, mapQueueNameId, 'Nederland'); } } String recordType = caseInfo.get('recordType').replaceAll('\\s+', ''); if (recordType != null) { //get record type Id if it was defined in email body Id recordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get(recordType).getRecordTypeId(); if (recordTypeId != null) { caseToCreate.RecordTypeId = recordTypeId; } } String contactEmail = caseInfo.get('email').replaceAll('\\s+', ''); if (contactEmail != null) { // looking for a matching contact here List<Contact> contactWithSameEmail = [Select Id From Contact Where Email = :contactEmail Limit 1]; if (!contactWithSameEmail.isEmpty()) { caseToCreate.ContactId = contactWithSameEmail[0].Id; } } system.debug('caseToCreate => ' + caseToCreate); insert caseToCreate; // checking if email contains attachments if (email.binaryAttachments != null && !email.binaryAttachments.isEmpty()) { ContentVersion cv = new ContentVersion( Title = email.binaryAttachments[0].fileName, PathOnClient = email.binaryAttachments[0].fileName, VersionData = email.binaryAttachments[0].body, IsMajorVersion = false ); // Create attachment with adding all needed info from email insert cv; // Creating document Link to connect this attachment with our case ContentDocumentLink docLink = new ContentDocumentLink(); docLink.ContentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id LIMIT 1].ContentDocumentId; docLink.LinkedEntityId = caseToCreate.Id; docLink.ShareType = 'V'; insert docLink; } result.success = true; return result; } private static void setCaseOwner(Case caseToCreate, Map<String, Id> mapQueueNameId, String queueName) { if (mapQueueNameId.containsKey(queueName)) { caseToCreate.OwnerId = mapQueueNameId.get(queueName); } } }Googled this answer:
https://help.salesforce.com/s/articleView?id=000338182&type=1
- Dave The Rave
- December 17, 2021
- Like
- 0