• Shawn Reichner 29
  • NEWBIE
  • 469 Points
  • Member since 2017

  • Chatter
    Feed
  • 11
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 28
    Questions
  • 133
    Replies
I have one checkbox field on account object and I want to create workflow rule where every time that checkbox values go from True to False then it will update one field which is date data type with on which date that checkbox goes from true to false and I do not want to update that date field when record is created it only fired when checkbox goes from true to false
What workflow rule do I run to populate a currency field with the value from another currency field on the same object (opp page)? 

Tried using TEXT () and VALUE () but kept getting errors.
Hi I am trying to figure out to how trigger works for updating checkbox on Case object when checkboxfield of other custom object is set to true ,Lets say I have a custom field Required__c on Destination object and I have a Criteria__c  field on Case object, whenever required__c is true I want to update the criteria__c on case object to true , can anyone please guide me on this 

Thanks in advance
  • November 03, 2017
  • Like
  • 0
Is there an easy way to find out how many records in a custom object have Notes and attachments in them?
I schedule a data export report, but that did not work. I'm working with Salesforce support.
Meanwhile, want to find out if there is a way to find out how many records have attachments.
 
Hello gurus,
I'm trying to be a better developer by writing the test code first.  I can't create all the needed test objects.  Account, Contact, Case are created and I can see the IDs in the debug log but get an error when creating the Asset.  What am I overlooking?

There is no other code created yet.
 
@IsTest(SeeAllData=True)

public class AssetUpdateTest {
    
    public static testmethod void testAssetLink()    {
        Account acc = createAccount(); 
        Contact con = createContact(acc); 
        Case cas = createCase(acc,con); 
        Asset ast = createAsset(acc,con,cas); 

        System.debug('**************************   Case ID: ' + cas.id); 
        System.debug('**************************   Case.Asset: ' + cas.AssetId);
        
    }
    
    public static Account createAccount(){
        Account theAccount = new Account(
            recordtypeid='01270000000Q7rQ', 
            name='Test Account', 
            Status__c='Client',
            Line_of_Business__c='CRS',
            phone='8005551212', 
            currencyisocode='USD'); 
        
        insert theAccount;
        return theAccount; 
    }    
    
    public static Contact createContact(Account theAccount){
        Contact theContact = new Contact(
            recordtypeid='01270000000Q7ra',
            email='tim.bouscal@audatex.com', 
            firstname='George', 
            lastname='Washington',
            accountid=theAccount.id);
        insert theContact;        
        return theContact; 
    }
    
    public static Case createCase(Account acc, Contact con){
        Case theCase = new Case(
            recordtypeid='01270000000Dy9u', 
            AccountId = acc.Id, 
            ContactId = con.Id,             
            
            Requestor__c=con.Id, 
            Reason='New Account Setup', 
            Reason_Detail__c='New - ADXE Shop 02',
            Status='New', 
            Origin='Sales Request', 
            Subject='AssetUpdateTest Sample BisOps Case', 
            Description='This is a sample case for testig the AssetUpdate class'
        ); 
        insert theCase; 
        return theCase;
    }    
    
    public static Asset createAsset(Account acc, Contact con, Case cas){
        System.debug('***** ***** ***** Account ID: ' + acc.id + '     Contact Id: ' + con.id + '     Case ID: '+ cas.Id); // all 3 values show in debug log
        Asset theAsset = new Asset(
            Name='Test Asset', 
            Account = acc, 
            Contact = con, 
            Case_Number__c = cas.CaseNumber, 
            Type__c='New', 
            Status='Purchased', 
            Description='This is a test asset for testing the AssetUpdate class'
        );
        insert theAsset; 
        return theAsset; 
    }
}

Error: 
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Every asset needs an account, a contact, or both.: [AccountId, ContactId]

Class.AssetUpdateTest.createAsset: line 69, column 1
Class.AssetUpdateTest.testAssetLink: line 9, column 1

 
Hello all,
I currently have the following logic in a workflow rule to determine when to send out an email alert:

AND ( 
RecordTypeId <> "012C0000000CCTo", 
ISCHANGED ( Requested_Dev_Date__c ), 

OR 
(ISPICKVAL (StageName , "6.5 Process with Signature" ), 
(ISPICKVAL (StageName , "6.3 Process without Signature" )) 
)))

I now have a need to test for more than one record type ID, while the rest of the logic would stay the same. How do I best accomplish this code? Is the right approach a CASE statement, or IF? How would this be structured?
 
Hi,

How do you "read" (in plane English) this piece of code?
List<OpportunityShare> oppShareList = [select Id, OpportunityAccessLevel, RowCause from OpportunityShare where OpportunityId IN :opportunityIdList and RowCause = 'Team'];
My interpretation is...

Create a variable of the type List<OpportunityShare> which is called oppShareList and assign the following records to such list: all records (display the fields Id, OpportunityAccessLevel, etc...) FROM the table OpportunityShare WHERE ...????

I do not understand the code after the WHERE

Could someone explain?

Thank you very much.


 
Hi All,

I have a request from our business, to create a field on a custom object called Vehicle, A field that show the total values of the opportunities associated with the specific vehicle. Our in-house developer managed to do that, but with a schedule rule that runs only once/day, and the request is to show the total amount every time an amount in a opportunity is updated.

The developer created 2 different classes, one to make a sum of all values from all opportunities:
/* @date 29.09.2017 */
/* @description for update a summary field on vehicle level, which sums up the opportunity amount of the related opportunities*/
global class Opportunity_Amount_Update implements Database.Batchable<sObject>  {

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT ID FROM Vehicle__c ' );
    }
    
    global decimal STTO = 0;

    global void execute(Database.BatchableContext bc, List<Vehicle__c> scope){
        // process each batch of records
        Set<Vehicle__c> Veh = new Set<Vehicle__c>();
        List<Vehicle__c> Veh1 = new List<Vehicle__c>();
        List<Opportunity> OPP = [Select Id, GH_Vehicle__c, Amount from Opportunity WHERE GH_Vehicle__c <>'']; 
          for (Vehicle__c VEx : scope) {    
            for (Opportunity Opps1 : OPP ) {
                 if(Opps1.GH_Vehicle__c == VEx.id) {
                     STTO = STTO + Opps1.Amount;
                 }
            }
            VEx.Opportunity_amount__c = STTO;
            Veh.add(VEx);
            STTO = 0;
         }
        Veh1.addall(Veh);
        update Veh1;
    }    

    global void finish(Database.BatchableContext bc){
    }    
}

And another one as a scheduler:
global class Opportunity_Amount_Update_scheduler implements Schedulable {
    global void execute(SchedulableContext ctx) {
        Opportunity_Amount_Update OA = new Opportunity_Amount_Update();
        DataBase.executeBatch(OA);
    }
}

Ans as I mentionated abowe, we need the first class (Opportunity_Amount_Update) to run every time the value of an opportunity changes.
Can you please help me with this, if it can be made like that?

Thank You
Calin B
I'm VERY new to Apex (literally all coding) and am trying to figure out how to update the Priority to High on an activity after it has become overdue by 10 days.

The Priority field update is the only thing that I need to happen and I cannot figure out what I'm missing.  My alerts are both on line 3; unexpected token ':' and unexpected syntax: 'mismatched input':' expecting Semicolon.
User-added image

Being pointed in the right direction would be greatly appreciated!

Hi

