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
george_dewaltgeorge_dewalt 

Contact-Creating Trigger : Where Did It Go?

Hi,

 

I've created a new custom object, called obj_Single_Account,

and added it a trigger that's supposed to create a new Contact_Object,

called obj_Cntct, of standard type Contact , every time upon user's

creating of a new obj_Single_Account object:

 

trigger trg_Contact_Create on obj_Single_Account__c (after insert) {

    List<Contact> obj_ContactList = new List<Contact>();
    
    for (obj_Single_Account__c obj_SA : Trigger.New) {
                
                Contact obj_Cntct = new Contact ();
                obj_Cntct.Description = obj_SA.txt_Company_Name__c;
                obj_Cntct.Phone=obj_SA.txt_Phone_Number__c;
        
                obj_ContactList.add(obj_Cntct);
    }

    try {
        insert obj_ContactList;
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
    

}

Now, I've also created some TEST_Class -- which, according to the

run_log seemed to have worked out correctly (i.e., w/o error),

but when I switched to USER_Mode interface (via my Internet browser)

and (as a USER) created a new obj_Single_Account, this new object

did NOT seem to have any CONTACT-type object attached.

 

Would anyone know why ?

 

Thanks

George

 

bob_buzzardbob_buzzard
I wouldn't expect this code to succeed as you haven't specified a contact name, via the FirstName and LastName fields. Name is a required field so you would receive a DML error on the insert.

As you are catching the error and just outputting debug, you are effectively swallowing the error. The code will appear to complete successfully even though the insert failed.
george_dewaltgeorge_dewalt

Capisci.

 

Will look into it.

 

Thanks

George

 

george_dewaltgeorge_dewalt

Attempt # 2 : Did NOT seem to work either :-(

 

I've fixed the code like so:

 
    trigger trg_Contact_Create on obj_Single_Account__c (after insert) {

    List<Contact> obj_ContactList = new List<Contact>();
    
    for (obj_Single_Account__c obj_SA : Trigger.New) {
                
                Contact obj_Cntct = new Contact ();
                obj_Cntct.FirstName = 'First';
                obj_Cntct.LastName = 'Last';
                obj_Cntct.Description = obj_SA.txt_Company_Name__c;
                obj_Cntct.Phone=obj_SA.txt_Phone_Number__c;
        
                obj_ContactList.add(obj_Cntct);
    }

    try {
        insert obj_ContactList;
    } finally { }
    
      // catch (system.Dmlexception e) {
      //  system.debug (e);
      // }
    
}

 

The TEST_Class returned a "success",

but I still could NOT find this supposedly-new Contact_Object

at either the obj_Single_Account object or the "Contacts_TAB"

as a user.

 

Any ideas ?

 

Thanks

George

 

 

 

 
bob_buzzardbob_buzzard

You haven't specified the obj_Single_Account__c that the contact belongs to anywhere.  You've copied the name and phone number, but you need to relate the objects to each other by filling in the lookup/master detail that relates this contact to the parent single account.

 

I don't think the contact will appear in the contacts tab because you haven't specified the AccountId field - this means its a private contact 

george_dewaltgeorge_dewalt

 

OK.

 

I've tried to add the following statements to my trigger - separately of course,

right before the other field-value assignments:

 

 

(1) First, I tried the VARIABLE name:

 

obj_Cntct.MasterRefFields = obj_Single_Account;

 

The compiler / syntax checker returned :

Error: Compile Error: Variable does not exist: obj_Single_Account at line 8 column 45

 

 

(2) Then, I thought, it might require the Custom name:

 

obj_Cntct.MasterRefFields = obj_Single_Account__c;

 

The compiler / syntax checker returned :

Error: Compile Error: Variable does not exist: obj_Single_Account__c at line 8 column 45

 

 

 (3) Next, I thought it might require quotation marks:

 

obj_Cntct.MasterRefFields = 'obj_Single_Account';

 

The compiler / syntax checker returned :

Error: Compile Error: Invalid field MasterRefFields for SObject Contact at line 8 column 17

 

 

 (4)  Thus, I thought, maybe the Custom name should be specified:

 

obj_Cntct.MasterRefFields = 'obj_Single_Account__c';

 

The compiler / syntax checker returned :

Error: Compile Error: Invalid field MasterRefFields for SObject Contact at line 8 column 17

 

Now, other online code examples have suggested I should use some sort of an ID field;

I assumed it'd be some unique number to be assigned by the internal database mechanism,

but I wouldn't know how to use it - should I simply copy it into some equivalent field in the

new-contact object ?

 

Puzzled,

George

 

 

 

  
bob_buzzardbob_buzzard

The instance of obj_Single_Account__c is in the iterator variable 'obj_SA'.  That will be the parent of the newly created contact.  So the code should be:

 

obj_Cntct.MasterRefFields = obj_SA;

However, I also think you'll need to use the __c notation for your MasterRefFields as its a custom field - e.g. 

 

obj_Cntct.MasterRefFields__c = obj_SA;