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
kevinjia1984kevinjia1984 

Question about after insert and before insert

Hi all,

 

My customer wants me to create a trigger fired on contact object, this trigger will create a relationship object related to this new created contact record and get some field values from contact. After inserting the contact object, I need to blank out three fields of this contact object and the relationship should get this contactId

 

My problem is, when I set this trigger before insert. The blank out code part works fine, but I cannot get the contactId in the relationship object for this trigger fired before insert.

 

And then I try to set the trigger after insert, it gives me some error message said that fields is read only, so I cannot get the fields to be blank out successfully.

 

Is there any way to meet both requirements ie. 1. Realtionship object should obtain the contactId 2. In the contact, field should be blank out. 

 

I have highlight the parts which will cause some problems. Thanks in advance for any answer

 

trigger ContactRelationshipTrigger on Contact (before insert) {

 

for(Contact CON : Trigger.New){

 

//A new Relationships__c record will be created

 Relationships__c relation = new Relationships__c();

 

 //Make it related to the Account which name is in the List Owner

Account ac = [SELECT Id, Name FROM Account WHERE Name =: CON.List_Owner__c];

relation.List_Owner__c = ac.Id;

relation.Contact__c = CON.Id;

 

 //Get the values in both List Id and opt-in of the contact record

relation.List_ID__c = CON.List_ID__C;

relation.List_Opt_In__c = CON.List_Opt_In__c;

 

insert relation;

 

 

CON.Relationship__c = relation.Id;

 //Blank out the three string field in the Contact

CON.List_Owner__c = '';

CON.List_ID__c = '';

CON.List_Opt_In__c = '';

 

}

}

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

I think you'll need two triggers to do this correctly. Here's how it looks:

 

 

trigger test1c on Contact (after insert, before update) {
    if(Trigger.isUpdate) {
        for(Contact c:Trigger.new)
            c.Killme__c = null;
    }
    if(Trigger.isInsert) {
        List<Test1__c> tests = new List<Test1__c>();
        for(Contact c:Trigger.new) {
            tests.add(new test1__c(name=c.lastname,contact__c=c.id));
        }
        insert tests;
    }
}

 

trigger test2c on Test1__c (after insert) {
    List<Contact> contacts = new list<contact>();
    for(Test1__c t:trigger.new)
        contacts.add(new contact(id=t.contact__c,test1__c=t.id));
    update contacts;
}

So, our first trigger creates the new relationships in an "after-insert" manner, which calls the second trigger, which then calls the frst trigger a second time, where we blank out the fields.

 

 

You could, of course, make this three triggers, but I rolled it into two for simplicity.

 

 

Also note that you can't put your query inside a for loop; you need to do the query outside the loop, and then inside the loop reference the results of the query.

All Answers

sfdcfoxsfdcfox

I think you'll need two triggers to do this correctly. Here's how it looks:

 

 

trigger test1c on Contact (after insert, before update) {
    if(Trigger.isUpdate) {
        for(Contact c:Trigger.new)
            c.Killme__c = null;
    }
    if(Trigger.isInsert) {
        List<Test1__c> tests = new List<Test1__c>();
        for(Contact c:Trigger.new) {
            tests.add(new test1__c(name=c.lastname,contact__c=c.id));
        }
        insert tests;
    }
}

 

trigger test2c on Test1__c (after insert) {
    List<Contact> contacts = new list<contact>();
    for(Test1__c t:trigger.new)
        contacts.add(new contact(id=t.contact__c,test1__c=t.id));
    update contacts;
}

So, our first trigger creates the new relationships in an "after-insert" manner, which calls the second trigger, which then calls the frst trigger a second time, where we blank out the fields.

 

 

You could, of course, make this three triggers, but I rolled it into two for simplicity.

 

 

Also note that you can't put your query inside a for loop; you need to do the query outside the loop, and then inside the loop reference the results of the query.

This was selected as the best answer
kevinjia1984kevinjia1984

Great!! Thanks a lot