I am using this app from the app exchange (https://appexchange.salesforce.com/listingDetail?listingId=a0N300000016cbDEAQ) to ensure that a contact role is added to an opportunity when a certain stage is selected on the opportunity. This works perfectly when updating an existing opportunity but I dont want it to apply when creating new opportunities.

I thought I could just add NOT(IsNew()) to the custom field, but it gives an error of IsNew can't be used in this type of formula.

Any ideas of how I can exclude this trigger from firing when creating a new opportunity would be appreciated?

Custom Field

IF( 
DATEVALUE( CreatedDate ) >= DATE(2017,09,28), 
CASE( 
StageName, 
"Stage to be Required 1",1, 
"Define",1, 
"Meeting / Proposal",1, 
"Negotiation",1, 
"Verbal Agreement/Contract",1, 
"Closed Won",1, 
0 
), 
0	
)
 


 

Apex Class

//This is provided as a sample to require a contact on opportunities it is provided without warranty and support. 

trigger opportunity_contact_required on Opportunity (before insert, before update) {

    //map to keep track of the contact_required = 1
    Map<String, Opportunity> oppy_contact = new Map<String, Opportunity>();

    //Trigger.new is an array of opportunities 
    //and adds any that have Contact_Required = 1 to the oppy_contact Map
    for (Integer i = 0; i < Trigger.new.size(); i++) {
        System.debug('*****Required? ' + Trigger.new[i].contact_required__c);
        if  (Trigger.new[i].contact_required__c == 1) {
            oppy_contact.put(Trigger.new[i].id,Trigger.new[i]);
        }
    }

    //map to keep track of the opportunity contact roles
    map<Id, OpportunityContactRole> oppycontactroles = new map<Id, OpportunityContactRole>();

    //select OpportunityContactRoles for the opportunities with contact role required 

    List<OpportunityContactRole> roles = [select OpportunityId, IsPrimary from OpportunityContactRole
        where (OpportunityContactRole.IsPrimary = True and OpportunityContactRole.OpportunityId
        in :oppy_contact.keySet())];


    for (OpportunityContactRole ocr : roles) {
        //puts the contact roles in the map with the Opportunity ID as the key
        oppycontactroles.put(ocr.OpportunityId,ocr);
    }

    // Loop through the opportunities and check if they exists in the contact roles map or contact role isn't required    
    for (Opportunity oppy : system.trigger.new) {
        //system.debug('List oppy Id - '+oppy.id);
        if  (oppy.contact_required__c ==1 && !oppycontactroles.containsKey(oppy.id))
        {
            oppy.addError('No Primary Contact has been added to the opportunity. Please go to the Contact Roles and select a primary contact.');       
        }
    } //for    
 }

Apex Test Class
 
public class test_opportunity_contact_required {


// test to ensure an opportunity can be added

public static testMethod void testoppyrequired0()
    {
        //create oppty 
        List<Opportunity> oppy = new List<Opportunity>();
                                    
        //add 10 opportunites without a contact, and with the condition contact required = 0
        
        for (Integer i = 0; i < 10; i++) {
            oppy.add(new Opportunity(Name='nick_test'+i,StageName='Perception Analysis',CloseDate=System.Today()));
        }
        insert oppy;
        
        map<Id, Opportunity> oppy_map = new map<Id, Opportunity>();
        
        for (Integer i = 0;i<10;++i){
            oppy_map.put(oppy[i].Id,oppy[i]);
        } //for
        
        System.assert([SELECT count() FROM Opportunity WHERE Id IN :oppy_map.keySet()] == 10);   
    } //testoppyrequired = 0
    
    
    //test to go from a not required value to a required value

public static testMethod void testoppyrequired1()
    {   
            //create oppty 
            List<Opportunity> oppy2 = new List<Opportunity>();
                                        
            //add 10 opportunites without a contact, and with the condition contact required = 0       
            for (Integer i = 0; i < 10; i++) {
                oppy2.add(new Opportunity(Name='nick_test'+i,StageName='Qualification',CloseDate=System.Today()));
            }
            
            insert oppy2;
            
            for (Integer i = 0; i < 10; i++) {
                
          	  oppy2[i].StageName='Negotiation/Review';
            
            }

			Test.startTest();

			try {
			
			    update oppy2;
			    
			    Opportunity sampleTest = [Select Id, Contact_Required__c From Opportunity where Id = :oppy2[0].id];
			    
			    System.debug('*****SAMPLE' + sampleTest);
			    
			    System.assert(false, 'This update should have failed.');
			
			} catch(System.DmlException e) {
			
			    System.assert(e.getMessage().contains('No Primary Contact Exists.'));
			
			}

    		Test.stopTest();
       
 
    } //testoppyrequired = 1



public static testMethod void testoppyrequired1woprimary()
    {   
            //create oppty 
            List<Opportunity> oppy = new List<Opportunity>();
                                        
            //add 10 opportunites 
                    
            for (Integer i = 0; i < 10; i++) {
                oppy.add(new Opportunity(Name='nick_test'+i,StageName='Qualification',CloseDate=System.Today()));
            }
            
            insert oppy;
            
            //add 10 contacts
            
            List<Contact> c = new List<Contact>();
                                        
    
            for (Integer i = 0; i < 10; i++) {
                c.add(new Contact(LastName='nick_test'+i));
            }

            insert c;

            for (Integer i = 0; i < 10; i++) {
                
            oppy[i].StageName='Negotiation/Review';
            
            }

            //add 10 opporunity contact roles associated to the opportunities and contacts above
            
            List<OpportunityContactRole> ocr = new List<OpportunityContactRole>();
            
            for (Integer i = 0; i < 10; i++) {
                ocr.add(new OpportunityContactRole(Role='Business User',OpportunityId=oppy[i].id,ContactId=c[i].id));
            }
            
            insert ocr;
            
boolean caughtException = false;


Test.startTest();

try {

    update oppy;

} catch(System.DmlException e) {

    System.assert(e.getMessage().contains('No Primary Contact Exists.'));

    caughtException = true;

}

    Test.stopTest();

    System.assert(caughtException);         
 
    } //testoppyrequired = 1


public static testMethod void testoppyrequired1primary()
    {   
            //create oppty 
            List<Opportunity> oppy = new List<Opportunity>();
                                        
            //add 10 opportunites 
                    
            for (Integer i = 0; i < 10; i++) {
                oppy.add(new Opportunity(Name='nick_test'+i,StageName='Qualification',CloseDate=System.Today()));
            }
            
            insert oppy;
            
            map<Id, Opportunity> oppy_map = new map<Id, Opportunity>();
            
            for (Integer i = 0;i<10;++i){
                oppy_map.put(oppy[i].Id,oppy[i]);
            } //for
            
            //add 10 contacts
            
            List<Contact> c = new List<Contact>();
                                        
    
            for (Integer i = 0; i < 10; i++) {
                c.add(new Contact(LastName='nick_test'+i));
            }

            insert c;

            //add 10 opporunity contact roles associated to the opportunities and contacts above
            
            List<OpportunityContactRole> ocr = new List<OpportunityContactRole>();
            
            for (Integer i = 0; i < 10; i++) {
                ocr.add(new OpportunityContactRole(Role='Business User',OpportunityId=oppy[i].id,ContactId=c[i].id,IsPrimary=True));
            }
            
            insert ocr;
            
            for (Integer i = 0; i < 10; i++) {
                
            oppy[i].StageName='Negotiation/Review';
            
            }

			try {
			
			    update oppy;
			    
			        System.assert([SELECT count() FROM Opportunity
			            WHERE Id IN :oppy_map.keySet()] == 10);
			
			} catch(System.DmlException e) {
			
			    System.assert(false);
			
			}   
 
    } //testoppyrequired = 1 and primary contact = true

} //test class





 
Hello awesome Devs, 

I have the following scheduled class that has been running fine but all of a sudden I am now getting an Apex Time Out error when this runs each morning.  After doing some research it looks like this class although is being scheduled it is still running syncronously and hittong the goveror limits. How can I update the followign code to be both Schedulable and Batchable to try and get arounf the timing issue?

Thanks for any advise or reworkign of my code that you can help out with,


Shawn

Trigger Code:
 
global class AMPCurrentAmountBatching Implements Schedulable {

    global void execute(SchedulableContext sc){
        AMPCurrentAmountBatching();
    }
    @future
    static public void AMPCurrentAmountBatching(){
        
       List<Opportunity> opps = new List<Opportunity>();
       Set<String> mySet = new Set<String>();
       Integer recordCount = [SELECT Count() FROM Zuora__Subscription__c WHERE OpportunityId__c != null AND LastModifiedDate = LAST_N_DAYS:60];
       Integer sizeBlock = recordCount/2000 + (math.mod(recordCount,2000)!=0?1:0);
        
        For(Integer i=0;i<sizeBlock;i++){
        For(AggregateResult objar : [SELECT OpportunityId__c Oid, SUM(Total_Booking_Amount__c) Amt
                                    FROM Zuora__Subscription__c WHERE OpportunityId__c !=null AND LastModifiedDate = LAST_N_DAYS:60
                                    AND OpportunityId__c NOT IN:mySet GROUP BY OpportunityId__c LIMIT 2000])
        {
        
        Decimal d = (Decimal)objar.get('Amt');
        
            Opportunity Opp = new Opportunity();
            Opp.Id = (Id)objar.get('Oid');
            Opp.Current_Value__c = (Decimal)objar.get('Amt');
            opps.add(Opp); 
            mySet.add(Opp.Id);
        }
        }
        
        If(opps.size()>0){
           update opps;
        }
        
        
    }
    
    
}

 
Awesome Devs!

I have the followign trigger that will fire upon a lead creation to auto convert the lead.  That is working like a charm, however the issue I am running into is doing a search for an existing account to append the Contact and Opportunity to that is created via the lead conversion. 

I have some logic built in that looks like it is not working to locate an existing account based on the Lead Company field, but my code is still creating a new account each time a lead is automaticlaly converted.  

Can anyone help with getting my code to properly append the lead to the existing account if one exists, and create a new one if one is not found?

Here is the trigger code:
 
Trigger AutoConverter on Lead (after insert) {
     LeadStatus convertStatus = [
          select MasterLabel
          from LeadStatus
          where IsConverted = true
          limit 1
     ];
     List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
	 MAP<Id,Account> AccountSearch = new MAP<Id,Account>([SELECT ID, Name FROM Account]);
    
     for (Lead lead: Trigger.new) {
          if (!lead.isConverted && lead.Company != null) {
              
              If(AccountSearch.equals(Lead.Company)){
               Account Acct = AccountSearch.get(Lead.Company);
               ID AcctId = Acct.Id;
               Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Company+Lead.CreatedDate;
               
               lc.setLeadId(lead.Id);
               lc.setOpportunityName(oppName);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               lc.setAccountId(AcctId);
               
               leadConverts.add(lc);
              }
              Else{
                  Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Name;
               
               lc.setLeadId(lead.Id);
               lc.setOpportunityName(oppName);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               
               leadConverts.add(lc);
              }
          }
     }

     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}

 
Hello awesome Devs!

I have the following Apex class that is ran eaxch morning to perfrom an aggregate results to compile all child records that are associated with a certain opportunity and sum up thier currency values from a formula field called Total Bookings Amount.  Then the class will grab that sum and place the total summed amount into a custom field on the Opportunity called Current Value.  

The issue is if the child record is in GBP currrency the aggregate results class will treat it as USD as that is our ORG's company currency and the value is off by the conversion amount.  

So for more context:
Child Record A - Total Booking Amount = 10 USD
Chile Record B - Total Booking Amount = 14.55 USD
Total Summed amoutn for both Child Records = 24.55 USd - Converted to 18.00 GBP on record. 

When Class runs it takes the GBP total listed above converted back into USD and populates this amount on the Parent Opportunity on the Current Value field so it shows a total of 24.55 GBP (really it is the USD rate, but since the Opportunity is in GBP it shows as GBP although it is not correct), however the conversion does not happen when this Current Value is populated although the amount standard field is converted to the proper amount which shows 18.00 GBP as the Amount. 

Hope this makes sense, but ultimately I am tryign to have the class convert the summed amount before populating on the parent Opportunity if the currencyIsoCode is GBP. 

Any thoughts????

Clas Code - 
 
global class AMPCurrentAmountBatching Implements Schedulable {

    global void execute(SchedulableContext sc){
        AMPCurrentAmountBatching();
    }
    @future
    static public void AMPCurrentAmountBatching(){
        
       List<Opportunity> opps = new List<Opportunity>();       
        
        For(AggregateResult objar : [SELECT OpportunityId__c Oid, SUM(Total_Booking_Amount__c) Amt
                                    FROM Zuora__Subscription__c WHERE OpportunityId__c !=null AND LastModifiedDate = LAST_N_DAYS:60
                                    GROUP BY OpportunityId__c])
        {
        
        Decimal d = (Decimal)objar.get('Amt');
        
            Opportunity Opp = new Opportunity();
            Opp.Id = (Id)objar.get('Oid');
            Opp.Current_Value__c = (Decimal)objar.get('Amt');
            opps.add(Opp); 
        }
        
        If(opps.size()>0){
           update opps;
        }
        
        
    }
    
    
}

 
Hello awesome devs! 

I have the following class which I woudl like to batch on in a nightly running batch.  This class is to Aggregate some subscription records and group by a String field which will contain the 18 digit ID number from an Opportunity record that the subscriptions make up.  I am then summing the total amoutn from those subscriptions in the aggregate results method so I am left with an amouny for each grouping of subscriptions grouped by the ID number.  

I then want to use that SUM amount to update a field on the Opportunity that is related to the group of subscriptions.  What is happening with my code below is when I run the class, all subscriptions are being summed for every opportunity.  Meaning any opportunity I go to now has the same SUM value which is the value of all Subscriptions in our database with an Opp ID populated and it seems like it is not grouping. 

What did I do wrong, or what should I try as I am stumped.....

Thank you in advance for any help you can provide,

Shawn
 
global class AMPCurrentAmountBatching Implements Schedulable {

    global void execute(SchedulableContext sc){
        AMPCurrentAmountBatching();
    }
    
    public void AMPCurrentAmountBatching(){
        
       List<Opportunity> opps = new List<Opportunity>();
        
       Id idOpp;
       Decimal amt;
       Set<ID> setIdOpp = new Set<ID>();
       Map<Id,Opportunity> mapOpp = new Map<Id,Opportunity>();
       List<AggregateResult> AR = new List<AggregateResult>();
       
        
        For(AggregateResult objar : [SELECT OpportunityId__c, SUM(Total_Booking_Amount__c)
                                    FROM Zuora__Subscription__c WHERE OpportunityId__c !=null
                                    GROUP BY ROLLUP(OpportunityId__c)])
        {
            AR.add(objar);
            setIdOpp.add((ID)objar.get('OpportunityId__c'));  
        }
        
        If(!setIdOpp.isEmpty()){
           // mapOpp = new Map<Id,Opportunity>([SELECT ID, Current_Value__c FROM Opportunity WHERE Id IN: setIdOpp]);
           opps = [SELECT ID, Current_Value__c FROM Opportunity WHERE Id IN: setIdOpp];
        }
        
        For(AggregateResult objar : AR){
            idOpp = (Id)objar.get('OpportunityId__c');
            amt = (Double)objar.get('expr0');
          
        }
        If(opps.size()>0){
        For(Opportunity o : opps){
            o.Current_Value__c = amt;
        }
        }
        
        update opps;
        
    }
    
    
}

 
Hello,

I have the following schedulable class that I want to set up to run on the last day of every month to update a currency field to match another currency field on the same object.  This is done to be able to track monthly reductions and or growth to the currency field being targeted in a report. 

However when I run this class in any sandbox it works just fine, but now when I deployed to production i get the System,LimitExecption: Apex CPU time limt exceeded error for line 13 of my class. 

Any idea why this woudl trigger this error and any ideas on how to resolve?

Thank you so much in advance fo rany help you can provide!

Class code:
 
global class NightlyStartingCMRRUpdate Implements Schedulable
{
    global void execute(SchedulableContext sc)
    {
        UpdateStartingCMRR();
    }
      
    public void UpdateStartingCMRR()
    {
        List<Zuora__CustomerAccount__c> updates = new List<Zuora__CustomerAccount__c>();
        
        For(Zuora__CustomerAccount__c CA : [SELECT ID, Zuora__MRR__c, Starting_CMRR__c FROM Zuora__CustomerAccount__c WHERE Zuora__MRR__c != null]){
                CA.Starting_CMRR__c = CA.Zuora__MRR__c;
                updates.add(CA);
        }
        
        If(updates.size()>0){
            update updates;
        }
        
    }
}

 
Hello Devs,

I have the followign code that is using the Aggregate Results method to batch up records to create one Opportunity record for each Account and Status of the record being Aggregated.  My question is I now have a need to take each one of the records that is being Aggregated and create a record for each of the Aggregated records in a new custom object.  I have th eobject and needed fields created but I am unsure how to use those records that are being aggregated to create a record in the custom object for each aggregated record, I can only get the aggregated results to create one record, btu again I need a record create din the new custom object for each of the records being used in the aggregate result and not the aggregated result itself.  

Can anyone help me understand how to tap into those records being aggregated before the aggregate for loop to achieve this need? 

Thank you so much for any help you can provide,

Shawn

Apex Class code: 
 
global class CreateUpgradeDowngradeOpportunities Implements Schedulable
{
    global void execute(SchedulableContext sc)
    {
        CreateOpportunities();
    }
      
    public void CreateOpportunities()
    {
        //Variable & List Declarations
        
        Date Today = Date.Today();
        Date d1 = Today.addDays(-1);
        List<Opportunity> lstOpp = new List<Opportunity>();
        Opportunity objOpp;
        Id idRecTypeDowngrade = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('AMP Downgrade').getRecordTypeId();
        Id idRecTypeUpgrade = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('AMP Upgrade').getRecordTypeId();
        User FH = [Select ID FROM User WHERE FirstName = 'Firehost' LIMIT 1];
        User API = [Select ID FROM User WHERE Alias = 'api' LIMIT 1];
        Map<Id, Account> mapAcc = new Map<Id, Account>();
        Id idAcc;
        String strStatus;
        String pline;
        Decimal amt;
        Set<Id> setIdAcc = new Set<Id>();
        List<AggregateResult> lstAR = new List<AggregateResult>();
        String ZID;
        
        // collect sum by account and status and product line
        for(AggregateResult objAR : [SELECT Zuora__Account__c , Zuora__Status__c,Product_Line__c, SUM(Total_Booking_Amount__c) 
                                    FROM Zuora__Subscription__c 
                                    WHERE Zuora__Status__c IN ('Cancelled', 'Active')
                                        AND Zuora__Account__c != NULL
                                        AND ((Zuora__ServiceActivationDate__c = YESTERDAY AND OpportunityId__c = null AND (Migrated__c = 'False' OR Migrated__c = null)) OR Zuora__TermEndDate__c = YESTERDAY)
                                       // AND Migrated__c = 'False'
                                     AND SameDayDeactivation__c = False
                                     AND isAcctDiscount__c = False
                                    GROUP BY ROLLUP(Zuora__Account__c, Zuora__Status__c, Product_Line__c)])
        {
            lstAR.add(objAR);
            setIdAcc.add((Id)objAR.get('Zuora__Account__c'));
        } // End of Aggregate For Loop
        
        // collect account infos
        if(!setIdAcc.isEmpty())
        {
            mapAcc = new Map<Id, Account>([SELECT Id, Name, OwnerId, Service_Lead__c FROM Account WHERE Id IN: setIdAcc]);   
        }
        
        // create opps
        for(AggregateResult objAR : lstAR)
        {
            idAcc = (Id)objAR.get('Zuora__Account__c');
            strStatus = (String)objAR.get('Zuora__Status__c');
            amt = (Double)objAR.get('expr0');
            pline = (String)objAR.get('Product_Line__c');
            
            if(strStatus == 'Cancelled' && !String.isBlank(mapAcc.get(idAcc).Service_Lead__c) && pline == 'Complete')
            {
                objOpp = new Opportunity();
                objOpp.RecordTypeId = idRecTypeDowngrade;
                objOpp.OwnerId = mapAcc.get(idAcc).Service_Lead__c;
                objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
                objOpp.AccountId = mapAcc.get(idAcc).Id;
                objOpp.Opportunity_Source__c = 'Portal';
                objOpp.Type = 'Downgrade';
                objOpp.Amount = amt * -1;
                objOpp.CloseDate = d1;
                objOpp.StageName = 'Cancelled';
                objOpp.Armor_Product_Category__c = 'Armor | '+pline;
                objOpp.Armor_Product__c = 'COMPLETE';
                objOpp.Armor_Anywhere_Location__c = 'None';
               // objOpp.ZuoraId__c = ZID;
                objOpp.Solution__c = 'General Security';
                objOpp.Auto_Bookings__c = true;
                objOpp.CreatedById = API.Id;
                lstOpp.add(objOpp);
                
            }
            else if(strStatus == 'Cancelled' && !String.isBlank(mapAcc.get(idAcc).Service_Lead__c) && pline == 'Anywhere')
            {
                objOpp = new Opportunity();
                objOpp.RecordTypeId = idRecTypeDowngrade;
                objOpp.OwnerId = mapAcc.get(idAcc).Service_Lead__c;
                objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
                objOpp.AccountId = mapAcc.get(idAcc).Id;
                objOpp.Opportunity_Source__c = 'Portal';
                objOpp.Type = 'Non Opportunity';
                objOpp.Amount = amt * -1;
                objOpp.CloseDate = d1;
                objOpp.StageName = 'Cancelled';
                objOpp.Armor_Product_Category__c = 'Armor | '+pline;
                objOpp.Armor_Product__c = 'CORE';
                objOpp.Armor_Anywhere_Location__c = 'Other';
                objOpp.Armor_Anywhere_Other_Location__c = 'Other';
               // objOpp.ZuoraId__c = ZID;
                objOpp.Solution__c = 'General Security';
                objOpp.Auto_Bookings__c = true;
                objOpp.CreatedById = API.Id;
                lstOpp.add(objOpp);    
            } 
            else if(strStatus == 'Cancelled' && String.isBlank(mapAcc.get(idAcc).Service_Lead__c) && pline == 'Complete')
            {
                objOpp = new Opportunity();
                objOpp.RecordTypeId = idRecTypeDowngrade;
                objOpp.OwnerId = FH.Id;
                objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
                objOpp.AccountId = mapAcc.get(idAcc).Id;
                objOpp.Opportunity_Source__c = 'Portal';
                objOpp.Type = 'Downgrade';
                objOpp.Amount = amt * -1;
                objOpp.CloseDate = d1;
                objOpp.StageName = 'Cancelled';
                objOpp.Armor_Product_Category__c = 'Armor | '+pline;
                objOpp.Armor_Product__c = 'COMPLETE';
                objOpp.Armor_Anywhere_Location__c = 'None';
               // objOpp.ZuoraId__c = ZID;
                objOpp.Solution__c = 'General Security';
                objOpp.Auto_Bookings__c = true;
                objOpp.CreatedById = API.Id;
                lstOpp.add(objOpp);    
            }
            else if(strStatus == 'Cancelled' && String.isBlank(mapAcc.get(idAcc).Service_Lead__c) && pline == 'Anywhere')
            {
                objOpp = new Opportunity();
                objOpp.RecordTypeId = idRecTypeDowngrade;
                objOpp.OwnerId = FH.Id;
                objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
                objOpp.AccountId = mapAcc.get(idAcc).Id;
                objOpp.Opportunity_Source__c = 'Portal';
                objOpp.Type = 'Non Opportunity';
                objOpp.Amount = amt * -1;
                objOpp.CloseDate = d1;
                objOpp.StageName = 'Cancelled';
                objOpp.Armor_Product_Category__c = 'Armor | '+pline;
                objOpp.Armor_Product__c = 'CORE';
                objOpp.Armor_Anywhere_Location__c = 'Other';
                objOpp.Armor_Anywhere_Other_Location__c = 'Other';
               // objOpp.ZuoraId__c = ZID;
                objOpp.Solution__c = 'General Security';
                objOpp.Auto_Bookings__c = true;
                objOpp.CreatedById = API.Id;
                lstOpp.add(objOpp);    
            }
            
            else if(strStatus == 'Active' && !String.isBlank(mapAcc.get(idAcc).Service_Lead__c) && pline == 'Complete')
            {
                objOpp = new Opportunity();
                objOpp.RecordTypeId = idRecTypeUpgrade;
                objOpp.OwnerId = mapAcc.get(idAcc).Service_Lead__c;
                objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
                objOpp.AccountId = mapAcc.get(idAcc).Id;
                objOpp.Opportunity_Source__c = 'Portal';
                objOpp.Type = 'Existing Business';
                objOpp.Amount = amt;
                objOpp.CloseDate = d1;
                objOpp.StageName = 'Closed Won';
                objOpp.Armor_Product_Category__c = 'Armor | '+pline;
                objOpp.Armor_Product__c = 'COMPLETE';
                objOpp.Armor_Anywhere_Location__c = 'None';
                objOpp.Closed_Comments__c = 'AMP Portal Self Service Upgrade';
               // objOpp.ZuoraId__c = ZID;
                objOpp.Solution__c = 'General Security';
                objOpp.Auto_Bookings__c = true;
                objOpp.CreatedById = API.Id;
                lstOpp.add(objOpp);
            }
            
            else if(strStatus == 'Active' && !String.isBlank(mapAcc.get(idAcc).Service_Lead__c) && pline == 'Anywhere')
            {
                objOpp = new Opportunity();
                objOpp.RecordTypeId = idRecTypeUpgrade;
                objOpp.OwnerId = mapAcc.get(idAcc).Service_Lead__c;
                objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
                objOpp.AccountId = mapAcc.get(idAcc).Id;
                objOpp.Opportunity_Source__c = 'Portal';
                objOpp.Type = 'Non Opportunity';
                objOpp.Amount = amt;
                objOpp.CloseDate = d1;
                objOpp.StageName = 'Closed Won';
                objOpp.Armor_Product_Category__c = 'Armor | '+pline;
                objOpp.Armor_Product__c = 'CORE';
                objOpp.Armor_Anywhere_Location__c = 'Other';
                objOpp.Armor_Anywhere_Other_Location__c = 'Other';
                objOpp.Closed_Comments__c = 'AMP Portal Self Service Upgrade';
               // objOpp.ZuoraId__c = ZID;
                objOpp.Solution__c = 'General Security';
                objOpp.Auto_Bookings__c = true;
                objOpp.CreatedById = API.Id;
                lstOpp.add(objOpp);
            }
            else if(strStatus == 'Active' && String.isBlank(mapAcc.get(idAcc).Service_Lead__c) && pline == 'Complete')
            {
                objOpp = new Opportunity();
                objOpp.RecordTypeId = idRecTypeUpgrade;
                objOpp.OwnerId = FH.Id;
                objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
                objOpp.AccountId = mapAcc.get(idAcc).Id;
                objOpp.Opportunity_Source__c = 'Portal';
                objOpp.Type = 'Existing Business';
                objOpp.Amount = amt;
                objOpp.CloseDate = d1;
                objOpp.StageName = 'Closed Won';
                objOpp.Armor_Product_Category__c = 'Armor | '+pline;
                objOpp.Armor_Product__c = 'COMPLETE';
                objOpp.Armor_Anywhere_Location__c = 'None';
                objOpp.Closed_Comments__c = 'AMP Portal Self Service Upgrade';
              //  objOpp.ZuoraId__c = ZID;
                objOpp.Solution__c = 'General Security';
                objOpp.Auto_Bookings__c = true;
                objOpp.CreatedById = API.Id;
                lstOpp.add(objOpp);
            }
            
            else if(strStatus == 'Active' && String.isBlank(mapAcc.get(idAcc).Service_Lead__c) && pline == 'Anywhere')
            {
                objOpp = new Opportunity();
                objOpp.RecordTypeId = idRecTypeUpgrade;
                objOpp.OwnerId = FH.Id;
                objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
                objOpp.AccountId = mapAcc.get(idAcc).Id;
                objOpp.Opportunity_Source__c = 'Portal';
                objOpp.Type = 'Non Opportunity';
                objOpp.Amount = amt;
                objOpp.CloseDate = d1;
                objOpp.StageName = 'Closed Won';
                objOpp.Armor_Product_Category__c = 'Armor | '+pline;
                objOpp.Armor_Product__c = 'CORE';
                objOpp.Armor_Anywhere_Location__c = 'Other';
                objOpp.Armor_Anywhere_Other_Location__c = 'Other';
                objOpp.Closed_Comments__c = 'AMP Portal Self Service Upgrade';
              //  objOpp.ZuoraId__c = ZID;
                objOpp.Solution__c = 'General Security';
                objOpp.Auto_Bookings__c = true;
                objOpp.CreatedById = API.Id;
                lstOpp.add(objOpp);
            }
        } // END of FOR Loop

        if(!lstOpp.isEmpty())
        {
            insert lstOpp;
        }
    } // End of CreateOpportunities Method 
} // End of Class

 
Hello Awesome Devs,

I have the followign Trigger that has ben working without error, but now that I have an need to update all of our Account records in our Org, I am hitting SOQL 101 errors a plenty.  Anyone have any thoughts on how to bulkify this code even more to stop these errors? 

Thank you for any help you are able to provide.....

Shawn

Trigger code - 
trigger SetAccountActiveOnContactRecord on Account (after insert, after update) {

    
    List<Contact> consToUpdate = new List<Contact>();
    set<id> accIds = new set<id>();
    For(Account a : Trigger.new){
        If(a.Status__c == 'Active'){
           accIds.add(a.id);
        }
    }
  
   List<Contact> consToAdd = [SELECT ID, Account_Active__c, AMP_Notification_Types__c, Contact_Types__c FROM Contact WHERE AccountId = : accIds  AND Account_Active__c != True AND 
                                       (AMP_Notification_Types__c INCLUDES('Technical','Billing','Account') OR Contact_Types__c INCLUDES('Technical'))];
            
            If(consToAdd.size()>0){
            For(Contact c : consToAdd){
                
                c.Account_Active__c = True;
                consToUpdate.add(c);
            
            }
            }
    
    if(consToUpdate.size()>0){
        update consToUpdate;
    }
    
    
}

 
Hello Devs, I have the following Trigger and Class that was working fine until this morning when I received the following error of Too Many future calls: 51. 

Any idea how to alleviate gettign this error?  Here is my trigger and class code....thank you all for any help you can provide....

Trigger:
 
trigger DeleteExtraBookingsTrigger on Opportunity (after insert) {

    For(Opportunity opps : Trigger.new){
        If(opps.Auto_Bookings__c == True){
            
            
        DeleteExtraBookingsClass.deleteRecords(Trigger.newMap.keySet());    
            
        }
    }
    
    
}

Class code:
 
public class DeleteExtraBookingsClass {

    @future
    public static void deleteRecords(Set<ID> oppIdsToDelete){
        List<Opportunity> opp = [SELECT ID, Armor_Product_Category__c From Opportunity WHERE ID IN : oppIdsToDelete AND Auto_Bookings__c = True
                                AND (Armor_Product_Category__c = null or Armor_Product_Category__c = 'Armor | null')];
        
        If(opp.size()>0){
        delete opp;
        database.emptyRecycleBin(opp);
        }
    }
    
}

Hello awesome devs! 

Need some help here as I am at a loss why the following Trigger is not setting values on all records but it setting a value on some of the records when doing a mass insert r or update.  When you go to a single record and perform an update the trigger always fires correctly.  

What is trying to be achieved:  

When a record on Object A is created or updated and it includes a SKU in a field called SKU__c .
Then trigger is called on Object A to find a Product from our standard Product2 object that have the same value in the ProductCode field as the SKU__c fiel don Object A.  
When found, a field (Product_Line__c) on Object A should be populated with the value form the found Product2 record and its Product_Line__c field. 

Again this works when updating records one by one, but anythign in mass, will only update a few records.  (Latest example: out of 1097 records in a mass update, only roughly 40 records got updated, the rest have nothign populated in the Product_Line__c field on Object A.

Can anyone look over my code, and help me out? :)

Thank you so much all,

Shawn

Trigger Code:
 
trigger PopulateProductLineOnCharge on Zuora__SubscriptionProductCharge__c (before insert, before update) {

    
    List<Zuora__SubscriptionProductCharge__c> spcToUpdate = new List<Zuora__SubscriptionProductCharge__c>();
    String pLineText = '';
    String SkuText = '';
    Product2 P;
    
    
    For(Zuora__SubscriptionProductCharge__c s : Trigger.new){
        If(s.Product_Line__c == null && s.SKU__c != null){
            SkuText = s.SKU__c;
        }
    }
    
    If(SkuText != null){
    P = [SELECT Id, Product_Line__c, ProductCode FROM Product2 WHERE ProductCode = : SkuText LIMIT 1];
    
    For(Zuora__SubscriptionProductCharge__c s2 : Trigger.New){
        If(SkuText.contains(s2.SKU__c)){
            s2.Product_Line__c = p.Product_Line__c;
            spcToUpdate.add(s2);
        }
        }
    }
 
}

 
Hello awesome devs! 

I have the followign trigger which works as intended, however when I do a mass upload or mass edit on more than 10 Lead records, I always get the "Apex CPU Time Limit Exceeded" error.  Can anyone tell me how to resolve this error in my code as if my users do a mass update of more than 10 lead records to assume ownership it throws this error and any other time an insert or update of more than 10 Lead records at a time. 

Thanks so much for any help you can provide,

Shawn

Trigger code:
 
trigger LeadCountOfTasks on Task (after delete, after insert, after undelete, after update) {
    Task [] tlist;
        if(Trigger.isDelete)
            tlist= Trigger.old;
        else
            tlist = trigger.new;
    set<id> lid = new set< id> ();
    
    If(!tlist.isEmpty()){
    for(Task ts : tlist)
    {   if(ts.WhoId!=null && ts.whatId == null)
    {
        If(string.valueOf(ts.WhoId).startsWith('00Q'))
        {
        lid.add(ts.Whoid);
    }
    }
    }
    If(lid.size()>0){
    Map <id,Task > tmap = new Map <id, Task>([select id, Whoid from Task where Whoid in:lid]);
    Map <id, Lead> lmap = new Map <id, Lead>([select id,Count_Activity__c from lead where id in:lid ]);
    
        If(lmap.size()>0){
       List<Lead> llist = [SELECT Id, Count_Activity__c FROM Lead WHERE ID IN:lmap.values()]; 
    for(lead l : lmap.values())
    {
        set<id> tids = new set <id>();
        for(Task tt : tmap.values())
        {
           if(tt.WhoId== l.Id)
            tids.add(tt.id);
        }
        if(l.Count_Activity__c!=tids.size())
        l.Count_Activity__c=tids.size();
        
              tlist = [select id, Whoid from task where whoid in:lid ];
        for(lead le :llist)
        {
            for(Task te:tlist)
            {
                if(tlist.size()>0)
                le.Count_Activity__c = tlist.size();
            }
        }
        
        
    }
    If(lmap.size()>0){
    update lmap.values();
    }
    }
    }
    }
}

 
Hello awesome devs!

I am attempting to create a VF page with 4 fields for user inputting.  2 of these fields are Date fields which when filled in like MM/DD/YYY when I press the search button to call my custom controller the date in the field populated has its format changed to Tue Dec 26 00:00:00 GMT 2017 and doesnt seem to be being applied in the search itself as all records are returned and not ones just for the date entered. 

Can someone help me with gettign the format to stay as I think this is the cause of the date field not being used in the search criteria. 

Please see my VF Page and controller code below. 

Thank you very much for any help you can provide,

Shawn

VF Page Code - 
 
<apex:page controller="SubSearchController">
    
    <apex:form>
    	<apex:pageBlock id="pb">
        	<apex:pageBlockButtons>
                <apex:commandButton action="{!searchRecords}" value="Search" rerender="pb"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="2">
                <apex:outputLabel value="Account Id" />
                <apex:inputText value="{!AccId}"/>
                <apex:outputLabel value="Status" />
                <apex:inputText value="{!SubStatus}"/>
                <apex:outputLabel value="Service Activation date" />
                <apex:inputText value="{!SAD}"/>
                <apex:outputLabel value="Term End Date" />
                <apex:inputText value="{!TermEnd}"/>
            </apex:pageBlockSection>
            <apex:pageBlockTable var="record" value="{!records}" id="pbTable">
                <apex:column value="{!record.Name}" />
                <apex:column value="{!record.Zuora__Status__c}" />
                <apex:column value="{!record.Zuora__ServiceActivationDate__c}" />
                <apex:column value="{!record.Zuora__TermEndDate__c}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
    
</apex:page>

Controller Code -

public class SubSearchController {

    public String AccId {get;set;}
    public String SubStatus {get;set;}
    public Date SAD {get;set;}
    public Date sadDate = Date.valueOf(SAD);
    public Date TermEnd {get;set;}
    public Date TermDate = Date.valueOf(TermEnd);
    
    public List<Zuora__Subscription__c> records {get; private set;}
    
    public SubSearchController(){
        records = new List<Zuora__Subscription__c>();   
    }
    
    public PageReference searchRecords() {
        
        records = [SELECT Zuora__Account__c, Zuora__Status__c, Zuora__ServiceActivationDate__c, Zuora__TermEndDate__c, OpportunityId__c, Total_Booking_Amount__c, Id, Name
                  FROM Zuora__Subscription__c WHERE Zuora__Account__c = : AccId AND Zuora__Status__c = : SubStatus AND (Zuora__ServiceActivationDate__c = : sadDate OR Zuora__TermEndDate__c = :TermDate)];
        return null;
        
    }
    
}

 
Hello awesome developers...I am not sure why I am gettign SOQL 101 Errors on the following trigger.  

Can anyone give some advice on what to try to ensure we are not gettign this error. 

What I am tring to do is if an account goes to an active state after an update, it should go and grab all contacts that meet some criteria and update a field on all of the contact records. 

Any help would be greatly appreciated...

Thanks in advance,

Shawn

Trigger code - 
 
trigger SetAccountActiveOnContactRecord on Account (after insert, after update) {

    
    List<Contact> consToUpdate = new List<Contact>();
    
    For(Account a : Trigger.new){
        If(a.Status__c == 'Active'){
            List<Contact> consToAdd = [SELECT ID, Account_Active__c, AMP_Notification_Types__c, Contact_Types__c FROM Contact WHERE AccountId = : a.Id AND Account_Active__c != True AND 
                                       (AMP_Notification_Types__c INCLUDES('Technical','Billing','Account') OR Contact_Types__c INCLUDES('Technical')) LIMIT 100];
            
            If(consToAdd.size()>0){
            For(Contact c : consToAdd){
                
                c.Account_Active__c = True;
                consToUpdate.add(c);
            
            }
            }
        }
    }
    
    if(consToUpdate.size()>0){
        update consToUpdate;
    }
    
    
}

 
Hello Awesome Devs!!!

I have the following Scheduled Class, and test class, and I am only able to get the code coverage on this class to 70 percent.  Looks like the issue is in my IF , ELSE IF statements, as the Null check always tests out, but if I am stating that the field is not null/blank then the code aftere that doe snot get covered.  

Can anyone help me figure out what is going on here and help to get this above the acceptable coverage limit?

Appreciate any help you can provide,

Shawn

Class - 
 
global class CreateUpgradeDowngradeOpportunities Implements Schedulable
{
    global void execute(SchedulableContext sc)
    {
        CreateOpportunities();
    }
      
    public void CreateOpportunities()
    {
        //Variable & List Declarations
        
        Date Today = Date.Today();
        Date d1 = Today.addDays(-1);
		List<Opportunity> lstOpp = new List<Opportunity>();
		Opportunity objOpp;
		Id idRecTypeDowngrade = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('AMP Downgrade').getRecordTypeId();
		Id idRecTypeUpgrade = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('AMP Upgrade').getRecordTypeId();
        User FH = [Select ID FROM User WHERE FirstName = 'Firehost' LIMIT 1];
		Map<Id, Account> mapAcc = new Map<Id, Account>();
		Id idAcc;
		String strStatus;
		Decimal amt;
		Set<Id> setIdAcc = new Set<Id>();
		List<AggregateResult> lstAR = new List<AggregateResult>();
		
		// collect sum by account
		for(AggregateResult objAR : [SELECT Zuora__Account__c , Zuora__Status__c, SUM(Zuora__MRR__C) 
									FROM Zuora__Subscription__c 
									WHERE Zuora__Status__c IN ('Cancelled', 'Active')
										AND Zuora__Account__c != NULL
										AND (Zuora__TermStartDate__c = YESTERDAY OR Zuora__TermEndDate__c = YESTERDAY)
                                     // Add additional Criteria here..
									GROUP BY ROLLUP(Zuora__Account__c, Zuora__Status__c)])
		{
			lstAR.add(objAR);
			setIdAcc.add((Id)objAR.get('Zuora__Account__c'));
		} // End of Aggregate For Loop
		
		// collect account infos
		if(!setIdAcc.isEmpty())
		{
			mapAcc = new Map<Id, Account>([SELECT Id, Name, OwnerId, Service_Lead__c FROM Account WHERE Id IN: setIdAcc]);   
		}
		
		// create opps
		for(AggregateResult objAR : lstAR)
		{
			idAcc = (Id)objAR.get('Zuora__Account__c');
			strStatus = (String)objAR.get('Zuora__Status__c');
			amt = (Decimal)objAR.get('expr0');
            
			if(strStatus == 'Cancelled' && !String.isBlank(mapAcc.get(idAcc).Service_Lead__c))
			{
				objOpp = new Opportunity();
				objOpp.RecordTypeId = idRecTypeDowngrade;
				objOpp.OwnerId = mapAcc.get(idAcc).Service_Lead__c;
				objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
				objOpp.AccountId = mapAcc.get(idAcc).Id;
				objOpp.Opportunity_Source__c = 'Portal';
				objOpp.Type = 'Downgrade';
				objOpp.Amount = amt * -1;
				objOpp.CloseDate = d1;
				objOpp.StageName = 'Cancelled';    
				lstOpp.add(objOpp);    
			}
            else if(strStatus == 'Cancelled' && String.isBlank(mapAcc.get(idAcc).Service_Lead__c))
			{
				objOpp = new Opportunity();
				objOpp.RecordTypeId = idRecTypeDowngrade;
				objOpp.OwnerId = FH.Id;
				objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
				objOpp.AccountId = mapAcc.get(idAcc).Id;
				objOpp.Opportunity_Source__c = 'Portal';
				objOpp.Type = 'Downgrade';
				objOpp.Amount = amt * -1;
				objOpp.CloseDate = d1;
				objOpp.StageName = 'Cancelled';    
				lstOpp.add(objOpp);    
			}
			else if(strStatus == 'Active' && !String.isBlank(mapAcc.get(idAcc).Service_Lead__c))
			{
				objOpp = new Opportunity();
				objOpp.RecordTypeId = idRecTypeUpgrade;
				objOpp.OwnerId = mapAcc.get(idAcc).Service_Lead__c;
				objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
				objOpp.AccountId = mapAcc.get(idAcc).Id;
				objOpp.Opportunity_Source__c = 'Portal';
				objOpp.Type = 'Existing Business';
				objOpp.Amount = amt;
				objOpp.CloseDate = d1;
				objOpp.StageName = 'Closed Won';
                objOpp.Closed_Comments__c = 'AMP Portal Self Service Upgrade';
				lstOpp.add(objOpp);
			}
            else if(strStatus == 'Active' && String.isBlank(mapAcc.get(idAcc).Service_Lead__c))
			{
				objOpp = new Opportunity();
				objOpp.RecordTypeId = idRecTypeUpgrade;
				objOpp.OwnerId = FH.Id;
				objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
				objOpp.AccountId = mapAcc.get(idAcc).Id;
				objOpp.Opportunity_Source__c = 'Portal';
				objOpp.Type = 'Existing Business';
				objOpp.Amount = amt;
				objOpp.CloseDate = d1;
				objOpp.StageName = 'Closed Won';
                objOpp.Closed_Comments__c = 'AMP Portal Self Service Upgrade';
				lstOpp.add(objOpp);
			}
		} // END of FOR Loop

		if(!lstOpp.isEmpty())
		{
			insert lstOpp;
		}
    } // End of CreateOpportunities Method 
} // End of Class

Test Class -
 
@isTest(SeeAllData = True)
public class CreateUpgradeDowngradeOpportunitiesTest {

    List<Zuora__Subscription__c> subs = new List<Zuora__Subscription__c>();
    
    
    private static testMethod void tm1(){
        
    	Date Today = Date.Today();
    	Date d1 = Today.addDays(-1);
        User u = [SELECT ID FROM USER WHERE FirstName = 'FireHost' LIMIT 1];
        
        Account a = New Account();
        a.Name = 'Test Account';
        a.Status__c = 'Active';
        insert a;
        
        Account a2 = New Account();
        a2.Name = 'Test Account 2';
        a.Status__c = 'Active';
        a.Service_Lead__c = u.Id;
        insert a2;
        
        Zuora__Subscription__c s = new Zuora__Subscription__c();
        s.Zuora__Account__c = a.Id;
        s.Zuora__Status__c = 'Active';
        s.Zuora__MRR__c = 100;
        s.Zuora__TermStartDate__c = d1;
        insert s;
        
        Zuora__Subscription__c s2 = new Zuora__Subscription__c();
        s2.Zuora__Account__c = a.Id;
        s2.Zuora__Status__c = 'Cancelled';
        s2.Zuora__MRR__c = 100;
        s2.Zuora__TermStartDate__c = d1;
        insert s2;
        
        Zuora__Subscription__c s3 = new Zuora__Subscription__c();
        s3.Zuora__Account__c = a2.Id;
        s3.Zuora__Status__c = 'Active';
        s3.Zuora__MRR__c = 100;
        s3.Zuora__TermStartDate__c = d1;
        insert s3;
        
        Zuora__Subscription__c s4 = new Zuora__Subscription__c();
        s4.Zuora__Account__c = a2.Id;
        s4.Zuora__Status__c = 'Cancelled';
        s4.Zuora__MRR__c = 100;
        s4.Zuora__TermStartDate__c = d1;
        insert s4;


        String CRON_EXP = '0 0 0 15 3 ? *';
        

		String jobId = System.schedule('ScheduleApexClassTest',  CRON_EXP, new CreateUpgradeDowngradeOpportunities());
		CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
		System.assertEquals(CRON_EXP, ct.CronExpression);
		System.assertEquals(0, ct.TimesTriggered);

    }
    
}

Hello awesome devs...I have a good one here today! 

The company has had a Consulting group create a VF page and controller to do the following function.  It seems that thier Test class was removed from our environment somehow as this is in our production org, however no test class provides any code coverage any longer.  I need to make a small edit to the VF page, and want to ensure that the controller has proper coverage after I make the small tweak, but again I can not find any test class that provides coverage.  Thsi was done a few years back so the change sets that included this info has expired and we no longer have visibility into it.  So I need to create a new test class, but am unsure how to for this piece of particular code. 

Can anyone help to help me create a test class for the folllowing code?

On Object A - the new button is pressed, which navigates you to the VF page which allows you to either

A: select an existing account with a specific record type or:
B: if not found the next section of the VF page will allow you to create a new Account record to use for the custom object upon saving the VF page. 

Here is the controller code for this VF page
 
public class new_testVfpage {
    
    Public Purchase_request__c pr{get;set;}
    Public string accountname{get;set;}
    Public string phonenumber{get;set;}
    Public string Billingstreet{get;set;}
    Public string Billingcity{get;set;}
    public string contactname{get;set;}
    public string contactemail{get;set;}
    Public String billingzipcode{get;set;}
    public Account account {get;set;}
    Public string testfield{get;set;}
    public boolean isExistError{get;set;}
    public boolean isNullError{get;set;}

    public new_testVfpage(ApexPages.StandardController sc) {
        pr= (Purchase_request__c )sc.getRecord();
        account=new account();
        pr.Renewal__c='Existing Vendor';
        Id rtid= Schema.SObjectType.Purchase_request__c.getRecordTypeInfosByName().get('PR Existing Vendor, No V-Qs /w Submit').getRecordTypeId();
        pr.recordtypeid =rtid;
        isExistError=false;
        
    }
    
    Public pagereference newsave(){
        integer needDesc = 0;
        integer descMax = 0;
        testfield=pr.Vendor_Name__c;

        //Identifiy if Insert or Update Vendor Type of Record Type
        Id vendorId = Schema.SobjectType.Account.getRecordTypeInfosByName().get('Vendor').getRecordTypeId();
        List<Account> accList = [SELECT Id FROM Account where Id = :pr.Vendor_Name__c AND RecordType.Id =: vendorId];

        if(accList.IsEmpty()){//insert
            Id prrtid= Schema.SObjectType.Purchase_request__c.getRecordTypeInfosByName().get('PR All Questions /w Submit Button').getRecordTypeId();
            pr.RecordTypeId = prrtid; //'01270000000UQfyAAG'; //PR All Questions /w Submit Button
        }else{//Update
            Id prrtid = Schema.SObjectType.Purchase_request__c.getRecordTypeInfosByName().get('PR Existing Vendor, No V-Qs /w Submit').getRecordTypeId();
            pr.RecordTypeId = prrtid; //'01270000000UQg1AAG'; //PR Existing Vendor, No V-Qs /w Submit
        }
        
       //CheckVendor Name is Empty
       if(testfield == null || testfield ==''){   
          //When Account Name has value    
          if(accountname!= '' && accountname!= null){
              account.name=accountname;
              isExistError=false;
              //New Vendor will check the following fields
              if((account.Will_vendor_store_NPPI_or_PCI_data__c != 'Yes' && account.Will_vendor_store_NPPI_or_PCI_data__c != 'No')){
                  ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor store NPPI or PCI data? is required'));
                  isExistError=true;
              }
              
              if((account.Will_vendor_store_PHI_data__c != 'Yes' && account.Will_vendor_store_PHI_data__c != 'No')){
                  ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor store PHI data?'));
                  isExistError=true;
              }
              
              if((account.Will_vendor_store_Armor_corporate_data__c != 'Yes' && account.Will_vendor_store_Armor_corporate_data__c != 'No')){
                  ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor store Armor corporate data? is required'));
                  isExistError=true;
              }
              
              if((account.Will_vendor_access_Armor_or_Cust_data__c != 'Yes' && account.Will_vendor_access_Armor_or_Cust_data__c != 'No')){
                  ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Will vendor access Armor or Customer Data? is required'));
                  isExistError=true; 
              }
              
              if((account.Are_vendor_services_Critical_to_Armor__c!= 'Yes' && account.Are_vendor_services_Critical_to_Armor__c!= 'No')){
                  ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Are vendor services critical to Armor? is required'));
                  isExistError=true;
              }
              //removes error if all the condition of required fields for new vendor is Yes
              if((account.Will_vendor_store_NPPI_or_PCI_data__c != '' && account.Will_vendor_store_NPPI_or_PCI_data__c != null && account.Will_vendor_store_NPPI_or_PCI_data__c != '--None--')
              && (account.Will_vendor_store_PHI_data__c != '' && account.Will_vendor_store_PHI_data__c != null && account.Will_vendor_store_PHI_data__c != '--None--')
              && (account.Will_vendor_store_Armor_corporate_data__c != '' && account.Will_vendor_store_Armor_corporate_data__c != null && account.Will_vendor_store_Armor_corporate_data__c != '--None--')
              && (account.Will_vendor_access_Armor_or_Cust_data__c != '' && account.Will_vendor_access_Armor_or_Cust_data__c != null && account.Will_vendor_access_Armor_or_Cust_data__c != '--None--')
              && (account.Are_vendor_services_Critical_to_Armor__c != '' && account.Are_vendor_services_Critical_to_Armor__c != null && account.Are_vendor_services_Critical_to_Armor__c != '--None--')){
                  isExistError=false;
              }
              
                //Error if NPPI/PCI desc is null when account.Will_vendor_store_PHI_data__c value is yes
                if(account.Will_vendor_store_NPPI_or_PCI_data__c == 'Yes' ){
                    if(account.NPPI_PCI_If_Yes_enter_description__c != null){
                       isExistError=false;
                       isNullError=false;
                    }else{
                        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'NPPI/PCI is set to Yes please enter description'));
                        isExistError=true;
                        isNullError=true;
                    }
            
                }else{
                    if(account.Will_vendor_store_NPPI_or_PCI_data__c == 'No'){
                        isExistError=false;
                        isNullError=false;
                    }else{
                        isExistError=true;
                    }
                }
            
                //Error if PHI desc is null when account.Will_vendor_store_PHI_data__c value is yes
                //if(account.Will_vendor_store_PHI_data__c == 'Yes' ){
                if(account.Will_vendor_store_PHI_data__c == 'Yes' ){
                    if(account.PHI_If_Yes_enter_description__c != null){
                       if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'PHI is set to Yes please enter description'));
                        isExistError=true;
                        isNullError=true; 
                    }
                
                }else{
                    if(account.Will_vendor_store_PHI_data__c == 'No'){
                        if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        isExistError=true;
                    }
                }
            
                //Error if Corp Data desc is null when Corp Data value is yes
                if(account.Will_vendor_store_Armor_corporate_data__c == 'Yes' ){
                    if(account.Corp_Data_If_Yes_enter_description__c != null){
                       if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Corp Data is set to Yes please enter description'));
                        isExistError=true;
                        isNullError=true; 
                    }
                
                }else{
                    if(account.Will_vendor_store_Armor_corporate_data__c == 'No'){
                        if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        isExistError=true;
                    }
                }
            
                //Error if Access Data desc is null when Access Data value is yes
                if(account.Will_vendor_access_Armor_or_Cust_data__c == 'Yes' ){
                    if(account.Cust_Data_If_Yes_enter_description__c != null){
                       if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Access Data is set to Yes please enter description'));
                        isExistError=true;
                        isNullError=true; 
                    }
                
                }else{
                    if(account.Will_vendor_access_Armor_or_Cust_data__c == 'No'){
                        if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        isExistError=true; 
                    }
                }
                
                //Error if Critical desc is null when account.Are_vendor_services_Critical_to_Armor__c value is yes
                if(account.Are_vendor_services_Critical_to_Armor__c == 'Yes' ){
                    if(account.Critical_If_Yes_enter_description__c != null){
                       if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Critical is set to Yes please enter description'));
                        isExistError=true;
                        isNullError=true;    
                    }
                
                }else{
                    if(account.Are_vendor_services_Critical_to_Armor__c == 'No'){
                        if(isNullError==false){
                           isExistError=false;
                           isNullError=false;
                       }
                    }else{
                        isExistError=true;   
                    }
                }
                
                //removes error if all required fields are not null
                if((account.Will_vendor_store_NPPI_or_PCI_data__c == 'Yes')
                  && (account.Will_vendor_store_PHI_data__c == 'Yes' )
                  && (account.Will_vendor_store_Armor_corporate_data__c == 'Yes')
                  && (account.Will_vendor_access_Armor_or_Cust_data__c == 'Yes')
                  && (account.Are_vendor_services_Critical_to_Armor__c == 'Yes')){
                    //Description null check                      
                    if((account.NPPI_PCI_If_Yes_enter_description__c != null && account.NPPI_PCI_If_Yes_enter_description__c != '')
                     &&(account.PHI_If_Yes_enter_description__c != null && account.PHI_If_Yes_enter_description__c != '')
                     &&(account.Corp_Data_If_Yes_enter_description__c != null && account.Corp_Data_If_Yes_enter_description__c != '')
                     &&(account.Cust_Data_If_Yes_enter_description__c != null && account.Cust_Data_If_Yes_enter_description__c != '')
                     &&(account.Critical_If_Yes_enter_description__c != null && account.Critical_If_Yes_enter_description__c != '')
                    ){
                        isExistError=false;
                    }else{
                        isExistError=true;
                    }
                 }
          }else{
              ApexPages.addMessage(new apexpages.message(ApexPages.Severity.ERROR, 'Account Name is required'));
              isExistError=true;
          }
          
          
                
        Id accrtid= Schema.SObjectType.Account.getRecordTypeInfosByName().get('Vendor').getRecordTypeId();
            account.recordtypeid=accrtid;
       
        contact c=new contact(); 
        if(contactname!= '' && contactname !=null){
           c.lastName=contactname;
        }
        if(isExistError==true){
            return null;
        }else{    
         upsert account;
         Upsert Pr; 
        }
         
        if(contactname != '' && contactname !=null){
        c.accountid=account.id;
        c.LeadSource='PR VF Page - Manually';
        insert c;
        }
         
        pr.Vendor_Name__c=account.id;
        pr.Renewal__c='New Vendor';
        upsert Pr; 
        }
        if(accountname!= '' && accountname!= null){
            upsert Pr;
            return new PageReference('/'+pr.Id+'/e?retURL=%2F'+pr.Id); 
        }else{
            if(testfield == null || testfield ==''){
                return null; 
            }else{
                upsert Pr;
                return new PageReference('/'+pr.Id+'/e?retURL=%2F'+pr.Id); 
            } 
        }   
             
    }
    
}

 
Awesome developers!

