1

This is what I have:

trigger ComplaintInitialize on Complaints__c (before insert, before update)
{
  for(Complaints__c Complaint : Trigger.new)
  {

    if(Complaint.RecordType_Name__c == 'ComplaintInitialize')
    {
       Id recordTypeId = Schema.SObjectType.Complaints__c.getRecordTypeInfosByDeveloperName().get('ComplaintOffice').getRecordTypeId();
       If(String.isBlank(Complaint.Case__c) == false)
       {
          Case C = [Select Id, OwnerId, ContactId, Intermediary__c, Polisnummer_lookup__c, Account.FirstName, Account.LastName, 
                    Contact.First_Name__c, Contact.LastName, Contact.MailingStreet, Contact.MailingPostalCode, Contact.MailingCity, 
                    Contact.Gender__c, Contact.Phone, Contact.Email from Case Where Id =: Complaint.Case__c ];
          Complaint.Employee__c = C.OwnerId;
          User KB = [select Id from User where LastName = :'ComplaintOffice'];
          Complaint.OwnerID = KB.id;
          User H = [select Id, Email, Department, Division from User where Id =: C.OwnerId];
          User TL = [Select Id from User Where Department =: H.Department AND IsActive =: true AND Function__c =: 'Complaint_Handler' LIMIT 1];
          Complaint.Complaint_Handler__c = TL.Id;

so far so good

but here I got stuck:

Illegal assignment from List to Complaints__c

at line....

      Complaints__c DT = [Select Id, Date__c, FirstContactDate__c, FirstContactDateAAV__c from PeriodOfTime__c Where Date__c =: date.today() LIMIT 1];
      If(Complaint.Received_by__c == 'Complaint coordinator AAV')
          {Complaint.Deadline__c = DT.FirstContactDateAAV__c;}
      else
          {Complaint.Deadline__c = DT.FirstContactDate__c;}

What am I doing wrong in the last part? (The 'Limit 1' is not necessary because there is only 1 record in the database with date of today)

3
  • 1
    Welcome to SFSE! Please take a look at How to Ask. Right now, your question isn't clear. There's a lot of text (and what looks like multiple questions), and digesting it is difficult. Reading up on how to format would help a little. The purpose of this site isn't to be a free-coding service. Showing us what you've tried so far, and pointing to a single, specific thing that you're stuck on (e.g. why am I getting error X?, How can I avoid a nested loop at this part?, etc...) is what this site works best with.
    – Derek F
    Commented Jan 5, 2020 at 13:22
  • 2
    If you're new to Apex (or really any part of the Salesforce platform), then trailhead.salesforce.com is going to be a better resource than this site is. The tutorial and sandbox approach is just simply better for learning the basics than this site's Q&A approach is.
    – Derek F
    Commented Jan 5, 2020 at 13:25
  • 1
    Note that queries on custom objects do work the same as queries on standard objects. If after researching and going through the relevant Trailheads you are still stuck, post a specific question with a small section of the exact code you are using and an explanation of what you expect and what you see, quoting any error messages exactly. Also, use the {} button to format code you post.
    – Keith C
    Commented Jan 5, 2020 at 13:50

1 Answer 1

4

Since you're new to Salesforce, I'd recommend that you ditch this approach. It's highly inefficient and manual. Instead, you can use BusinessHours and Custom Metadata to provide a configurable setting for your trigger.

BusinessHours

In Setup, create a new Business Hours setting. You can just specify the start and end times as 12:00AM on each end to make your calculations easier.

Holidays

You can specify holidays for your Business Hours--this will allow you to skip days off using the BusinessHours library.

Custom Metadata

Create a Custom Metadata object with a single custom field, Value__c that specifies the number of business days to calculate.

Trigger

Now, for each record, you can simply query your Custom Metadata for each record, use the BusinessHours library to calculate the appropriate date, and then assign the result to the custom field. That code would look like:

trigger on Complaints__c (before insert) {
  BusinessHours complaintHours = [SELECT Id FROM BusinessHours WHERE Name='Complaint Deadlines'];
  for(Complaints__c record: Trigger.new) {
      Deadline__mdt[] deadline = [SELECT Value__c FROM Deadline__c WHERE Name = :record.Option__c];
      if(deadline.size() == 1) {
          record.Deadline__c = BusinessHours.add(complaintHours.Id, DateTime.newInstance(Date.today(), Time.newInstance(0,0,0,0)), 86400000).date();
      }
   }
}

This may not be exactly correct, so some changes may be necessary. The documentation should help you out here.

2
  • Thanx, I didn't think of that yet. I will look into it. Commented Jan 5, 2020 at 14:53
  • I found the businesshours and holiday object (and filled the holiday object) and will start working on this. I still wonder what went wrong in the first approach. Commented Jan 6, 2020 at 12:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .