1

Send email and include only fields with value

Hello.
I do want to send service reports to customers. I have a code which works fine (see below) but I would like to exclude fields from the text in the body of the email, if they don't have any value.
Can anyone be so kind and tell me how to achieve this?

Fields which can be empty are
Service Technician
Service requested by
On-Site Hours
Call-Out hours
Off-Site hours
Remote Incidents
RMM hours
Overtime hours

Thanks for any help.

let xCogDate := 'Service Date';
'Service Date' := date(year(xCogDate), month(xCogDate), day(xCogDate) + 1);
let mySigName := userName(user());
sendEmail({
    from: userEmail(user()),
    to: Recipients,
    subject: "Service Report " + fdate,
    text: " " + "
" + 'Service Type' + ": " + 'Service Date' + "
Service Priority: " + 'Service Priority' + "
Technician: " + 'Service Technician' + "
Requested by: " + 'Service requested by' + "
Maintenance Hours: " + 'On-Site Hours' + "
Call-Out Hours: " + 'Call-Out hours' + "
Off-Site Hours: " + 'Off-Site hours' + "
Remote Incidents: " + 'Remote Incidents' + "
RMM Hours: " + 'RMM hours' + "
Overtime Hours: " + 'Overtime hours' + "

" + Report + "
"
});
'Date Sent' := now()

3 replies

null
    • Fred
    • 1 yr ago
    • Reported - view

    Since fields are involved, I don't think there is any easy way around using a bunch of if statements. Then you will have to play around with formatting, by adding spaces or carriage returns at the end or the variable or in the body.

    let xCogDate := 'Service Date';
    'Service Date' := date(year(xCogDate), month(xCogDate), day(xCogDate) + 1);
    let mySigName := userName(user());
    let SDcheck := if 'Service Date' != null then
        'Service Type' + ": " + 'Service Date'
    end;
    {keep adding new variables for each field to check}
    sendEmail({
        from: userEmail(user()),
        to: Recipients,
        subject: "Service Report " + fdate,
        text: " " + "
    " + SDcheck +
    Service Priority: " + 'Service Priority' + "
    Technician: " + 'Service Technician' + "
    Requested by: " + 'Service requested by' + "
    Maintenance Hours: " + 'On-Site Hours' + "
    Call-Out Hours: " + 'Call-Out hours' + "
    Off-Site Hours: " + 'Off-Site hours' + "
    Remote Incidents: " + 'Remote Incidents' + "
    RMM Hours: " + 'RMM hours' + "
    Overtime Hours: " + 'Overtime hours' + "
    
    " + Report + "
    "
    });
    'Date Sent' := now()
    
    • NetSol Co., Ltd.
    • Bernd_Krell
    • 1 yr ago
    • Reported - view

    Fred Thanks a lot for this. Great help.
    I used the code as below, it does work well but have one questions.
    If one of the fields is empty, it's not shown in the email which is good. But it creates an empty line. (Applies currently to check6, check7 and check8).
    With 2 or 3 empty lines between, it looks odd. Any way to prevent this?

    let xCogDate := 'Service Date';
    'Service Date' := date(year(xCogDate), month(xCogDate), day(xCogDate) + 1);
    let mySigName := userName(user());
    let check1 := if 'Call-Out hours' != null then
            "Call-Out Hours: " + 'Call-Out hours'
        end;
    let check2 := if 'On-Site Hours' != null then
            "Maintenance Hours: " + 'On-Site Hours'
        end;
    let check3 := if 'Off-Site hours' != null then
            "Off-Site Hours: " + 'Off-Site hours'
        end;
    let check4 := if 'Remote Incidents' != null then
            "Remote Incidents: " + 'Remote Incidents'
        end;
    let check5 := if 'RMM hours' != null then
            "RMM Hours: " + 'RMM hours'
        end;
    let check6 := if 'Service Priority' != null then
            "Service Priority: " + 'Service Priority'
        end;
    let check7 := if 'Service requested by' != null then
            "Service requested by: " + 'Service requested by'
        end;
    let check8 := if 'Overtime hours' != null then
            "Overtime Hours: " + 'Overtime hours'
        end;
    sendEmail({
        from: userEmail(user()),
        to: Recipients,
        subject: "Service Report " + fdate,
        text: check1 + check2 + check3 + check4 + check5 + "
    " + check8 + "
    Technician: " + 'Service Technician' + "
    " + check7 + "
    " + check6 + "
    
    " + Report + "
    "
    });
    'Date Sent' := now()
    
    • Fred
    • 1 yr ago
    • Reported - view
    Bernd Krell said:
    With 2 or 3 empty lines between, it looks odd. Any way to prevent this?

     I would say to play around with putting the carriage returns in the check formulas, so they only show up when needed.

Content aside

  • 1 Likes
  • 1 yr agoLast active
  • 3Replies
  • 87Views
  • 2 Following