I have the following trigger that is operating as intended, however every now and then I receive an Null pointer exection error on line 21 of the following code.  I can not trace this back to anything and the code looks to be looking for nulls but again I am not a very seasoned developer with code so I am hopign someone can help share why this error is generated every now and then.  

Appreciate the help!
trigger AcctPaymentRollup on Zuora__Payment__c (after insert, after update, before delete, after unDelete) {
    List<Id> AccIds=new List<Id>();
    
If(Trigger.isInsert || Trigger.isUpdate || Trigger.isUnDelete) {
    for(Zuora__Payment__c mst:Trigger.New){
    if(mst.Zuora__Account__c !=null && mst.Zuora__AppliedInvoiceAmount__c != null && mst.Zuora__GatewayStatus__c == 'Submitted' && mst.Zuora__PaymentMethod__c != 'Check'){
     AccIds.add(mst.Zuora__Account__c);
    }
        else if (mst.Zuora__Account__c != null && mst.Zuora__AppliedInvoiceAmount__c != null && (mst.Zuora__PaymentMethod__c == 'Check' || string.ValueOf(mst.Zuora__PaymentMethod__c).startsWith('ACH'))){
            AccIds.add(mst.Zuora__Account__c);
        }
    }
   

   Map<Id,Account> AccMap = new Map<Id,Account>([select id,Current_Invoice_Amount_Paid__c from Account where id in:AccIds]);
   
   for(Zuora__Payment__c mst:Trigger.New)
   {
        if(AccMap != NULL && !AccMap.IsEmpty())
        {
            AccMap.get(mst.Zuora__Account__c).Current_Invoice_Amount_Paid__c = mst.Zuora__AppliedInvoiceAmount__c;
        
        }
   }

   if(AccMap != NULL && !AccMap.IsEmpty())
   {
        update AccMap.values();
   }}
   
   If(Trigger.isDelete) {
    for(Zuora__Payment__c mst:Trigger.Old){
    if(mst.Zuora__Account__c !=null && mst.Zuora__GatewayStatus__c == 'Submitted' && mst.Zuora__PaymentMethod__c != 'Check'){
     AccIds.add(mst.Zuora__Account__c);
    }
    else if (mst.Zuora__Account__c != null && (mst.Zuora__PaymentMethod__c == 'Check' || string.ValueOf(mst.Zuora__PaymentMethod__c).startsWith('ACH'))){
     AccIds.add(mst.Zuora__Account__c);   
        }
    }
   

   Map<Id,Account> AccMap = new Map<Id,Account>([select id,Current_Invoice_Amount_Paid__c from Account where id in:AccIds]);
   
   for(Zuora__Payment__c mst:Trigger.Old)
   {
        if(AccMap != NULL && !AccMap.IsEmpty())
        {
            AccMap.get(mst.Zuora__Account__c).Current_Invoice_Amount_Paid__c = AccMap.get(mst.Zuora__Account__c).Current_Invoice_Amount_Paid__c - mst.Zuora__AppliedInvoiceAmount__c;
        }
   }

   if(AccMap != NULL && !AccMap.IsEmpty())
   {
        update AccMap.values();
   }}

}

 
Devs,

