+ Start a Discussion

Part of Trigger to Create New Account not working

I'm trying to create a trigger that will fire when an opportunity is updated with a checkbox "create_hybrid__c" checked.  The trigger should create a new account record and then update a field on the opportunity with the new account.


I can get the trigger to create the new account, but any criteria that references the opportunity is ignored.  I've highlighted those lines in red below.  Also, I specifically assign a record type in the trigger, but the account is created with a completely different record type.  I also tried to assign a parent account and that criteria was ignored.


Is there any way to modify the trigger below so that all the fields I've specified are populated correctly?  With this trigger, an account is created, the name, agency_id__c, cmr_numer__c, and assign_rpm_client__c are populated correctly, the record type is not correct, and the other fields I specified are just left blank.


trigger CreateHybridAcct on Opportunity (before update) {


List<Account> a = new List<Account>();


for (Opportunity p : Trigger.new){
    if (p.create_hybrid__c == True) {
    a.add (new Account(
            name = 'testtrigger',
            agency_id__c = '300',
            cmr_number__c = '997',
            client_name__c = p.account.name,
            hybrid_account__c = p.account.id,
            owner = p.owner,
            recordtypeid = '01270000000UHZcAAO',
            parentid = '0017000000PgOvrAAF',
            assign_rpm_client__c = TRUE
    insert a;
    //p.hybrid_account__c = h.id;
    p.create_hybrid__c = false;     



Please write the trigger on after update event.


I tried that as well, which works fine for creating the account, but it wouldn't let me update the opportunity because the record was locked.  Do you know how to get around that problem?


After the account is created, I have to update a lookup field on the opportunity with the newly created account.




Rajesh ShahRajesh Shah

The code seems fine and should work. However, in this line "p.hybrid_account__c = h.id;", what is h?

hi rajesh, i have a small query, please provide your email id. 
thanks and regards ]\