You need to sign in to do that
Don't have an account?
Soundar Rajan Ponpandi
Email is not sending from this apex code
Hi,
Email notification is not sending after sharing the record. Can anyone please check and where i did a mistake.
Regards,
Soundar.
Email notification is not sending after sharing the record. Can anyone please check and where i did a mistake.
public static void GD_shareOrderWithStoreUser(Map<Id,GD_Order__c> newMap, Map<Id,GD_Order__c> oldMap){ List<GD_Order__c> ordersToShare = new List<GD_Order__c>(); for( GD_Order__c order : newMap.values()) { if(order.GD_Type__c == 'Online Order' && order.GD_Status__c != oldMap.get(order.Id).GD_Status__c && order.GD_Status__c == 'Accepted' && order.GD_Sales_Rep__c != null ){ ordersToShare.add(order); system.debug('*Order to share ***' + order.name); } } if(ordersToShare.size() > 0){ List<GD_Order__Share> orderShareList = new List<GD_Order__Share>(); Map<String,Group> groupIdByName = new Map<String,Group>(); Map<String,List<Id>> usersByGroup = new Map<String,List<Id>>(); for(Group grp : [SELECT Id,DeveloperNAME,(SELECT Id,UserOrGroupId FROM GroupMembers) FROM Group WHERE DeveloperNAME = 'Pharma_Store_Dubai' OR DeveloperNAME = 'Pharma_Store_Abu_Dhabi']){ if(grp.DeveloperNAME == 'Pharma_Store_Dubai'){ groupIdByName.put('Dubai', grp); } if(grp.DeveloperNAME == 'Pharma_Store_Abu_Dhabi'){ groupIdByName.put('Abu Dhabi', grp); } for(GroupMember gm : grp.GroupMembers){ if(grp.DeveloperNAME == 'Pharma_Store_Dubai'){ if(usersByGroup.containsKey('Dubai')){ usersByGroup.get('Dubai').add(gm.UserOrGroupId); }else{ usersByGroup.put('Dubai',new List<Id>{gm.UserOrGroupId}); } }else if(grp.DeveloperNAME == 'Pharma_Store_Abu_Dhabi'){ if(usersByGroup.containsKey('Abu Dhabi')){ usersByGroup.get('Abu Dhabi').add(gm.UserOrGroupId); }else{ usersByGroup.put('Abu Dhabi',new List<Id>{gm.UserOrGroupId}); } } } } if(groupIdByName.size() > 0){ List<EmailTemplate> templateList = [SELECT Id,Body FROM EmailTemplate WHERE DeveloperName ='GD_Email_Notification_to_Approvers']; List<Messaging.SingleEmailMessage> sendEmailNotification = new List<Messaging.SingleEmailMessage>(); for( GD_Order__c order : ordersToShare) { if(groupIdByName.containsKey(order.GD_Pharma_Location__c)){ GD_Order__Share shareRecord = new GD_Order__Share(); shareRecord.AccessLevel = 'Edit'; shareRecord.UserOrGroupId = groupIdByName.get(order.GD_Pharma_Location__c).Id; shareRecord.ParentId = order.Id; system.debug('shareRecord '+shareRecord); orderShareList.add(shareRecord); if(templateList.size() > 0){ if(usersByGroup.containsKey(order.GD_Pharma_Location__c)){ for(Id storeUserId : usersByGroup.get(order.GD_Pharma_Location__c)){ system.debug('Store User Email |' + storeUserId); Messaging.SingleEmailMessage mail = Messaging.renderStoredEmailTemplate(templateList.get(0).Id, storeUserId, order.Id); String emailSubject = mail.getSubject(); String emailTextBody = mail.getPlainTextBody(); mail.setSubject(emailSubject); mail.setPlainTextBody(emailTextBody); mail.setSaveAsActivity(false); mail.setTargetObjectId(storeUserId); sendEmailNotification.add(mail); } } } } } if(orderShareList.size () > 0){ Insert orderShareList; } if(sendEmailNotification.size() > 0){ system.debug('sendEmailNotification.size()' + sendEmailNotification.size()); List<Messaging.SendEmailResult> results = Messaging.sendEmail(sendEmailNotification); } } } }
Regards,
Soundar.
Guess the debug statement you added 'system.debug('sendEmailNotification.size()' + sendEmailNotification.size());' is getting printed. If that is the case, please capture Email Logs and check for error codes
All Answers
Guess the debug statement you added 'system.debug('sendEmailNotification.size()' + sendEmailNotification.size());' is getting printed. If that is the case, please capture Email Logs and check for error codes
Now Email notification is sending but twice. Can you please let me know why it's happened ?
Regards,
Soundar.
It's because of i created 1 group and 1 Queue.
Regards,
Soundar.