I am wondering if there is a way to create an Apex class that will upon creating a new sandbox or refreshing an existing sandbox, that will scour the Email template object and find any email addresses in the Additional email Address fields specificaly for workflow email alerts and then edit them to make then un-usable just as the email addresses for users is edited to include somethign like @example.com.com or somethign of our choosing to ensure that a lot of manual tasks of running through each email alert to either edit manaully or remove completely for testing. 

I have not been able to find any help on this and wasnt sure if anyone has been able to do this somehow,

Thanks for your time,


Shawn
Hello great Devs out there!

I have the following Schedulable apex class that I need a test class for.  I am attaching the test class I have attempted but I am getting errors.  Can anyone tell me if this is the correct approach for a test class for the following code as I have written test classes before but not for scheduled code so what am I missing?  

Thanks for helping this newbie at code out!

Shawn

Scheduled Class Code - 
 
global class CreateUpgradeDowngradeOpportunities Implements Schedulable
{
    global void execute(SchedulableContext sc)
    {
        CreateOpportunities();
    }
      
    public void CreateOpportunities()
    {
        //Variable & List Declarations
        
        Date Today = Date.Today();
        Date d1 = Today.addDays(-1);
		List<Opportunity> lstOpp = new List<Opportunity>();
		Opportunity objOpp;
		Id idRecTypeDowngrade = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('AMP Downgrade').getRecordTypeId();
		Id idRecTypeUpgrade = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('AMP Upgrade').getRecordTypeId();
		Map<Id, Account> mapAcc = new Map<Id, Account>();
		Id idAcc;
		String strStatus;
		Decimal amt;
		Set<Id> setIdAcc = new Set<Id>();
		List<AggregateResult> lstAR = new List<AggregateResult>();
		
		// collect sum by account
		for(AggregateResult objAR : [SELECT Zuora__Account__c , Zuora__Status__c, SUM(Zuora__MRR__C) 
									FROM Zuora__Subscription__c 
									WHERE Zuora__Status__c IN ('Cancelled', 'Active')
										AND Zuora__Account__c != NULL
										AND (Zuora__TermStartDate__c = YESTERDAY OR Zuora__TermEndDate__c = YESTERDAY)
									GROUP BY ROLLUP(Zuora__Account__c, Zuora__Status__c)])
		{
			lstAR.add(objAR);
			setIdAcc.add((Id)objAR.get('Zuora__Account__c'));
		} // End of Aggregate For Loop
		
		// collect account infos
		if(!setIdAcc.isEmpty())
		{
			mapAcc = new Map<Id, Account>([SELECT Id, Name, OwnerId FROM Account WHERE Id IN: setIdAcc]);
		}
		
		// create opps
		for(AggregateResult objAR : lstAR)
		{
			idAcc = (Id)objAR.get('Zuora__Account__c');
			strStatus = (String)objAR.get('Zuora__Status__c');
			amt = (Decimal)objAR.get('expr0');
			if(strStatus == 'Cancelled')
			{
				objOpp = new Opportunity();
				objOpp.RecordTypeId = idRecTypeDowngrade;
				objOpp.OwnerId = mapAcc.get(idAcc).OwnerId;
				objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
				objOpp.AccountId = mapAcc.get(idAcc).Id;
				objOpp.Opportunity_Source__c = 'Portal';
				objOpp.Type = 'Downgrade';
				objOpp.Amount = amt * -1;
				objOpp.CloseDate = d1;
				objOpp.StageName = 'Cancelled';    
				lstOpp.add(objOpp);    
			}
			else if(strStatus == 'Active')
			{
				objOpp = new Opportunity();
				objOpp.RecordTypeId = idRecTypeUpgrade;
				objOpp.OwnerId = mapAcc.get(idAcc).OwnerId;
				objOpp.Name = mapAcc.get(idAcc).Name + ' - AMP';
				objOpp.AccountId = mapAcc.get(idAcc).Id;
				objOpp.Opportunity_Source__c = 'Portal';
				objOpp.Type = 'Existing Business';
				objOpp.Amount = amt;
				objOpp.CloseDate = d1;
				objOpp.StageName = 'Closed Won';
                objOpp.Closed_Comments__c = 'AMP Portal Self Service Upgrade';
				lstOpp.add(objOpp);
			} 
		} // END of FOR Loop

		if(!lstOpp.isEmpty())
		{
			insert lstOpp;
		}
    } // End of CreateOpportunities Method 
} // End of Class

