+ Start a Discussion

When my custom Account-table saves, it needs to trigger a Insert on Contracts. How?

Hi guys,


When the user saves a new Account record, it should trigger an INSERT into the Contracts object. How would one do this in Apex code?


IU am completely new to Apex code. I know onecould use classes & triggers.

How would i call an insert into another table, passing selected fields?



The main flow is like this:

1. The ClientServiceRep (CSR) selects the New Account option.

2. The system displays the Accounts page allowing the CSR to enter a new Account.

3. The CSR enters the obligatory Account details into the provided fields.

4. The CSR selects the Save Account option.

5. The system checks for errors, having found none it proceeds to the next step.

6. The system creates a new Accounts record, storing the Account name, unique Account ReferenceID for later usage.

7. The system insert a new Account into the system database.

8. The system creates a new contract

9. The system assigns both the Contract.[Account name]  = Account.[Account name] and also assigns Contract.[ContractID] = Account.[Account ReferenceID].

10. The system insert a new Contract into the system database.

11. Having no errors the system proceeds to the next step.

12. The system commits both the new Account and the new Contract to the system database.

13. The system informs the CSR that a new account was created succesfully.

This is the end of this flow.



Any concrete examples on how to do the above would be GREATLY appreciated!






Hi there I am attaching a trigger of mine for your reference:-


trigger chkIns_Account on Account (before insert)
    if(Trigger.size == 1) // this is to escape bulk processing and avoid hitting governor limit
            Contact conTemp = [Select id, Name,Legal_Name__c  from Contact where accountid =:Trigger.new[0].id];
            if(conTemp.Contact.size() > 0) //if the account has some contact
                       Contract tempConc = new Contract();
                        tempConc.Accountid = Trigger.new[0].id;
                        tempConc.Client__c = conTemp.Legal_Name__c;
                        tempConc.Role__c = 'Advisor';
                        tempConc.Reverse_Role__c = 'Client';
                        insert tempConc;






Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.




  I would suggest to go for "after insert" trigger rather than "before insert". You wount get the Account Id for reference on Contract (not contact) if before insert is used.