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
Ajay Kumar 583Ajay Kumar 583 

Need Apex Code for the Logic please.

Hi Team,

Need a Apex class for following:

     Create an Apex class called “SalesUtility”. Methods in this class must be accessible without creating an instance of it.
     And a utility method in “SalesUtility” which will return sum of “Expected Revenue” from Opportunity for current fiscal quarter. And 
     Write a utility method in “SalesUtility” class which will return all the Opportunity records having the same name as Lead Company field on Lead records. 
     Write a test class to verify all the above scenarios.

Thanks,
Aj
Best Answer chosen by Ajay Kumar 583
GovindarajGovindaraj
Hi Ajay,

Please refer below class, This is onof the ways to acheve your requirement.
public class SalesUtility {    
    public static decimal getExpectedRevenue() {
        decimal totalExpectedRevenue;
        AggregateResult OptyExpectedRevenue = [SELECT SUM(ExpectedRevenue)exp FROM Opportunity WHERE CloseDate = THIS_FISCAL_QUARTER AND StageName <> 'Closed Lost'];
        totalExpectedRevenue = (decimal)OptyExpectedRevenue.get('exp');
        return totalExpectedRevenue;
    }
    public static list<Opportunity> getSimilarOpty() {
        list<Opportunity> matchingOpty = new list<Opportunity>();
        list<Lead> lstLead = [SELECT Company FROM Lead];
        list<Opportunity> lstOpportunity = [SELECT Name FROM Opportunity];
        for(Lead leadObj : lstLead) {
            for(Opportunity oppObj : lstOpportunity) {
                if(oppObj.Name == leadObj.Company)
                	matchingOpty.add(oppObj);
            }  
        }   
        return matchingOpty;
    }
}
Please let us know, if this helps.

Thanks,
Govindaraj.S

All Answers

GovindarajGovindaraj
Hi Ajay,

Please refer below class, This is onof the ways to acheve your requirement.
public class SalesUtility {    
    public static decimal getExpectedRevenue() {
        decimal totalExpectedRevenue;
        AggregateResult OptyExpectedRevenue = [SELECT SUM(ExpectedRevenue)exp FROM Opportunity WHERE CloseDate = THIS_FISCAL_QUARTER AND StageName <> 'Closed Lost'];
        totalExpectedRevenue = (decimal)OptyExpectedRevenue.get('exp');
        return totalExpectedRevenue;
    }
    public static list<Opportunity> getSimilarOpty() {
        list<Opportunity> matchingOpty = new list<Opportunity>();
        list<Lead> lstLead = [SELECT Company FROM Lead];
        list<Opportunity> lstOpportunity = [SELECT Name FROM Opportunity];
        for(Lead leadObj : lstLead) {
            for(Opportunity oppObj : lstOpportunity) {
                if(oppObj.Name == leadObj.Company)
                	matchingOpty.add(oppObj);
            }  
        }   
        return matchingOpty;
    }
}
Please let us know, if this helps.

Thanks,
Govindaraj.S
This was selected as the best answer
Ajay Kumar 583Ajay Kumar 583
Thanks Govindaraj,

It Worked.