My Attempt at the test class for this scheduled class. as you can see I create an Accoutn record and date variables and then use them in the Subscription record as needed, and then run my scheduled class.  Thought this would have done the trick....but nope...
 
@isTest
public class CreateUpgradeDowngradeOpportunitiesTest {

    List<Zuora__Subscription__c> subs = new List<Zuora__Subscription__c>();
    
    
    private static testMethod void tm1(){
        
    	Date Today = Date.Today();
    	Date d1 = Today.addDays(-1);
        
        Account a = New Account();
        a.Name = 'Test Account';
        a.Status__c = 'Active';
        insert a;
        
        Zuora__Subscription__c s = new Zuora__Subscription__c();
        s.Zuora__Account__c = a.Id;
        s.Zuora__Status__c = 'Active';
        s.Zuora__MRR__c = 100;
        s.Zuora__TermStartDate__c = d1;
        insert s;
        
        CreateUpgradeDowngradeOpportunities sc= new CreateUpgradeDowngradeOpportunities();
		sc.execute(null);
        
    }
    
    
    
    
}



Hello Awesome Developers.....

I have the following trigger that should be updating an Apttus Quote Record based on Line Items that are added.  

Ultimately what I am trying to achieve is when a new Apttus config file is created (gets created when a cart is finalized) and is marked finalized for its status, look for the line Items associated with that Apttus config record, and if any of those items has a particualr product code through the product selected to be used and its quantity is greater that 3 then add those records to a list and then for that list update the master Quote record to have a checkbox checked true which will then require an approval to be done.  

