1

Can some one please help me on this?

My requirement:

Once child account is created, it's respective (parent) account should also be created using trigger. The parent account's name should be "XXX account name"(a custom field on the child account) and it's key is "XXX account name-GSS" (another custom field on the child account).

I m new to triggers. I have created a trigger and parent account is also created, but the problem is that on " Child - Account mapping ", on Child account, "Parent" lookup is not being populated.

My Code:

for(Account ac: trigger.new) {

        newAccountName = ac.XXX_Account_Name__c ;
        newAccountKey = ac.XXX_Account_Name__c +'-'+ac.GSS__c;
        system.debug('---New Account Key --'+newAccountKey );

        acc = new Account();
        acc.name = newAccountName ;
        acc.Account_Key__c = newAccountKey ;

        accMap.put(ac.account_key__c,acc.account_key__c);

        accList.add(acc);

}
upsert accList;
  • Not clear.Please put more code .Whats child and parent?Is both of them Accounts.Also whats your event ?After insert or before insert or after update or before update?? – Mohith Shrivastava Nov 17 '12 at 6:52
  • Thanks Mohith for your quick response. As of now i am loading(using data loader or manually) only child account. and then it's parent account should create automatically. The data of Parent is as i mentioned in my question. – Sandeep_Dobariya Nov 17 '12 at 7:20
  • The event can be any thing, at the end i want my parent account created once i upload child account with mapping(In the child account field the parent field should having reference(look-up) to that created parent account) – Sandeep_Dobariya Nov 17 '12 at 7:24
  • You want child record to create Parent record?Parent has to be first created and then only you will get Ids to fill in lookup of child records – Mohith Shrivastava Nov 17 '12 at 7:30
  • Other way is using external Id and upserting the child records based on external Id of the Parent – Mohith Shrivastava Nov 17 '12 at 7:32
3

For starters I'm assuming this is an After Trigger. Consider using a static variable, so that the same trigger does not get invoked twice.

You are creating the Parent Account alright, all you need is for the Parent lookup to be set on the child.

trigger AccountAfter on Account(After Insert){

Map<Id, Account> childToParentAccountMap = new Map<Id, Account>{};

for(Account ac: trigger.new) {

        String newAccountName = ac.XXX_Account_Name__c ;
        String newAccountKey = ac.XXX_Account_Name__c +'-'+ac.GSS__c;
        system.debug('---New Account Key --'+newAccountKey );

        Account acc = new Account();
        acc.name = newAccountName ;
        acc.Account_Key__c = newAccountKey ;

        childToParentAccountMap.put(ac.Id, acc); //add to map child Id - Parent Acc

}

if(!childToParentAccountMap.isEmpty())
Database.upsert(childToParentAccountMap.values());

//Now that the parent Accounts have been created, use the Ids to set Parent on Child Accts
List<Account> childAccts = new List<Account>{};
for(Id accId : childToParentAccountMap.keySet())
childAccts.add(new Account(Id=accId, ParentId = childToParentAccountMap.get(accId).Id));

if(!childAccts.isEmpty())
Database.update(childAccts);

}
| improve this answer | |
  • +1 Looks good since code makes it clear while mine was verbal and for starters they need code – Mohith Shrivastava Nov 17 '12 at 8:18
  • Main issue i am facing is, if i am using "After insert" then i can not be able to update(Can't do mapping) that created account and if i am using "before insert" then i can not be able to create parent account. – Sandeep_Dobariya Nov 17 '12 at 8:59
  • Since now my issue is resolved, i have one Dummy account, and created trigger on that dummy account, now i can be able to update other object(Account) using that trigger. That is the only solution of this problem i think so. – Sandeep_Dobariya Nov 17 '12 at 9:01
  • @techtrekker can you maybe improve this code snippet a bit :) new Map<Id, Account){}; won't work, variables without types (Strings I believe). I know the source was like that but still we could make it a bit more reusable? – eyescream Nov 17 '12 at 11:57
  • How do you mean, just mistyped > as ) – techtrekker Nov 17 '12 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.