0

I have a problem with my code. This is a Trigger on Opportunity. When the Opportunity is closed and won, we create a new Policy__c from that opportunity. The Policy__c also has a related list, called PolicyERN__c. This is the part I am having trouble with. The line on which it says:

Map<Id, Policy__c> polsWithJunc = new Map<Id, Policy__c>( [Select Id, (Select ERN__c, Policy__c From PolicyERNs__r) From Policy__c WHERE Id IN :polIds]);

, the polIDs is not defined. I am not sure how to get these polIDs. They should be the IDs of newpolicies. It would be great if someone could help me amend my code. Tia.

 trigger CreatePolicy on Opportunity (after insert, after update) {
    //Mcreate a filtered map of opps excluding any Targets
    Map<Id, Opportunity> mapRealOpps = new Map<Id, Opportunity>();
    for(Opportunity anOpp: Trigger.new){
        if(!anOpp.Target__c){
            mapRealOpps.put(anOpp.Id, anOpp);
        }
    }
    if(Trigger.IsUpdate && Trigger.IsAfter){
        CurrencyChange.cascade(Trigger.newMap,Trigger.oldMap);        
    }
    if (TriggerStopper.StopOpp) {    
    } else if (Renewal_Settings.Instance.Triggers_Enabled) {

        TriggerStopper.StopOpp = true;

        List<Policy__c> newPolicies = new List<Policy__c>();
        List<Opportunity> renewalOpps = new List<Opportunity>();
        Set<id> accountIds = new Set<id>();
        for (Opportunity o : mapRealOpps.values()) {
            Opportunity oldOpp;
            if (trigger.isUpdate) {
                oldOpp = trigger.oldMap.get(o.id);
            }
            if ((trigger.isInsert && o.isClosed && o.isWon) || (oldOpp != null && o.isClosed && o.isWon && !oldOpp.isWon)) {

                Policy__c newPolicy = new Policy__c(
                    Client_Name__c = o.AccountId, 
                    Source_Opportunity__c = o.id,
                    Start_Date__c = o.Policy_Inception_Date__c,
                    End_Date__c = o.Expiry_Date__c,
                    //Add policy number from opp if available
                    Policy_Number__c = o.Policy_Number__c
                    );

                newPolicies.add(newPolicy);
            }
            accountIds.add(o.AccountId);
        }

        INSERT newPolicies;

        Map<Id, Policy__c> polsWithJunc = new Map<Id, Policy__c>([Select Id, (Select ERN__c, Policy__c From PolicyERNs__r) From Policy__c 
                                WHERE Id IN :polIds]);

        Map<Id, Policy__c> newPolsByOppId = new Map<Id, Policy__c>();
        for (Policy__c pol : newPolicies) {
            newPolsByOppId.put(pol.Opporunity, pol);
        }


        List<PolicyERN__c> newPolERNs = new List<PolicyERN__c>();

        for (Opportunity opp : scopedOpps) {
            Policy__c pol = polsWithJunc.get(opp.Policy__c);

        for (PolicyERN__c polERN : pol.PolicyERN__c) { 
            PolicyERN__c newPolERN = polERN.clone();
            newPolERN.Policy__c = newPolsByOppId.get(opp.Id).Id;
            newPolERNs.add(newPolERN);
            }
        }

        insert newPolERNs;

    }
}
1

You could just use the following in your SOQL and it should work.

Map polsWithJunc = new Map([Select Id, (Select ERN__c, Policy__c From PolicyERNs__r) From Policy__c WHERE Id IN :newPolicies ]);

0

Once you have inserted the newPolicies list the record id's will be added to that list.

So in order to create a set of id's from that list (which you will need for the query), you can do this:

INSERT newPolicies;
// define the id set
Set<Id> polIds = new Set<Id>();
// add the policy ids to the set
for(Policy__c thePolicy : newPolicies){
   polIds.add(thePolicy.Id);
}

Map<Id, Policy__c> polsWithJunc = new Map<Id, Policy__c>([Select Id, (Select ERN__c, Policy__c From PolicyERNs__r) From Policy__c 
                        WHERE Id IN :polIds]);
  • Can't we just used newPolicies instead of polIds in the WHERE clause of SOQL? – Mahmood Dec 16 '14 at 10:13
  • 1
    @MahmoodButt that should be possible as well indeed and much more efficient. Worth posting an answer I'd say! – Guy Clairbois Dec 16 '14 at 10:17

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.