+ Start a Discussion
LudivineLudivine 

Trigger create event issue

Hi,

I need to create an Event each time a contact had answered Yes to be contacted on a survey.
When the survey is created, the trigger should create an event for the Account Owner.

But I have an issue with the owner Id , someone can help me to correct it?

trigger EventForCustomerToContact on Amcor_Surveys__c (After insert ) {
For (Amcor_Surveys__c ams : trigger.new){
If(ams.Q6_NeedAmcorToContactYou__c == 'Yes'){
System.debug('Creating event record');
//create an event record
Event ev = new Event();
ev.RecordTypeId='012g00000004ajF';//'Visit Report VOC';
ev.Type ='VOC';

ev.Ownerid = ams.Account_ID__r.Owner.Id;

ev.whoId =ams.Contact_ID__c;
ev.Subject ='VOC Survey - Contact customer' ;
ev.objective__c = 'Improve Relationship';
//System.debug('************ ' + system.today().format());
//System.debug('************ ' + system.today());Date startDate = system.today();
ev.ActivityDate = startDate;Date endDate = startDate.adddays(5);
ev.RecurrenceStartDateTime = Datetime.newInstanceGmt(startDate.year(), startDate.month(), startDate.day(), 0, 0, 0);
ev.RecurrenceEndDateOnly = endDate;
System.debug('Attempting to insert...');
try{
insert ev;
}
catch(Exception e)
{  
 System.debug(e.getMessage()); 
 }}}}

When I create a survey I get this error message on my survey form :
Error: Invalid Data.
Review all error messages below to correct your data.
Please select a value

And In my debug log : INVALID_CROSS_REFERENCE_KEY, Assigned To ID: owner cannot be blank: [OwnerId]

I paste the result :

31.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
17:02:25.173 (173947493)|EXECUTION_STARTED
17:02:25.173 (173982146)|CODE_UNIT_STARTED|[EXTERNAL]|01qg00000008kvE|EventForCustomerToContact on Amcor_Surveys trigger event AfterInsert for [a0xg0000007OB0F]
17:02:25.175 (175163438)|SYSTEM_METHOD_ENTRY|[4]|LIST<Amcor_Surveys__c>.iterator()
17:02:25.175 (175345821)|SYSTEM_METHOD_EXIT|[4]|LIST<Amcor_Surveys__c>.iterator()
17:02:25.175 (175384554)|SYSTEM_METHOD_ENTRY|[4]|system.ListIterator.hasNext()
17:02:25.175 (175420796)|SYSTEM_METHOD_EXIT|[4]|system.ListIterator.hasNext()
17:02:25.175 (175550722)|SYSTEM_METHOD_ENTRY|[9]|System.debug(ANY)
17:02:25.175 (175578523)|USER_DEBUG|[9]|DEBUG|Creating event record
17:02:25.175 (175586115)|SYSTEM_METHOD_EXIT|[9]|System.debug(ANY)
17:02:25.178 (178295195)|SYSTEM_METHOD_ENTRY|[23]|System.today()
17:02:25.178 (178331688)|SYSTEM_METHOD_EXIT|[23]|System.today()
17:02:25.178 (178403191)|SYSTEM_METHOD_ENTRY|[27]|com.salesforce.api.interop.apex.bcl.DateMethods.addDays(Integer)
17:02:25.178 (178448078)|SYSTEM_METHOD_EXIT|[27]|com.salesforce.api.interop.apex.bcl.DateMethods.addDays(Integer)
17:02:25.178 (178465076)|SYSTEM_METHOD_ENTRY|[29]|com.salesforce.api.interop.apex.bcl.DateMethods.year()
17:02:25.178 (178487761)|SYSTEM_METHOD_EXIT|[29]|com.salesforce.api.interop.apex.bcl.DateMethods.year()
17:02:25.178 (178500453)|SYSTEM_METHOD_ENTRY|[29]|com.salesforce.api.interop.apex.bcl.DateMethods.month()
17:02:25.178 (178512317)|SYSTEM_METHOD_EXIT|[29]|com.salesforce.api.interop.apex.bcl.DateMethods.month()
17:02:25.178 (178523198)|SYSTEM_METHOD_ENTRY|[29]|com.salesforce.api.interop.apex.bcl.DateMethods.day()
17:02:25.178 (178539286)|SYSTEM_METHOD_EXIT|[29]|com.salesforce.api.interop.apex.bcl.DateMethods.day()
17:02:25.178 (178564852)|SYSTEM_METHOD_ENTRY|[29]|com.salesforce.api.interop.apex.bcl.DatetimeMethods.newInstanceGmt(Integer, Integer, Integer, Integer, Integer, Integer)
17:02:25.178 (178595978)|SYSTEM_METHOD_EXIT|[29]|com.salesforce.api.interop.apex.bcl.DatetimeMethods.newInstanceGmt(Integer, Integer, Integer, Integer, Integer, Integer)
17:02:25.178 (178665508)|SYSTEM_METHOD_ENTRY|[32]|System.debug(ANY)
17:02:25.178 (178687579)|USER_DEBUG|[32]|DEBUG|Attempting to insert...
17:02:25.178 (178694695)|SYSTEM_METHOD_EXIT|[32]|System.debug(ANY)
17:02:25.178 (178749289)|DML_BEGIN|[35]|Op:Insert|Type:Event|Rows:1
17:02:25.183 (183792416)|DML_END|[35]
17:02:25.183 (183935547)|EXCEPTION_THROWN|[35]|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Assigned To ID: owner cannot be blank: [OwnerId]
17:02:25.185 (185534938)|SYSTEM_METHOD_ENTRY|[41]|System.DmlException.getMessage()
17:02:25.185 (185575133)|SYSTEM_METHOD_EXIT|[41]|System.DmlException.getMessage()
17:02:25.185 (185587600)|SYSTEM_METHOD_ENTRY|[41]|System.debug(ANY)
17:02:25.185 (185595912)|USER_DEBUG|[41]|DEBUG|Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Assigned To ID: owner cannot be blank: [OwnerId]
17:02:25.185 (185602578)|SYSTEM_METHOD_EXIT|[41]|System.debug(ANY)
17:02:25.185 (185616339)|SYSTEM_METHOD_ENTRY|[4]|system.ListIterator.hasNext()
17:02:25.185 (185631370)|SYSTEM_METHOD_EXIT|[4]|system.ListIterator.hasNext()
17:02:25.729 (185664470)|CUMULATIVE_LIMIT_USAGE
17:02:25.729|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 10
  Number of Mobile Apex push calls: 0 out of 10


