function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lawlaw 

Map bring back on 1 of 2 records

In the highlighted query below.  The result only returns 1 of 2 records.  Any help into how to resolve this.  There does exist 2 opportunities in this contact.  Thanks in Advance.

 

 

public OfferStatusChangeFlowHelper (List <Offer__c> triggerNew) {

if (triggerNew != null)
this.triggerNew = triggerNew;

//get opps and con ids
for (Offer__c offer : triggerNew) {
contactIds.add(offer.Candidate_Name__c);
oppIds.add (offer.opportunity__c);
}

for (Offer__c o:[SELECT Id, Name, Offer_Status__c, Candidate_Name__c FROM Offer__c WHERE Candidate_Name__c IN :contactIds
AND (Offer_Status__c = 'Offer Accepted' OR Offer_Status__c = 'Offer Extended') ]) {
//check map for contact id
if (contactOffersMap.get(o.Candidate_Name__c)==null) {//no entry yet
contactOffersMap.put (o.Candidate_Name__c, new List <Offer__c> {o});
} else {//otherwise get list of opps and add a new one
contactOffersMap.get(o.Candidate_Name__c).add(o);
}
}

for (Opportunity o:[SELECT Id, Name, StageName, Contact__c FROM Opportunity WHERE Id IN :oppIds AND isClosed = false]) {// comment out to test bring back open and closed for first part of step 9.
System.debug('OPPORTUNITY IDS: ' + oppIds);
//for (Opportunity o:[SELECT Id, Name, StageName, Contact__c FROM Opportunity WHERE Id IN :oppIds]) {
//check map for contact id
if (oppMap.get(o.Contact__c)==null) {//no entry yet
oppMap.put (o.Contact__c, new List <Opportunity> {o});
} else {//otherwise get list of opps and add a new one
oppMap.get(o.Contact__c).add(o);
}
}
}

Best Answer chosen by Admin (Salesforce Developers) 
GunishGunish

Hi,

 

In that case, change your selection code to following. 

 

for (Opportunity o: [SELECT Id, Name, StageName, Contact__c FROM Opportunity WHERE Contact__c IN: contactIds AND isClosed = false]) { // comment out to test bring back open and closed for first part of step 9.
        System.debug('OPPORTUNITY IDS: ' + oppIds);

 

Let us know if this helps!

 

-Gunish

All Answers

GunishGunish
HI There,

Can you tell us what was the result of this debug statement ?

System.debug('OPPORTUNITY IDS: ' + oppIds);

Regards,
Gunish
lawlaw

|USER_DEBUG|[45]|DEBUG|OPPORTUNITY IDS: {006J000000BCTQbIAP}

GunishGunish

I think I figured out the problem.

 

The basic problem is thet oppIds field gets only a single opportunity Id, where as you are expecting it to contain two.

 

for (Offer__c offer: triggerNew) {
        contactIds.add(offer.Candidate_Name__c);
        oppIds.add(offer.opportunity__c);
    }

 

In this section, you populate oppIds with the values collected in triggerNew collection. 

 

If you add a new item to this collection, one at a time, the oppIDs will get only one OpportunityID. 

 

Following this, further in your code. 

 

for (Opportunity o: [SELECT Id, Name, StageName, Contact__c FROM Opportunity WHERE Id IN: oppIds AND isClosed = false]) { // comment out to test bring back open and closed for first part of step 9.
        System.debug('OPPORTUNITY IDS: ' + oppIds);

 

You only select, Opportunities which match the values in the oppIds, which will return only one value. 

 

Solution :

 

If you want to select all the opportunities from your system, simply remove the where clause in your code. which will return both. 

 

for (Opportunity o: [SELECT Id, Name, StageName, Contact__c FROM Opportunity WHERE isClosed = false]) { // comment out to test bring back open and closed for first part of step 9.
        System.debug('OPPORTUNITY IDS: ' + oppIds);

 

Let us know if this helps!

 

-Gunish

 

lawlaw

Gunish

 

Thanks for the suggestion.  However, I do not want all opportunities in the system.  I want all opportunities for a specific contact.

GunishGunish

Hi,

 

In that case, change your selection code to following. 

 

for (Opportunity o: [SELECT Id, Name, StageName, Contact__c FROM Opportunity WHERE Contact__c IN: contactIds AND isClosed = false]) { // comment out to test bring back open and closed for first part of step 9.
        System.debug('OPPORTUNITY IDS: ' + oppIds);

 

Let us know if this helps!

 

-Gunish

This was selected as the best answer