+ Start a Discussion
Shawn Reichner 29Shawn Reichner 29 

Create Opportunity for Child Accounts when child created using info from parent account opportunity

I have a process that based on checkboxes on the Account object when the Account is created, it will create a child account for the Parent account being created.  This will usually be done during the lead conversion process where the Account gets created and based on the checkboxes selected on the lead the approprate Child Accounts will be created.  Now here is my question.....When the lead conversion is successful and the opportunity is created for the parent account, I would like a trigger to based on the two check boxes on the Parent account (Anywhere or Complete) to create an Opportunity using the same field data from the opp created by the lead conversion for each child account created with a small tweak to the name field and re-pointing the Account ID on the new child opps to point to thier correct Child account again base don the proper checkbox selected and the trigger process.   Can this happen?   If so, can you give me a head start as I woudl like to get this automated and I know we can do this, but I have no idea. 

Please be my saving hero!!!!

Thanks in advance,

Shawn
sandeep@Salesforcesandeep@Salesforce
Hi Shawn,

This is possible. All you need to do is write a trigger on Account (Event after insert) ( please apply condition in trigger that parent ID is not null) to create opportunity (populating child account's id on opportunity along with tweaking opportunity name).

Please mark this best answer if it was helpful for you to resolve your issue. 

Thanks
Sandeep Singhal
http://www.codespokes.com/
Shawn Reichner 29Shawn Reichner 29
Sandeep, first thank you sir for your reply.  This is what I am attempting, although I constantly get a List has no rows for Assignment.  Below is my Trigger can you see how I can resolve the no row error sir?
 
trigger CreateOppForChildAccount on Account (After insert) {

    List<Opportunity> AnywhereOpp = new List<Opportunity>();
    List<Opportunity> CompleteOpp = new List<Opportunity>();
    
    For(Account a : Trigger.new){
        
        If(!String.isBlank(a.ParentId) && a.ChildTriggerRan__c == True){
            
            Opportunity ParentOpp = [SELECT ID, AccountId FROM Opportunity WHERE AccountId =: a.ParentId LIMIT 1];
            System.debug('Parent Opp is - '+ParentOpp.Name);
            
                AnywhereOpp.add(new Opportunity(name = ParentOpp.Name + '_Anywhere',
                                               AccountId = a.Id,
                                               Vms__c = ParentOpp.VMs__c,
                                               Additional_Customer_Project_Comments__c = ParentOpp.Additional_Customer_Project_Comments__c,
                                               Armor_Anywhere__c = ParentOpp.Armor_Anywhere__c,
                                               Compliance_Regulation_Concerns__c = ParentOpp.Compliance_Regulation_Concerns__c,
                                               Contains_cardholder_data__c = ParentOpp.Contains_cardholder_data__c,
                                               Contains_ePHi__c = ParentOpp.Contains_ePHi__c,
                                               DF_MQL_Source__c = ParentOpp.DF_MQL_Source__c,
                                               DF_SAL_Source__c = ParentOpp.DF_SAL_Source__c,
                                               Expected_Close_Date__c = ParentOpp.Expected_Close_Date__c,
                                               General_Security__c = ParentOpp.General_Security__c,
                                               HIPAA__c = ParentOpp.HIPAA__c,
                                               Partner_Account__c = ParentOpp.Partner_Account__c,
                                               Partner_Submitted__c = ParentOpp.Partner_Submitted__c,
                                               PCI__c = ParentOpp.PCI__c,
                                               Submitting_Partner_User__c = ParentOpp.Submitting_Partner_User__c));
                
            }
            
        }
        
    Insert AnywhereOpp;
    
    }

Thanks again,

Shawn
Shawn Reichner 29Shawn Reichner 29
Sorry the error populates for line #10 -  Opportunity ParentOpp = [SELECT ID, AccountId FROM Opportunity WHERE AccountId =: a.ParentId LIMIT 1];
sandeep@Salesforcesandeep@Salesforce
Hi Shawn, 
Please replace your line #10 as below 


Opportunity[] ParentOpplist = [SELECT ID, AccountId FROM Opportunity WHERE AccountId =: a.ParentId LIMIT 1];
Opportunity ParentOpp = new Opportunity();
if (ParentOpplist .size() > 0)
  ParentOpp  = ParentOpplist [0].Id;

but also try to find reason why this soql does not has any record to return.

Please mark this best answer if it was helpful for you to resolve your issue. 

Thanks
Sandeep Singhal
http://www.codespokes.com/
Shawn Reichner 29Shawn Reichner 29
Thanks again. I will try this and alert you of the findings. I ran the logs and can not find a reason why the list would be empty. When I create the account via lead conversion the opp on the master account gets created. 
Shawn Reichner 29Shawn Reichner 29
Sandeep, I made the changes to my code base don your advice, and now I get an error "Illegal Assignment from ID to Opportunity" for this line - ParentOpp = ParentOppList[0].Id;   Any thoughts?
sandeep@Salesforcesandeep@Salesforce
Please change as below 
if (ParentOpplist .size() > 0)
  ParentOpp  = ParentOpplist [0];

Thanks 
Sandeep Singhal