Looks like through my debug statements I can see that the records and lists are being treated fine up until line 45 as the master quote record does not get updated.  

Any help on this would be greatly appreciated, and if you need any mor einformation, please ask,

Thanks again,

Shawn

Trigger Code:
 
trigger ColocationCrossConnectApproval on Apttus_Config2__ProductConfiguration__c (before insert, before update) {

    List<Apttus_Config2__LineItem__c> colocationList = new List<Apttus_Config2__LineItem__c>();
    	system.debug('colocationList Size Initial -'+colocationList.size());
    List<Apttus_Config2__LineItem__c> crossConnectList = new List<Apttus_Config2__LineItem__c>();
    	system.debug('crossConnectList Size Initial -'+crossConnectList.size());
    List<Apttus_Config2__ProductConfiguration__c> configRecords = new List<Apttus_Config2__ProductConfiguration__c>();
    	system.debug('configRecords Size Initial -'+configRecords.size());
    
    For(Apttus_Config2__ProductConfiguration__c config : Trigger.new){
        
        If(config.Apttus_Config2__Status__c == 'Finalized'){
                configRecords.add(config);
            	system.debug('configRecords Size After -'+configRecords.size());
        } // End of Status = Finalized If Statement
     } // End of Trigger.New For Loop
    
    If(configRecords.size()>0){
  
    	For(Apttus_Config2__ProductConfiguration__c configToWork : configRecords){
        
            List<Apttus_Config2__LineItem__c> colocationItems = [SELECT ID, Name, Apttus_Config2__OptionId__c,Apttus_Config2__OptionId__r.ProductCode,
                                                                 Apttus_Config2__ConfigurationId__c,Apttus_Config2__Quantity__c,
                                                                 Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__r.ColocationApprovalRequired__c,
                                                                 Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__r.CrossConnectApprovalRequired__c 
                                                                 FROM Apttus_Config2__LineItem__c 
                                                                 WHERE Apttus_Config2__ConfigurationId__c IN : configRecords];
            system.debug('colocationItems Size -' + colocationItems.size());
            
            For(Apttus_Config2__LineItem__c coLoItems : colocationItems){
                
                If(coLoItems.Apttus_Config2__OptionId__r.ProductCode == 'SAN-001' && coLoItems.Apttus_Config2__Quantity__c > 3){
                    colocationList.add(coLoItems);
                    	system.debug('colocationList Size After -'+colocationList.size());
                } // End Of Colocation If Statement To Add CoLocation Line Items to colocationList
                
                else If(coLoItems.Apttus_Config2__OptionId__r.ProductCode == 'SAN-002' && coLoItems.Apttus_Config2__Quantity__c > 3) {
                    crossConnectList.add(coLoItems);
                    	system.debug('crossConnectList Size After -'+crossConnectList.size());
                } // End of Else If Statement to add Cross Connect Lines to crossConnectList   
            } // End of Line For Loop   
    	} // End of configToWork For Loop
    } // End Of Bulkify If Statement
    
    If(colocationList.size()>0){
        For(Apttus_Config2__LineItem__c finalcoLoList : colocationList){
            finalcoLoList.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__r.ColocationApprovalRequired__c = True;
            Database.update(finalcoLoList);
        } // End of finalcoLoList For Loop
    } // End of colocationList size If Statement
    
    If(crossConnectList.size()>0){
        For(Apttus_Config2__LineItem__c finalcrossList : crossConnectList){
            finalcrossList.Apttus_Config2__ConfigurationId__r.Apttus_QPConfig__Proposald__r.CrossConnectApprovalRequired__c = True;
            Database.update(finalcrossList);
        } // End of finalcrossList For Loop
    } // End of crossconnectList size If Statement    
} // End Of Trigger

 
Hello Awesome Developers! 