Thanks!!
Best Answer chosen by Ludivine
Tony TannousTony Tannous
Dear,

you can't access Account_ID__r.OwnerId directly. but you need to do a select

also i did some changes for the creation so it support bulk insert without getting DML exception using insert for a list<Event>

trigger EventForCustomerToContact on Amcor_Surveys__c (After insert )
{
     set<id> setamcorIds= new set<id>();
For (Amcor_Surveys__c ams : trigger.new)
{
  If(ams.Q6_NeedAmcorToContactYou__c == 'Yes')
  {
     setamcorIds.add(ams.Id);
  }
}

list<Amcor_Surveys__c> listSelectedAmcor= [select Account_ID__r.OwnerId,Contact_ID__c
                                            from  Amcor_Surveys__c
              where id in :setamcorIds];
System.debug('Creating event record');

list<Event> listEventToCreate= new list<Event> ();
for(Amcor_Surveys__c ams :listSelectedAmcor)
{
  //create an event record
  Event ev = new Event();
  ev.RecordTypeId='012g00000004ajF';//'Visit Report VOC';
  ev.Type ='VOC';
  ev.Ownerid = ams.Account_ID__r.OwnerId;
  ev.whoId =ams.Contact_ID__c;
  ev.Subject ='VOC Survey - Contact customer' ;
  ev.objective__c = 'Improve Relationship';
  //System.debug('************ ' + system.today().format());
  //System.debug('************ ' + system.today());Date startDate = system.today();
  ev.ActivityDate = startDate;Date endDate = startDate.adddays(5);
  ev.RecurrenceStartDateTime = Datetime.newInstanceGmt(startDate.year(), startDate.month(), startDate.day(), 0, 0, 0);
  ev.RecurrenceEndDateOnly = endDate;
  System.debug('Attempting to insert...');
  listEventToCreate.add(ams);
    }
try{
insert listEventToCreate;
}
catch(Exception e)
{
  System.debug(e.getMessage());
  }
}

Good Luck

All Answers

Ramu_SFDCRamu_SFDC
I suspect the problem could be in line # 10 ev.Ownerid = ams.Account_ID__r.Owner.Id;

Please try changing this to ev.Ownerid = ams.Account_ID__r.OwnerId; and see if this works .
Tony TannousTony Tannous
Dear,

you can't access Account_ID__r.OwnerId directly. but you need to do a select

also i did some changes for the creation so it support bulk insert without getting DML exception using insert for a list<Event>

trigger EventForCustomerToContact on Amcor_Surveys__c (After insert )
{
     set<id> setamcorIds= new set<id>();
For (Amcor_Surveys__c ams : trigger.new)
{
  If(ams.Q6_NeedAmcorToContactYou__c == 'Yes')
  {
     setamcorIds.add(ams.Id);
  }
}

list<Amcor_Surveys__c> listSelectedAmcor= [select Account_ID__r.OwnerId,Contact_ID__c
                                            from  Amcor_Surveys__c
              where id in :setamcorIds];
System.debug('Creating event record');

list<Event> listEventToCreate= new list<Event> ();
for(Amcor_Surveys__c ams :listSelectedAmcor)
{
  //create an event record
  Event ev = new Event();
  ev.RecordTypeId='012g00000004ajF';//'Visit Report VOC';
  ev.Type ='VOC';
  ev.Ownerid = ams.Account_ID__r.OwnerId;
  ev.whoId =ams.Contact_ID__c;
  ev.Subject ='VOC Survey - Contact customer' ;
  ev.objective__c = 'Improve Relationship';
  //System.debug('************ ' + system.today().format());
  //System.debug('************ ' + system.today());Date startDate = system.today();
  ev.ActivityDate = startDate;Date endDate = startDate.adddays(5);
  ev.RecurrenceStartDateTime = Datetime.newInstanceGmt(startDate.year(), startDate.month(), startDate.day(), 0, 0, 0);
  ev.RecurrenceEndDateOnly = endDate;
  System.debug('Attempting to insert...');
  listEventToCreate.add(ams);
    }
try{
insert listEventToCreate;
}
catch(Exception e)
{
  System.debug(e.getMessage());
  }
}

Good Luck
This was selected as the best answer
LudivineLudivine
Hi Tony,

Thanks, I have pasted your code , now I can't save.

Error: Compile Error: Incompatible element type SOBJECT:Amcor_Surveys__c for collection of SOBJECT:Event at line 36 column 3

this line is :  listEventToCreate.add(ams);
(after system.debug('Attempting to insert...')

What I did wrong?
Tony TannousTony Tannous
sorry it should be listEventToCreate.add(ev);

regards
LudivineLudivine
Tony,

You are a star, it works! my event is created, many many thanks!!!