0

I need to display related opportunities with matches same gsm and asset tracker and other conditions need to display in lead level.

For example:

  1. query on asset tracker & gsm matches
  2. If first list is empty i need to search only gsm matches
  3. If second list empty look for other filter.

My like looks like

newopplist=[Select Id,StageName,Type from Opportunity where Asset=:assetset 
            AND Oppgsm=:gsmset AND StageName='Closed Won' ];
    if(newopplist.size() < 1)
    {
    newopplist=[Select Id,StageName,Type from Opportunity where 
                 Asset=:assetset AND StageName='Closed Won' ];
    }

But two conditions are working fine if i try to filter like same way i am unable to get results.

Please let me know is it the better way to find

1
  • 2
    Please stop introducing every post with "Hi friends I have a requirement like".
    – Adrian Larson
    Jun 7 '17 at 18:10
0

I would suggest you to first run a SOQL which could give you all values including or excluding Oppgsm

Then from the List, you will create separate list based on conditions.

Since you are firing SOQL queries multiple times on Opportunity, so better fire the SOQL once, get the list and filter in the for loop.

Map<String,String> gsmset = new Map<String,String>();
gsmset.put(key,value);

newopplist=[Select Id,StageName,Type, Oppgsm 
            From Opportunity 
            WHERE Asset=:assetset 
            AND StageName='Closed Won' ];



for(Opportunity oppty:newopplist)
{
    if(gsmset.containsKey(oppty.Oppgsm))
    {
        //prepare your desired list
    }
    else{
        //prepare your desired other list
}

Update based on comments

If you want to match gsmset with startswith, here is the approach:

for(Opportunity oppty:newopplist)
{
    for(String str:gsmset.values())
    {
        if(str.startsWithIgnoreCase(oppty.Oppgsm))
        {

        }
    }
}
6
  • N.B. the "if" statement is not actually required here
    – cropredy
    Jun 7 '17 at 18:15
  • I thought that he will try to match the dataset based on Oppgsm and create a new list Jun 7 '17 at 18:16
  • sorry - i meant the first 'if' statement
    – cropredy
    Jun 7 '17 at 18:17
  • oh, thats my habit, I will remove Jun 7 '17 at 18:18
  • I tried to customize my code with your suggestion.But i want to match gsmset with startswith not contains any possibility is there.@SantanuBoral
    – Pavan tej
    Jun 14 '17 at 13:45

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.