I am hoping one of you heroes out there can help me with a requirmeent I have.  Let me be up front, I have not been developing in apex for long and have not had any other developign experience with code in any other language or platform, so in other words a newbie. :)

So here is the deal, I have a need to have opportnities created every night based upon another object having records created the previous day. This object is a Zuora application object called Subscriptions and a record would be created whenever a customer of ours would use our self servic eportal (outside of Salesforce) to perform an upgrade or downgrade of thier services.  This action by the customer will create a Subscription record in Salesforce and when those get created I need every night to create an Upgrade or downgrade Opportunity from those subscription records created. Also we need to batch the same customer so if they do 3 upgrades, we take the amount from all three subscription records and sum them so that we only create one opportunity per customer for upgrades and an opportunity for downgrades and then have the Amoutn field on th enewly created opp be the total sum of the Upgrade subscription records and if there are downgrades, the same would need to happen to sum all of those downgrade record amounts into one opp and the amount would be that SUM.  

Please see my attempt at this code below, where I am setting the class as a scheduled class, and when I run this in the Dev console no opps are bneing created even though I have subscription records I manually created for this test.  That is issue number one, gettign this to create opps, but I havent gotten as far to test if the Aggregate function being used will do the magic of SUMing thw amounts and only creating one opportunity per account for all upgrades and one opp for all downgrades for that account. 

I hope this makes sense, I am just tryign to once a night, grab all subscriptions created for that day (Actually this will run at 2AM so createddate would be the day before) and then rn though those Subscription records and create one opp per account and have the amoutn be the sum for both types (Upgrades and downgrades) 

Thank you very much for your help! 

Shawn
 
global class CreateUpgradeDowngradeOpportunities Implements Schedulable
{
     global void execute(SchedulableContext sc)
      {
            CreateOpportunities();
      }
      
      public void CreateOpportunities()
      {
         Date d1 = system.today()-1;
         List<Zuora__Subscription__c> SubsCreated = [SELECT Id, Name, Zuora__Account__c, Zuora__MRR__c, Zuora__Status__c FROM Zuora__Subscription__c WHERE CreatedDate =: d1];
          
          For(Zuora__Subscription__c z : SubsCreated){
              If(z.Zuora__Status__c == 'Cancelled'){
                  AggregateResult[] groupedResultsDown = [SELECT Zuora__Account__c , SUM(Zuora__MRR__C) FROM Zuora__Subscription__c WHERE ID IN: SubsCreated GROUP BY Zuora__Account__c];
                 //Create Downgrade Opp 
                 
                  List<Opportunity> downgradeOpps = new List<Opportunity>();
                 
                  For(AggregateResult ard : groupedResultsDown){
                   
                      Opportunity od = new Opportunity();
                      	od.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('AMP Downgrade').getRecordTypeId();
                      	od.OwnerId = z.Zuora__Account__r.OwnerId;
                      	od.Name = z.Zuora__Account__r.Name + ' - AMP';
                      	od.AccountId = z.Zuora__Account__r.Id;
                      	//od.Armor_Product_Category__c
                      	//od.Armor_Product__c	
                      	//od.Armor_Anywhere_Location__c
                      	od.Opportunity_Source__c = 'Portal';
                      	od.Type = 'Downgrade';
                      	od.Amount = z.Zuora__MRR__c * -1;
                      	od.CurrencyIsoCode = z.CurrencyIsoCode;
                      	od.CloseDate = d1;
                      	od.StageName = 'Cancelled';
                      
                      downgradeOpps.add(od);
                      //End of Creating Downgrade Opp
                  } // End of GroupedResultDown For Loop
                  
                  insert downgradeOpps;
                  
              }
              Else If(z.Zuora__Status__c == 'Active'){
                  AggregateResult[] groupedResultsUp = [SELECT Zuora__Account__c , SUM(Zuora__MRR__C) FROM Zuora__Subscription__c WHERE ID IN: SubsCreated GROUP BY Zuora__Account__c];
                 //Create Upgrade Opp 
                 
                 List<Opportunity> upgradeOpps = new List<Opportunity>();
                  For(AggregateResult aru : groupedResultsUp){
                    
                        
                      Opportunity ou = new Opportunity();
                     	ou.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('AMP Upgrade').getRecordTypeId();
                      	ou.OwnerId = z.Zuora__Account__r.OwnerId;
                      	ou.Name = z.Zuora__Account__r.Name + ' - AMP';
                      	ou.AccountId = z.Zuora__Account__r.Id;
                      	//ou.Armor_Product_Category__c
                      	//ou.Armor_Product__c	
                      	//ou.Armor_Anywhere_Location__c
                      	ou.Opportunity_Source__c = 'Portal';
                      	ou.Type = 'Existing Business';
                      	ou.Amount = z.Zuora__MRR__c;
                      	ou.CurrencyIsoCode = z.CurrencyIsoCode;
                      	ou.CloseDate = d1;
                      	ou.StageName = 'Closed Won';
                      
                      upgradeOpps.add(ou);
                      //End of Creating Upgrade Opp

                  } // End of GroupedResultUp For Loop
                 
                 insert upgradeOpps;
              }
              
          } // End of SubsCreated For Loop
         
          
      } //End of CreateOpportunities Method
} // End Of Class

 
I have created a VF Page to replicate a form that we use, with an extension to when the command button is pressed on the VF page will attach the form to the record the button to pull up the VF page was caled from. 

I am now trying to test my test class and I am getting the following error message upon testing.  "Methods defined as TestMethod do not support getContent call"

Can anyone help re-write or give suggestions on how to get this test class to test out the controller extension? 

Appreciate any help!

Extension Code:
public class ArmorIncMDNAExtension {

    private final Contract__c contract;
    
    private ApexPages.StandardController standardController;
    
    public ArmorIncMDNAExtension(ApexPages.StandardController standardController) {
        this.Contract = (Contract__c)standardController.getRecord();
    }
    
    public pagereference GeneratePDF(){
pagereference Pg = Page.ArmorIncMDNA;
Blob pdf1 = pg.getcontentAsPdf();
Attachment d = new Attachment();
d.ParentId = contract.Id;
d.Body = Pdf1;
d.Name = contract.Account__r.Name + ' Armor Defense Inc. Mutual NDA.pdf';
d.ContentType = 'application/pdf';
insert d;
return null;
}
    
}

Test class Code:
@isTest
public class ArmorContractExtTest {

    
    
    static testMethod void tm1 () {

		Date myDate = Date.newInstance(2017, 8, 17);
        
        Contract__c c = new Contract__c();
        	c.Account__c = '001S000000rwY8H';
        c.Auto_Renew__c = 'No';
        c.Contract_Term_In_Months__c = 12;
        c.Effective_Date__c = myDate;
        c.Renewal_Period_Term_In_Months__c = 12;
        
        insert c;
         
        ArmorIncMDNAExtension ae = new ArmorIncMDNAExtension(new ApexPages.StandardController(c));
        ae.GeneratePDF();

    }
    
    
}

I want to restrict sales users from accepting new leads under they ownership when there are old leads in a queue

How can this be done? Please, help!
Hello awesome Devs, 

I have the following scheduled class that has been running fine but all of a sudden I am now getting an Apex Time Out error when this runs each morning.  After doing some research it looks like this class although is being scheduled it is still running syncronously and hittong the goveror limits. How can I update the followign code to be both Schedulable and Batchable to try and get arounf the timing issue?

Thanks for any advise or reworkign of my code that you can help out with,


Shawn

Trigger Code:
 
global class AMPCurrentAmountBatching Implements Schedulable {

    global void execute(SchedulableContext sc){
        AMPCurrentAmountBatching();
    }
    @future
    static public void AMPCurrentAmountBatching(){
        
       List<Opportunity> opps = new List<Opportunity>();
       Set<String> mySet = new Set<String>();
       Integer recordCount = [SELECT Count() FROM Zuora__Subscription__c WHERE OpportunityId__c != null AND LastModifiedDate = LAST_N_DAYS:60];
       Integer sizeBlock = recordCount/2000 + (math.mod(recordCount,2000)!=0?1:0);
        
        For(Integer i=0;i<sizeBlock;i++){
        For(AggregateResult objar : [SELECT OpportunityId__c Oid, SUM(Total_Booking_Amount__c) Amt
                                    FROM Zuora__Subscription__c WHERE OpportunityId__c !=null AND LastModifiedDate = LAST_N_DAYS:60
                                    AND OpportunityId__c NOT IN:mySet GROUP BY OpportunityId__c LIMIT 2000])
        {
        
        Decimal d = (Decimal)objar.get('Amt');
        
            Opportunity Opp = new Opportunity();
            Opp.Id = (Id)objar.get('Oid');
            Opp.Current_Value__c = (Decimal)objar.get('Amt');
            opps.add(Opp); 
            mySet.add(Opp.Id);
        }
        }
        
        If(opps.size()>0){
           update opps;
        }
        
        
    }
    
    
}

 
Awesome Devs!

