function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sam_Adminsam_Admin 

Help with code

Hi folks,

       Here is the code for my task but it's nt working, here is my task, if you go to solutions in case realated list, and when you click on find solutions and select any solution then it gets attached to the case and the casecontact gets the solution as an email, iam not able to figure out what needs to be done in this code to get executed, can anyone help me out with this please.

 

trigger solutionInsert on Solution (after insert) {

 List<Id> sIdList;
 Map<Id,String> userMap = new Map<Id,String>();
 Map<Id,Id> caseMap = new Map<Id,Id>();
 
 for(Solution s:Trigger.New)
 {
  sIdList.add(s.id);
  }
 if(sIdList.size()>0)
 {
  List<Case> cList = [select c.id,c.Owner.Email,c.Owner.Name,c.OwnerId,(select cs.caseId,cs.SolutionId from CaseSolutions cs)  from Case c where c.id in:sIdList limit 1];
 
  for(Case c:cList)
  {
   userMap.put(c.OwnerId,c.Owner.Email);

   caseMap.put(c.OwnerId,c.CaseSolutions.SolutionId);
  }
 
  for(Solution s: Trigger.New)
  {
    Id sId = s.Id;
    Id oId = caseMap.get(sId);
    String emailId = userMap.get(oId);
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    String[] toAddresses = new String[] {emailId};
    mail.setToAddresses(toAddresses); 
     
  }
}
}
Thanks
Sam

 

paul-lmipaul-lmi

You code is going to fire when you create a Solution, not attach one to a Case.

 

If you're trying to trigger the email as soon as the solution is attached, you should be triggering on the CaseSolution object, not the Solution object.  CaseSolution is a union object between Case and Solution.

 

That said, I'm not sure if that allows triggers, so you'll have to check.  If so, the flow would be like this:

 

 

  1. Agent searches for solution
  2. Agent attaches solution
  3. Your code is fired
  4. You grab the Solution.Description and put it in the body of your email
  5. You set up the rest of your email based on a query of Case data to get the associated contact (this table only has two columns, case and solution id's)
  6. You send your email
If triggers aren't supported on CaseSolution, you can still achieve your goal, but it gets much more complicated.  You'd trigger on update of Solution, query CaseSolution for its ID to get the most recently created CaseSolution item, and then do 4-6 in the list above.

 

zettahertzzettahertz

I have a feeling that won't work.

You can't create a trigger on CaseSolution.

And I don't believe that a trigger on Solution will execute when a Solution is added into a Case.