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
AussieBattlerAussieBattler 

Problem with an Insert and Update Trigger

Hi. I have created a custom object (Enquiry__c) and when this object is created or updated I want it to create or update a Contact record. I have added a custom lookup field to the Contact object that captures the Enquiry__c unique ID.

I have got the creating part of the trigger working but am struggling on the update. If I try to update an Enquiry__c record I get an error message "MISSING_ARGUMENT, Id not specified in an update call". I realise I must need to locate the ID for the relevant Contact record but have not worked out how to do this. Following is my code:

Code:
trigger enquiryInsertContact on Enquiry__c (after insert, before update) {
List<Contact> contacts = new Contact[0];
if (Trigger.isInsert) {
 for (Enquiry__c enq : Trigger.new) {
  contacts.add(new Contact (FirstName = enq.Enquiry_First_Name__c,
     LastName = enq.Enquiry_Surname__c,
     Salutation = enq.Salutation__c));
  }
 insert contacts;
 }
else if (Trigger.isUpdate) {
 for (Enquiry__c enq : Trigger.new) {
  contacts.add(new Contact (FirstName = enq.Enquiry_First_Name__c));
  }
 update contacts;
 }
}

 If anybody coudl point me in the right direction it would be greatly appreciated.

Cheers.

Best Answer chosen by Admin (Salesforce Developers) 
BunnyBoyBunnyBoy
You need a SOQL call to retrieve the related contact Ids.  Within your else-clause for-loop:

String id = enq.Id;
Contact c = [select Id from contact where Enquiry__c = :id limit 1];
< ... perform your field updates to the contact >
contacts.add ( c );
...
update contacts;

For batch updates, though, this approach will quickly exceed the governor limits for SOQL calls.  You will likely need to restructure this so that you can retrieve all the contact Ids in a single SOQL call.  Psuedo-code ....

List<ID> ids = new List<ID>();
for (Enquiry__c enq : Trigger.new) {
      ids.add( enq.Id );
}        
contacts = [select Id < and any other required fields > from contact where Enquiry__c in :ids];
< ... loop to perform appropriate updates to all records  >
update contacts;


Good luck.

All Answers

BunnyBoyBunnyBoy
You need a SOQL call to retrieve the related contact Ids.  Within your else-clause for-loop:

String id = enq.Id;
Contact c = [select Id from contact where Enquiry__c = :id limit 1];
< ... perform your field updates to the contact >
contacts.add ( c );
...
update contacts;

For batch updates, though, this approach will quickly exceed the governor limits for SOQL calls.  You will likely need to restructure this so that you can retrieve all the contact Ids in a single SOQL call.  Psuedo-code ....

List<ID> ids = new List<ID>();
for (Enquiry__c enq : Trigger.new) {
      ids.add( enq.Id );
}        
contacts = [select Id < and any other required fields > from contact where Enquiry__c in :ids];
< ... loop to perform appropriate updates to all records  >
update contacts;


Good luck.

This was selected as the best answer
AussieBattlerAussieBattler
Thanks. That did the trick.