0

I've Updated my code, and I'm still not getting the additional records inserted. My goal is; once an opportunity is closed a project is created. Once the project is created Credit Details (OpportunityLineItems) are added to the project. Any help on why I'm not getting this to happen is definitely appreciated. I've posted the code below.

trigger AddProject on Opportunity (after insert, after update) {

list<Project__c> lstProject = new List<Project__c>();
list<CreditDetail__c> lstCD = new List<CreditDetail__c>();
list<map<Date,CreditDetail__c>> lstmapCreditDetail = new List<map<Date,CreditDetail__c>>();


    list<Opportunity> lstOpp = [Select id, Accountid, CloseDate, StageName, Generate_Projects__c, RecordTypeId,(Select Id, ProductID__c,Jurisdiction__c,Quantity,ServiceDate From Opportunitylineitems) From Opportunity
                               ];

    for(opportunity opp : trigger.new){
        if(opp.Generate_Projects__c == true && opp.StageName =='7 - Closed Won' && opp.RecordTypeId == '012M00000004qsD') {
            lstProject.add(new Project__c(Opportunity__c = opp.Id, Project_Stage__c = '1 - Awaiting Start - Internal', Contract__c = opp.Contract__c, Account__c = opp.AccountId, Kickoff_Date__c = opp.CloseDate, Program_Type__c = 'Research & Development', Name = opp.name+'-'+'Research & Development'));

        }
    }

    insert lstProject;

**for (Integer i = 0; i < lstOpp.size(); i++) {
        lstmapCreditDetail.add(new map<Date,CreditDetail__c>());
        if (lstOpp[i].StageName == 'Closed Won' && lstOpp[i].RecordType.name == 'Corporate Sales' && lstOpp[i].Projects_Created__c == false && 
            lstOpp[i].Generate_Projects__c == true){

     for (integer j = 0; j < lstOpp[i].OpportunityLineItems.size(); j++){
                if (lstmapCreditDetail[i].get(lstOpp[i].OpportunityLineItems[j].ServiceDate) == null) {
                    lstmapCreditDetail[i].put(lstOpp[i].OpportunityLineItems[j].ServiceDate, 
                       new CreditDetail__c(Project__c =lstmapCreditDetail[i].get(lstOpp[i].OpportunityLineItems[j].ServiceDate).Id,
                                            Name = 'Test Credit Detail',
                                            Beneficiary__c = 'LLC',
                                            Jurisdiction__c = 'AL',
                                            Tax_Year__c = '2014'));
                }
     }
            }
}
insert lstCD;
}**

1 Answer 1

0

Your lstCD never actually gets CreditDetail__c passed into it. So when you get to your insert you are inserting only a new List(); In your for loop make sure that you are adding the items to the lstCD as you iterate through.

list<CreditDetail__c> lstCD = new List<CreditDetail__c>();

Nowhere is lstCD used except in the insert statement:

insert lstCD

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.