I have the followign trigger that will fire upon a lead creation to auto convert the lead.  That is working like a charm, however the issue I am running into is doing a search for an existing account to append the Contact and Opportunity to that is created via the lead conversion. 

I have some logic built in that looks like it is not working to locate an existing account based on the Lead Company field, but my code is still creating a new account each time a lead is automaticlaly converted.  

Can anyone help with getting my code to properly append the lead to the existing account if one exists, and create a new one if one is not found?

Here is the trigger code:
 
Trigger AutoConverter on Lead (after insert) {
     LeadStatus convertStatus = [
          select MasterLabel
          from LeadStatus
          where IsConverted = true
          limit 1
     ];
     List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
	 MAP<Id,Account> AccountSearch = new MAP<Id,Account>([SELECT ID, Name FROM Account]);
    
     for (Lead lead: Trigger.new) {
          if (!lead.isConverted && lead.Company != null) {
              
              If(AccountSearch.equals(Lead.Company)){
               Account Acct = AccountSearch.get(Lead.Company);
               ID AcctId = Acct.Id;
               Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Company+Lead.CreatedDate;
               
               lc.setLeadId(lead.Id);
               lc.setOpportunityName(oppName);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               lc.setAccountId(AcctId);
               
               leadConverts.add(lc);
              }
              Else{
                  Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Name;
               
               lc.setLeadId(lead.Id);
               lc.setOpportunityName(oppName);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               
               leadConverts.add(lc);
              }
          }
     }

     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}

 
I have one checkbox field on account object and I want to create workflow rule where every time that checkbox values go from True to False then it will update one field which is date data type with on which date that checkbox goes from true to false and I do not want to update that date field when record is created it only fired when checkbox goes from true to false
Hello - We are trying to get the standard pricebook entry price from a product into another custom object "Installed Product". There is a field called "Product Price" on "Installed Product" object that we would like to equal the price of the standard pricebook entry price. I wish we could accomplish this through a formula, but i believe it needs to be an APEX trigger which we have light familiarity on. There is a lookup on Installed Product to the product itself, so I assume we'd use that as a reference to find the price. Any guidance or help on creating this trigger would be extremely appreciated!
Hi all,
scenario : i have custom object  name : 'Travel'  and fileds are firstname_c,lastName_c,age_c, dob_c..
third party will send data to Travel object .
so my requirement is , when record created in Travel object , immediatly Task created under contact object and update fields firstname_c,lastName_c,age_c, dob_c. and status is Completed.
How can i write Trigger code for this?
Hi All,
I am attempting to create a pop up message on the Cases page for my community portal to prompt the users to call our support line if the issue they have submitted has been calculated as a "P1". i have attempted a few times but my coding skills are not where they need to be yet as apex is new for me. the field that i have created that it would need to trigger off of is a custom formula field Called "P1_Ticket__C" it is a boolean field and if it is checked i would like it to generate a pop up message that does not stop the user from saving teh record only to remind them to call our support number.

if someone can help me out wiht the code for this it would be super helpful.
Hello awesome devs! 

I have the following class which I woudl like to batch on in a nightly running batch.  This class is to Aggregate some subscription records and group by a String field which will contain the 18 digit ID number from an Opportunity record that the subscriptions make up.  I am then summing the total amoutn from those subscriptions in the aggregate results method so I am left with an amouny for each grouping of subscriptions grouped by the ID number.  

I then want to use that SUM amount to update a field on the Opportunity that is related to the group of subscriptions.  What is happening with my code below is when I run the class, all subscriptions are being summed for every opportunity.  Meaning any opportunity I go to now has the same SUM value which is the value of all Subscriptions in our database with an Opp ID populated and it seems like it is not grouping. 

What did I do wrong, or what should I try as I am stumped.....

Thank you in advance for any help you can provide,

Shawn
 
global class AMPCurrentAmountBatching Implements Schedulable {

    global void execute(SchedulableContext sc){
        AMPCurrentAmountBatching();
    }
    
    public void AMPCurrentAmountBatching(){
        
       List<Opportunity> opps = new List<Opportunity>();
        
       Id idOpp;
       Decimal amt;
       Set<ID> setIdOpp = new Set<ID>();
       Map<Id,Opportunity> mapOpp = new Map<Id,Opportunity>();
       List<AggregateResult> AR = new List<AggregateResult>();
       
        
        For(AggregateResult objar : [SELECT OpportunityId__c, SUM(Total_Booking_Amount__c)
                                    FROM Zuora__Subscription__c WHERE OpportunityId__c !=null
                                    GROUP BY ROLLUP(OpportunityId__c)])
        {
            AR.add(objar);
            setIdOpp.add((ID)objar.get('OpportunityId__c'));  
        }
        
        If(!setIdOpp.isEmpty()){
           // mapOpp = new Map<Id,Opportunity>([SELECT ID, Current_Value__c FROM Opportunity WHERE Id IN: setIdOpp]);
           opps = [SELECT ID, Current_Value__c FROM Opportunity WHERE Id IN: setIdOpp];
        }
        
        For(AggregateResult objar : AR){
            idOpp = (Id)objar.get('OpportunityId__c');
            amt = (Double)objar.get('expr0');
          
        }
        If(opps.size()>0){
        For(Opportunity o : opps){
            o.Current_Value__c = amt;
        }
        }
        
        update opps;
        
    }
    
    
}

 
So this formula needs to Put down if a customer is an A,B, or C customer ranking depending on if the Annual Support field (which is a currency) is between these ranges, but we have two different kinds of structure depending on product. So the first IF is the split there, our JustWare customers have a ranking, and everything else falls into the other catagory, so IF product = Justware  TRUE first section ranking, False second ranking.  Right now it hates the formula and I was wondering if im missing a golden oppetunity to make this into a simpler CASE statement.

IF(Product__c = 'JustWare',
  IF(Annual_Support__c >=100000, 'A',
    IF(AND(Annual_Support__c<100000, Annual_Support__c >=50000), 'B',
     IF(Annual_Support__c>50000, 'C',
         )
       )
     ),
  IF(Annual_Support__c>=200000, 'A',
     IF(AND( Annual_Support__c<200000, Annual_Support__c >=80000), 'B',
        IF(Annual_Support__c>80000, 'C'
    )
   )
  )
)
trigger Trigger4 on Account (After update) 
{
        System.debug('Total number of records in Trigger' + Trigger.size);
         
         for(account a : trigger.old)
         {
              System.debug('Account that got inserted is......' + a.name);
         }
        for(account b : trigger.new)
        {
                b.description = 'Updated by Trigger';
            System.debug('Account that got inserted is......' + b.name);
           }
             
}

Get Error on on updating any record-
Review all error messages below to correct your data.
Apex trigger Trigger4 caused an unexpected exception, contact your administrator: Trigger4: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.Trigger4: line 11, column 1

And whenever try to create any new record get below error-

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger Trigger9 caused an unexpected exception, contact your administrator: Trigger9: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Trigger4: execution of AfterUpdate caused by: System.FinalException: Record is read-only Trigger.Trigger4: line 11, column 1: []: Trigger.Trigger9: line 13, column 1
Hi all,  I'm a week into learning Salesforce and I'm looking for a way to view previous week totals by opportunity amount classification compared directly to current weeks totals in order to track the difference.  Any suggestions how that can be achieved? Opp Example
Current Dash view

Thanks for your help!
Hi,

A developer created a piece of apex code back in 2015 that is blocking a current deployment. For some reason it has only just started to fail, and without knowing the reason why, I (a non-developer) need to address the errors within the class.

The Apex class keeps updates of a portal user in sync with its corresponding contact. As far as I can see, as part of the Test, the code is creating a new portal user and contact record based on the Current Users details.

Unfortunately due to a flow triggered by contact creation, the class and subsequent trigger are not firing.The flow is looking for the account record of the contact that is being created, to update fields within the contact record. 

Is their a way to capture within the apex class the current user's related contact detail, to pull the account record to satisfy the flow? 
I have question , once we integrate sales navigator and CRM sync from Linkedin will i be able to reply to linkedin messages directly from my sales force crm 
I have a form (record) that needs to get locked except for the sys admin once the field changes to "approved" or etc.. The scenario is they have to pay for the form once it is approved. We have a formaula field which acts like a button to a visualforce page for payment. 
-I wrote a validation rule which unlocks the record once it's status changes to certain value.
  AND( 
$Profile.Name <> 'System Administrator', 
$Profile.Name <> 'Agent Manager', 

NOT(ISCHANGED(Status__c)), 
OR( 
ISPICKVAL(Status__c, 'Approved'), 
ISPICKVAL(Status__c, 'Denied'), 
ISPICKVAL(Status__c, 'AM Approved') 

)

How to exclude one field from it? Like formual field"Payment" from it.
I have a custom field on the user object to populate a field (text) and I am at my character limit. When I use the same formula in a workflow I get this error: Error: Formula result is data type (Text), incompatible with expected data type (true or false). 

​IF(TEXT(UserType)='CSN Only','Chatter Only', 
IF(CONTAINS(Profile.Name,'Agent'),'Agent',
IF(CONTAINS(Profile.Name,'Country M'),'Country Manager',
IF(CONTAINS(Profile.Name,'System Admin'), 'System Admin',
IF(CONTAINS(Profile.Name,'Marketing'),'Marketing',
IF(CONTAINS(Profile.Name,'Product')||CONTAINS(Profile.Name, 'E-S'),'Product / Implementation',
IF(CONTAINS(Profile.Name,'Sales Support'),'Sales Support',
IF(CONTAINS(Profile.Name,'Recon'),'Reconciliation User',
IF(CONTAINS(Profile.Name,'Tech Support')||CONTAINS(Profile.Name, 'On Line Support'), 'Configuration/Online Support',
IF(CONTAINS(Profile.Name, 'Client Exec') || CONTAINS(Profile.Name, 'Client Manager')|| CONTAINS(Profile.Name, 'CRM Manager')|| CONTAINS(Profile.Name, 'Customer Relations')|| Profile.Name =  'EMEA DFX', 'CRM & Dealing',
IF(CONTAINS(Profile.Name, 'Accredit') || CONTAINS(Profile.Name, 'Credit')|| CONTAINS(Profile.Name, 'Refresh') || CONTAINS(Profile.Name, 'Enrolement'),'Accreditation/Credit/Enrollment',
IF(CONTAINS(Profile.Name, 'Customer Service') || CONTAINS(Profile.Name, 'Customer Support')||CONTAINS(Profile.Name, 'Mid Office'),'Client Support & Customer Service',
IF(Profile.Name ='GBP Global Banking Operations','Banking Implementation',
IF(CONTAINS(Profile.Name, 'FBA') || CONTAINS(Profile.Name, 'Sales Operations'),'Sales Operations & Finance',
IF(CONTAINS(Profile.Name, 'Investig') || CONTAINS(Profile.Name, 'Operations')|| CONTAINS(Profile.Name, 'Cash Letters')|| CONTAINS(Profile.Name, 'GBP NA Collections')|| Profile.Name =  'GBP NA ePay Support Rep'|| CONTAINS(Profile.Name, 'Ops')|| CONTAINS(Profile.Name, 'WU Retail Payments'), 'Operations & Investigations',
IF(CONTAINS(Profile.Name, 'Transworks') || CONTAINS(Profile.Name, 'External'),'Lead Generation External',
IF(CONTAINS(Profile.Name, 'Lead Gen'),'Lead Generation',
IF(CONTAINS(Profile.Name, 'L&D'), 'L&D',
IF(CONTAINS(Profile.Name, 'Sales Rep') || CONTAINS(Profile.Name, 'Sales Manager'),'Sales',
IF(CONTAINS(Profile.Name, 'EUR AR') || CONTAINS(Profile.Name, 'NA AR'),'AR&Collections',
IF(CONTAINS(Profile.Name, 'Compliance') ||  CONTAINS(Profile.Name, 'Derivatives'),'Compliance',
IF(Profile.Name ='GBP Global EDGE Migration User','Global SBA/VMS',
IF(CONTAINS(Profile.Name, 'API Enabled'),'EDGE HCL Support', 
IF(CONTAINS(Profile.Name, 'NA Audit') || CONTAINS(Profile.Name, 'Read Only')|| CONTAINS(Profile.Name, 'WUBS User'),'Read Only',
'Not Defined'
))))))))))))))))))))))))