Do triggers work for attachments object on insert event? - triggers

I have the requirement to calculate the number of attachments for custom object.
So I have written the trigger on attachment where as it was not working on Insert event. I want to check is insert event work on Attachments or not?

After Insert triggers are supported on Attachments and Notes. Please refer to this link:
https://help.salesforce.com/articleView?id=000181538&type=1
Below an example from the documentation link above:
trigger SetTitleToAttachment on Attachment (after insert) {
String Title;
Id pId;
for(Attachment att: Trigger.new){
Title=att.Name;
pId=att.ParentId;
}
List<Case> c=[select Id , Title__c from Case where Id=:pId];
//assuming one record is fetched.
c[0].Title__c=Title;
update c[0];
}

Okay so you're working with Files and not Attachments. Files work with ContentVersions, ContentDocuments and ContentDocumentLinks.
You want a trigger on a ContentVersion.

Related

Get SOST database ID of sent emails

I have an ABAP program that sends emails. A sent email is stored in SOOD table. After sending an email I would like to get some ID of the email to be able to check its status later (in SOST table). I have seen more functions/methods to send email (e.g. cl_bcs/send, SO_NEW_DOCUMENT_SEND_API1), but none of them returns any ID. Is there a reliable way to get it?
Function module SO_NEW_DOCUMENT_SEND_API1 create and export a new OBJECT_ID for every new message sent, As you can see in here -
This NEW_OBJECT_ID stored at BCST_SR table in SCOM_KEY field. From BCST_SR table you've to get DOC_OID, using DOC_OID you can get details from SOOD table. (Reference field in SOOD is - IF_DOC_BCS ) Then use the Object number OBJNO to get the details from SOST table.
Also you can refer t-code SBWP to check your mail status.
For class CL_BCS, you can check the send_request object's method doc_wrapper_id. This will return the sood structer.
Two other answers gave me together valuable clues to get it done (+1). But both missed some accuracy and code snippets, so I sum it all up in my answer.
using cl_bcs
DATA gr_send_request TYPE REF TO cl_bcs.
DATA emailid LIKE soodk.
gr_send_request = cl_bcs=>create_persistent( ).
" ...
CALL METHOD gr_send_request->send(EXPORTING i_with_error_screen = 'X'
RECEIVING result = gv_sent_to_all ).
IF gv_sent_to_all = 'X'.
emailid = gr_send_request->send_request->doc_wrapper_id( ).
ENDIF.
SOODK (not sood) is structure containing three components (OBJTP, OBJYR, OBJNO) which are together the key in SOOD table.
using SO_NEW_DOCUMENT_SEND_API1
DATA LT_OBJECTID TYPE SOFOLENTI1-OBJECT_ID.
CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
EXPORTING
DOCUMENT_DATA = LT_MAILSUBJECT
DOCUMENT_TYPE = 'HTM'
IMPORTING
new_object_id = lt_objectid
" ...
lt_objectid (SOFOLENTI1-OBJECT_ID) is char(17), that contains concatenated SOODK structure OBJTP+OBJYR+OBJNO. When divided to parts, it can be used to lookup a record in SOODK table. (I didn't find it in BCST_SR-SCOM_KEY, but it was not necessary.)

How to remove update date function in data file of mail merge?

I've inserted a date field in mail merge and set it to the automatically update. But when I dispatch the data file from the original mail merge the automatically update functionality of the date doesn't removed. But I want to remove it automatically as well as the user dispatch the data file from the mail merge.
Please give your kind suggestions to remove the automatically update functionality on date.
Nest the date field inside a { QUOTE } field, e.g.
{ QUOTE { DATE } }
Where both pairs of {} are the special field code brace pairs that you can insert in Windows Word using ctrl-F9.

MyBatis delete isn't working with multiple conditions

I am relatively new to MyBatis. If I use only one condition like below, I don't have any issues at all.
final String DELETE = "Delete from request where author=#{author}"; // Working
final String DELETE = "Delete from request where refid=#{referenceId}"; // working
But if I give two conditions for delete, rows are not getting deleted at all.
final String DELETE = "Delete from request where refid=#{referenceId} and author=#{author}"; // Not working.
And my interface looks like this,
#Delete(DELETE)
public void delete(Request request);
Could someone help me to understand what mistake I am doing in this? I am using MyBatis 3.1.1 and MySQL
Thanks,
I don't know, after changing the return type in the interface to Integer, things started working fine.
But as per the document, http://mybatis.github.io/mybatis-3/java-api.html, return value just indicates the number of rows affected.
#Delete(DELETE)
public Integer delete(InviteRequest request); // This made stuff working with two conditions.

Access 2010 Trying to Enter a unique record through Form

I'm trying to input an error check for my form. I have the user entering the name and I would like a prompt to inform them if they are attempting to use a Name already in the records.
Example: the Person table has 3 records with FNames being: Jeff, Kyle, Darren.
If on the add person form in the Fname Box Kyle is entered, the after update event will notify the user that this name has been claimed and null the field. Where as if Greg was enter no notifications will occur.
I just don't know how to compare a text field value to values in a filtered query list, and Google searches have other loosely related links in the way.
Thank you for help!
If all fnames must be unique, add a unique index to the table. This will prevent duplicates being entered. The form error property will allow you to provide a custom error.
You can also check if the name exists in the Before Update event of the control.
In this example, the control and field are both called AText. Generally, you should rename controls so they are not the same as fields.
Private Sub AText_BeforeUpdate(Cancel As Integer)
Dim IsOk As Boolean
''One of the very few places where the .Text property is used
sLookUp = Me.AText.Text
IsOk = IsNull(DLookup("Atext", "Table1", "Atext='" & sLookUp & "'"))
If Not IsOk Then
MsgBox "Found!"
Cancel = True
End If
End Sub

How to implement scheduler in Apex?

I see scheduler like feature in salesforce but it is somewhat tied with existing features that salesforce provide and no sample source code is provided as far as my research goes.
What I want to do is to create my own scheduler that sends simple email based on date.
Goal:
Custom Object Player has fields
startDate : date like '2010-11-01'
email : text field like foo#bar.com
name : player's name like John.
If today's date is one day before the startDate, I want to send email to the Player.
For instance, Player's name is John and if today is 2010-12-10 and one Player's startDate is set to 2010-12-11, email saying "hello John" is sent.
Unfortunately I cannot find good example or tutorial online or salesforce doc how to do this using Apex.
Could anyone point out where to get started?
UPDATE
I want to extend the answer from eyescream.
After setting scheduler, you can set what follow up action to take like sending email using template or set custom object fields to some values.
Below I found useful for people using email template in Visualforce format.
I have custom object 'alertTester' which has reference to other object 'custom' and even this object 'custom' has reference to another object 'custom1GrandChild' and all the relationship (up to 3 or 5 layers I think) can be accessed like below.
I've tested below and works fine. Now I'm receiving email with my condition set :)
<messaging:emailTemplate subject="Hello" recipientType="User" relatedToType="alertTester__c" >
<messaging:plainTextEmailBody >
{!relatedTo.name}
{!relatedTo.custom__r.name}
{!relatedTo.custom__r.custom1GrandChild__r.name}
</messaging:plainTextEmailBody>
</messaging:emailTemplate>
Check out solutions that don't involve code before you'll dive deeply to Apex...
Email Alert + Workflow Rule should provide you with all functionality you need in this scenario and involve just clicking without any code.
I'm answering to my own question again..
Below link, search for schedule
http://www.salesforce.com/us/developer/docs/apexcode/index.htm
Looks like Apex has Schedulable interface that I can implements and set up cron task.
Below is sample code provided in the doc:
global class TestScheduledApexFromTestMethod implements Schedulable {
// This test runs a scheduled job at midnight Sept. 3rd. 2022
public static String CRON_EXP = '0 0 0 3 9 ? 2022';
global void execute(SchedulableContext ctx) {
CronTrigger ct = [SELECT id, CronExpression, TimesTriggered, NextFireTime
FROM CronTrigger WHERE id = :ctx.getTriggerId()];
System.assertEquals(CRON_EXP, ct.CronExpression);
System.assertEquals(0, ct.TimesTriggered);
System.assertEquals('2022-09-03 00:00:00', String.valueOf(ct.NextFireTime));
Account a = [SELECT id, name FROM Account WHERE name =
'testScheduledApexFromTestMethod'];
a.name = 'testScheduledApexFromTestMethodUpdated';
update a;
}
}