• Tom Ebenhoch
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
So we are in the process of setting up a "Buy Now" function on our website and I have an existing apex class and workflow process that will auto convert the "buy now" leads and creates an account and opportunity. My question is, how can I set it up that if the company account already exists in our instance, that an opportunity will be automatically created?

This is the apex class that I have in place now, I would like it to be able to look up if an account already exists in our instance and then create just an opportunity if so.
 
Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead: LeadIds){
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead);                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                MassLeadconvert.add(Leadconvert);
        }
        
        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }
}

 
Good morning,

I have an apex trigger on opportunities that creates a new opportunity for the next year when a specific "sales stage" is selected. I recently updated the trigger to fire when the Closed/Won sales stage is selected OR Membership Started is selected. The problem is that the Membership Started stage is changed at a later date to Closed/Won, which will create a duplicate new opportunity (we only want 1 new opp). Does anyone have any advice? I have been stuck for a few days. My code is shown below:

 public static void createNextYearOpp(List<Opportunity> newlist, Map<Id, Opportunity> oldMap){
        Map<String, Schema.RecordTypeInfo> recordTypeInfo = Schema.SObjectType.Opportunity.getRecordTypeInfosByName();      
        list<Opportunity> newYearOppList = new list<Opportunity>();
        map<Id,Opportunity> originalOppIdToCloneOpp = new map<Id,Opportunity>();
        
        for(Opportunity opp : newList){
            if(oldMap != null && oldMap.get(opp.id).StageName != opp.StageName && opp.StageName == 'Closed/Won'|| opp.StageName =='Membership Started PIP'
                    && opp.Buy_Type__c == 'Security Benchmarks' && opp.Prospect_Type__c == 'SB New Business'
                    && opp.Membership_Start_Date__c != null) {
                Opportunity newOpp = new Opportunity();
                Id recordId = recordTypeInfo.get(opp.Prospect_Type__c).getRecordTypeId();
                newOpp.RecordTypeId = recordId;
                newOpp.name = opp.CloseDate.addYears(1).year() + ' ' + opp.name.subString(4);
                newOpp.Prospect_Type__c = 'SB Renewal';
                newOpp.CloseDate = opp.Membership_Start_Date__c.addYears(1);
                newopp.Membership_Start_Date__c = opp.Membership_Start_Date__c.addYears(1);
                newopp.Buy_Type__c = opp.Buy_Type__c;
                newOpp.AccountId = opp.AccountId;
                newOpp.NextStep = opp.NextStep;
                newOpp.Follow_up_Date__c = opp.Follow_up_Date__c;
                newOpp.CIS_CAT_Trial__c = opp.CIS_CAT_Trial__c;
                newOpp.Billing_Cycle__c = opp.Billing_Cycle__c;
                newOpp.Priority__c = opp.Priority__c;
                newOpp.Membership_Category__c = opp.Membership_Category__c;
                newOpp.OwnerId = opp.OwnerId;
                newOpp.Legacy_Membership_Fee__c = opp.Legacy_Membership_Fee__c;
                newOpp.TotalOpportunityQuantity = opp.TotalOpportunityQuantity;
                newOpp.Probability = opp.Probability;
                newOpp.Reason_Lost__c = opp.Reason_Lost__c;
                newOpp.Forecast__c = opp.Forecast__c;
                newOpp.LeadSource = opp.LeadSource;
                newOpp.Description = opp.Description;
                newOpp.Legacy_Created_By__c = opp.Legacy_Created_By__c;
                newOpp.Legacy_Created_Date__c = opp.Legacy_Created_Date__c;
                newOpp.Campaign = opp.Campaign;
                newOpp.Amount = opp.Amount;
                newOpp.StageName = 'To Be Invoiced';
                system.debug('@@ '+newOpp.name);
                //newYearOppList.add(newOpp);
                originalOppIdToCloneOpp.put(opp.Id,newOpp);
            }            
        }
So we are in the process of setting up a "Buy Now" function on our website and I have an existing apex class and workflow process that will auto convert the "buy now" leads and creates an account and opportunity. My question is, how can I set it up that if the company account already exists in our instance, that an opportunity will be automatically created?

This is the apex class that I have in place now, I would like it to be able to look up if an account already exists in our instance and then create just an opportunity if so.
 
Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead: LeadIds){
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead);                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                MassLeadconvert.add(Leadconvert);
        }
        
        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }
}