• Dave Shulman
  • NEWBIE
  • 95 Points
  • Member since 2020

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 10
    Questions
  • 12
    Replies
trigger TriggerNew1Problem on Account(after insert)
{
    Set<String> setacc = new Set<String>();
    for(Account a : Trigger.new)
    {
        setacc.add(a.Name);
    }
    
    List<Account> acclist = [Select Id, Name, (Select Id, Name From Opportunities)
    From Account Where Name In : setacc
    ];
    List<Opportunity> opplist = new List<Opportunity>();
    
    Map<String, Account> nmap = new Map<String, Account>();
    for(Account a : acclist)
    {
        nmap.put(a.Name, a);
    }
    
    for(Account acc : Trigger.new)
    {
        if(nmap.get(acc.Name)!=null)
        {
            opplist.add(new Opportunity(AccountId =acc.Id, Name ='Duplicate Opportunity'+acc.Name, StageName = 'prospecting'
            , CloseDate = System.today()));         
        }
        else if(nmap.get(acc.Name)==null)
        {
             opplist.add(new Opportunity(AccountId =acc.Id, Name ='First Opportunity'+acc.Name, StageName = 'prospecting'
            , CloseDate = System.today()));
        }
    }
    if(opplist.size()>0)
    {
        insert opplist;
    }
}
when i inserted duplicate name in account then print duplicate opportunity name on the bases of account if not a duplicate account name then print first opportunity
But First Opportunity is not printed
Hi All,

Im trying to work with multi layer maps and have never done so before.  

What i want to do is get Category as top KeySet then for each category a Contract Name, then for each contract name Value of Sales.  

Im iterating through sales for each Account and trying to add them to the maps or purge and replace to add $$ to the Value of Sales.

it doesnt seem to be working and i cant figure why.  Please advise thanks!
Map<String,Map<String,Decimal>> CategoryList = new Map<String,Map<String,Decimal>>();
    Map<String,Decimal> SubGroup = new Map<String,Decimal>();
    Map<String,Map<String,Decimal>> TierList = new Map<String,Map<String,Decimal>>();
    Map<String,Decimal> TierSubGroup = new Map<String,Decimal>();
    

if(!CategoryList.keyset().contains(RelatedSale.Product_Group__c)){
                                SubGroup.put(RelatedSale.Related_Contract__r.External_Contract_Name__c, RelatedSale.Amount__c);
                                CategoryList.put(RelatedSale.Product_Group__c, SubGroup);
                                SubGroup.clear();
                            }
                            if(!TierList.keySet().contains(RelatedSale.Product_Group__c)) {
                            	TierSubGroup.put(RelatedSale.Related_Contract__r.Contract_Tier__c, RelatedSale.Amount__c);
                                TierList.put(RelatedSale.Product_Group__c, TierSubGroup);
                                TierSubGroup.clear();
                            }
                            if(CategoryList.keyset().contains(RelatedSale.Product_Group__c)) {
                                if(CategoryList.get(RelatedSale.Product_Group__c).keyset().contains(RelatedSale.Related_Contract__r.External_Contract_Name__c)) {
                                	UpdateAmount = CategoryList.get(RelatedSale.Product_Group__c).get(RelatedSale.Related_Contract__r.External_Contract_Name__c) + RelatedSale.Amount__c;
                                    CategoryList.get(RelatedSale.Product_Group__c).remove(RelatedSale.Related_Contract__r.External_Contract_Name__c);
                                    CategoryList.get(RelatedSale.Product_Group__c).put(RelatedSale.Related_Contract__r.External_Contract_Name__c, UpdateAmount);
                                }
                                if(!CategoryList.get(RelatedSale.Product_Group__c).keyset().contains(RelatedSale.Related_Contract__r.External_Contract_Name__c)) {
                                    CategoryList.get(RelatedSale.Product_Group__c).put(RelatedSale.Related_Contract__r.External_Contract_Name__c, RelatedSale.Amount__c);
                                }
                            }
                            if(TierList.keyset().contains(RelatedSale.Product_Group__c)) {
                            	if(TierList.get(RelatedSale.Product_Group__c).keyset().contains(RelatedSale.Related_Contract__r.Contract_Tier__c)) {
                                	UpdateAmount = TierList.get(RelatedSale.Product_Group__c).get(RelatedSale.Related_Contract__r.Contract_Tier__c) + RelatedSale.Amount__c;
                                    TierList.get(RelatedSale.Product_Group__c).remove(RelatedSale.Related_Contract__r.Contract_Tier__c);
                                    TierList.get(RelatedSale.Product_Group__c).put(RelatedSale.Related_Contract__r.Contract_Tier__c, UpdateAmount);
                                }
                                if(!TierList.get(RelatedSale.Product_Group__c).keyset().contains(RelatedSale.Related_Contract__r.Contract_Tier__c)) {
                                    TierList.get(RelatedSale.Product_Group__c).put(RelatedSale.Related_Contract__r.Contract_Tier__c, RelatedSale.Amount__c);
                                }	   
                            }

 
Hi all im trying to deploy a couple batch apex classes but they are not working on the change set.  The code works, the apex tests vailidate in the sandbox, the Code Coverage estimate is giving me about 85% for each, but when i deploy and Run Specified Test (the 2 test classes) i get 0% coverage.  why might this happen.  
@isTest
public class DeleteHalfYearSalesTest {
    public static testMethod void testSetup() {
        Test.startTest();
        List<Account> AccountList = new List<Account> (); 
        List<Sales_Data__c> SDList = new List<Sales_Data__c> (); 
        Account	Distributor = new Account(Name = 'Account Dist Test' ,
                                        RecordTypeID = '01280000000HeW4AAK');
        	insert Distributor;
        for (Integer a=0; a<5; a++) {
            Account acc = new Account(Name = 'Account ' + a,
                                      PDIid__c = 'PDI234' + a);
            AccountList.add(acc);
        }
        insert AccountList;
        for (Account ac : AccountList) {
            for (Integer s=0; s<5; s++) {
                Sales_Data__c sd = new Sales_Data__c (End_user__c = ac.Id,
                                                      Distributor__c = Distributor.ID,
                                                      Amount__c = 100,
                                                      Date__c = Date.today().addDays(-20),
                                                      Product__c = '01tC0000003HEvUIAW');
                SDList.add(sd);
            }
        }
        insert SDList;
        ID Var = SDList[0].ID;
                
        DeleteHalfYearSales d = new DeleteHalfYearSales();
        Database.executeBatch(d);

        Test.stopTest();
        
        Integer Actual = ([SELECT COUNT() FROM Sales_Data__c WHERE ID = :Var]);
        system.assertEquals(0, Actual);
        System.debug('Amount ' + Actual);
        
    }    
}

test class 2
 
@isTest
public class SalesDataBatchUpdateV3Test {
    public static testMethod void testSetup() {
        Test.startTest();
        List<Account> AccountList = new List<Account> (); 
        List<Sales_Data__c> SDList = new List<Sales_Data__c> (); 
        Account	Distributor = new Account(Name = 'Account Dist Test',
                                        RecordTypeID = '01280000000HeW4AAK');
        	insert Distributor;
        Account ParentAcc = new Account(Name = 'Account ParentAcc Test',
                                        RecordTypeID = '01280000000FzKfAAK',
                                        CMPNYid__c = 'CMPNY1234'); 
			insert ParentAcc;
        for (Integer a=0; a<5; a++) {
            Account acc = new Account(Name = 'Account ' + a,
                                      ParentID = ParentAcc.Id,
                                      CMPNYid__c = 'CMPNY234' + a);
            AccountList.add(acc);
        }
        insert AccountList;
        for (Account ac : AccountList) {
            for (Integer s=0; s<5; s++) {
                Sales_Data__c sd = new Sales_Data__c (End_user__c = ac.Id,
                                                      Distributor__c = Distributor.ID,
                                                      Amount__c = 100,
                                                      Date__c = Date.today().addDays(-20),
                                                      Product__c = '01tC0000003HEvUIAW');
                SDList.add(sd);
            }
        }
        Sales_Data__c Parentsd = new Sales_Data__c (End_user__c = ParentAcc.Id,
                                                    Distributor__c = Distributor.ID,
                                                    Amount__c = 100,
                                                    Date__c = Date.today().addDays(-20),
                                                    Product__c = '01tC0000003HEvUIAW');
        SDList.add(Parentsd);
        insert SDList;
        
        SalesDataBatchUpdateV3 t = new SalesDataBatchUpdateV3();
        Database.executeBatch(t);
        Test.stopTest();
        
        Account UpdatedTestChild = [SELECT ID, Sales_6_months__c, Acct_Primary_Distributor__c FROM Account WHERE ID = :AccountList[0].ID Limit 1];
        Account UpdatedTestParent = [SELECT ID, Rolling_12_months__c FROM Account WHERE ID = :ParentAcc.ID Limit 1];
        System.assertEquals(500, UpdatedTestChild.Sales_6_months__c);
        System.debug('Child Sales_6_months__c: 500' + ' Actual: ' + UpdatedTestChild.Sales_6_months__c);
        System.assertEquals(Distributor.id, UpdatedTestChild.Acct_Primary_Distributor__c);
        System.debug(Distributor.Id + ' : 01280000000HeW4AAK' + ' Actual: ' + UpdatedTestChild.Acct_Primary_Distributor__c);
        System.assertEquals(2600, UpdatedTestParent.Rolling_12_months__c);
        System.debug('Parent Rolling_12_months__c : 2600' + ' Actual: ' + UpdatedTestParent.Rolling_12_months__c);
    }
}


 
Have no idea why im getting this error. From what i found on the community people were saying that i could have a weird quote or something, but I replaced all the single quotes and still getting this error.  Does any one know what this might be.

System.QueryException: line 1:94 no viable alternative at character ' '
 
global class DeleteHalfYearSales implements Database.Batchable <sObject>, Database.Stateful, Schedulable{
    private static final Date eightMonthsAgoBatch = Date.newInstance(Date.today().addMonths(-7).year(), Date.today().addMonths(-7).month(), 1);
    global void execute(SchedulableContext sc){
        DeleteHalfYearSales batch = new DeleteHalfYearSales();
        Database.executeBatch(batch);
    }
    global Database.QueryLocator start(Database.BatchableContext bc){
        String query = 'SELECT ID FROM Sales_Data__c WHERE Sales_Type__c = \'Actual\' AND Date__c >= ' + eightMonthsAgoBatch + ' ORDER BY Date__c DESC';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc, List<Sales_data__c> scope){
        delete scope;    
    }
    global void finish(Database.BatchableContext BC){
		AsyncApexJob a = [SELECT ID, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems FROM AsyncApexJob WHERE ID = :bc.getJobId()];
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        List<String> ToAddress = new List<String>();
        ToAddress.add('david.shulman@pdihc.com');
        mail.setToAddresses(ToAddress);
        mail.setSubject( 'The status of execute batch DeleteHalfYearSales');
        mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems +
            ' batches with ' + a.NumberOfErrors + ' failures.');
        try{
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
        }
        catch(exception e){
            System.debug('>>>error: ' + e.getMessage());
        }
    }
}
Test Class
@isTest
public class DeleteHalfYearSalesTest {
	@isTest
    public static void testSetup() {
        List<Account> AccountList = new List<Account> (); 
        List<Sales_Data__c> SDList = new List<Sales_Data__c> (); 
        Account	Distributor = new Account(Name = 'Account Dist Test',
                                        RecordTypeID = '01280000000HeW4AAK');
        	insert Distributor;
        for (Integer a=0; a<5; a++) {
            Account acc = new Account(Name = 'Account ' + a,
                                      Cmpid__c = 'CMP234' + a);
            AccountList.add(acc);
        }
        insert AccountList;
        for (Account ac : AccountList) {
            for (Integer s=0; s<5; s++) {
                Sales_Data__c sd = new Sales_Data__c (End_user__c = ac.Id,
                                                      Distributor__c = Distributor.ID,
                                                      Amount__c = 100,
                                                      Date__c = Date.today().addDays(-20),
                                                      Product__c = '01tC0000003HEvUIAW');
                SDList.add(sd);
            }
        }
        insert SDList;
        ID Var = SDList[0].ID;
                
        Test.startTest();
        DeleteHalfYearSales d = new DeleteHalfYearSales();
        Database.executeBatch(d);
        Test.stopTest();
        
        Sales_Data__c Sales = [SELECT ID FROM Sales_Data__c WHERE ID = :Var Limit 1];
        System.assertEquals(null, Sales.Id);
        System.debug('Expected null Actual ' + Sales.Id);
    }    
}


 
Looking back at some of the existing code in my org someone used something like this to get child records onto a list
for(Account acc : accounts){
            List<Sales_data__c> salesDatas = acc.Sales_data1__r;
I had no idea you were able to do it like this without a query, but now when i try to repurpose a version of this somewhere else, it isnt pulling working when everything is virtually the same? Please advise thanks!
Hi All,

Im trying to carry sales through account heirarchy adding throughout each level.  Sales Data is the individual sales line items object, and then it goes up to EndUser Accounts which Is parented to other accounts and so on.  I want the Sales Line items to roll up to the EndUser, the EndUsers to Roll Up to the Parents (who might also have sales that i want rolled in) and so on.  

Currently i have something close.  Cant figure out why its not working though.  please advise why this wont do what i am trying to do.
 
public class SalesDataRollingUp {
    private static final Date sixMonthsAgo = Date.today().addMonths(-6);
    private static final Date twelveMonthsAgo = Date.today().addMonths(-12);
    private static final Date startCurrentYear = Date.newInstance(Date.today().year(), 1, 1);
    private static Map<ID, Account> LevelCarryThrough(List<ID> AccountIDs) {
        Map<ID, Account> RelevantAccounts = new Map <ID, Account> ([SELECT ID, Rolling_6_Months__c, Rolling_12_months__c
                                     	    					  FROM Account
                                     	    					  WHERE ID in :AccountIDs]);
        for(Account Parent : RelevantAccounts.values()){
            Parent.Rolling_6_Months__c = 0;
            Parent.Rolling_12_months__c = 0;
            List<Account> ChildAccounts = Parent.ChildAccounts;
            if(ChildAccounts != null && !ChildAccounts.isEmpty()){
                for(Account ChildAcc : ChildAccounts){
                    if(ChildAcc.Rolling_6_Months__c > 0){
                        Parent.Rolling_6_Months__c += ChildAcc.Rolling_6_Months__c;
                    }
                    if(ChildAcc.Rolling_12_months__c > 0){
                        Parent.Rolling_12_months__c += ChildAcc.Rolling_12_months__c;
                    }
                } 
            }
            List<Sales_Data__c> RelatedSales = Parent.Sales_Data1__r;
            if(RelatedSales != null && !RelatedSales.isEmpty()){
                for(Sales_Data__c RelatedSale : RelatedSales){
                    if(RelatedSale.Date__c > sixMonthsAgo){
                        Parent.Rolling_6_Months__c += RelatedSale.Amount__c;
                    }
                    if(RelatedSale.Date__c > twelveMonthsAgo){
                        Parent.Rolling_12_months__c += RelatedSale.Amount__c;
                    }
                }
            }           
        }
        Database.update(RelevantAccounts.values());
        Map<ID, Account> ParentAccountsAll = new Map<ID, Account>([SELECT ParentID
                                    							  FROM Account
                                                                  WHERE ID in :AccountIDs]);
        Map<ID, Account> ParentAccounts = new Map<ID, Account>([SELECT ID
                                    							FROM Account
                                                                WHERE ID in :ParentAccountsAll.keySet()]);
        Return ParentAccounts;
    }
    public void UpdateParentRollings(List<ID> ChildAccountIDs) {
        List<ID> Level1 = new List<ID> (SalesDataRollingUp.LevelCarryThrough(ChildAccountIDs).keyset());
        List<ID> Level2 = new List<ID> (SalesDataRollingUp.LevelCarryThrough(Level1).keyset());
        List<ID> Level3 = new List<ID> (SalesDataRollingUp.LevelCarryThrough(Level2).keyset());
        List<ID> Level4 = new List<ID> (SalesDataRollingUp.LevelCarryThrough(Level3).keyset());
        List<ID> Level5 = new List<ID> (SalesDataRollingUp.LevelCarryThrough(Level4).keyset());
    }
}

 
Im trying to loop through an AggregatedResult.   
AggregateResult[] groupedResults = [SELECT End_user__c,Distributor__c,SUM(Amount__c) 
                                               FROM Sales_data__c 
                                               WHERE Date__c >= :AfterDate 
                                                    AND Sales_Type__c = 'Actual' 
                                         			AND End_user__r.ID IN :accountIds
                                               GROUP BY End_user__c, Distributor__c
                                               ORDER BY End_user__c, SUM(Amount__c)];
how do i loop through this to get the first Distributor ID for each End User.  We're trying to capture the primary distributor for each End User based on Amount.  Please let me know how to loop through this and get the information  
 
Hi All,

Im writing a test class for batch apex that will be scheduled to run on the first of each month.  Were testing it here, but i keep running into this error at line 25 "System.QueryException: List has no rows for assignment to SObject".  I dont know why that list would be blank.  Can anyone advise? thanks/
 
public class TouchSalesDatasTest {
	@isTest
    public static void Test() {
        Map<ID,Sales_Data__c> SDMap = new Map<ID,Sales_Data__c>();
        Account Acc = TestDataFactory.createAccount('TouchSalesDataTest EU ', 'desiredrecordtypeid', null, null, true);
        Account Dist = TestDataFactory.createAccount('TouchSalesDataTest Dist ', 'desiredrecordtypeid', null, null, true);
        for (Integer i = 0; i < 200; i++) {
            Sales_Data__c SD = new Sales_Data__c(End_User__c = Acc.ID,
                                                 Distributor__c = Dist.Id,
                                                 Amount__c = i,
                                                 Quantity__c = i,
                                                 Date__c = System.today().addDays(-25),
                                                 Product__c = 'desiredproductid');
            SDMap.put(SD.Id,SD);
        }
        insert SDMap.values();
        
        Decimal expectedValue = System.today().addDays(-25).daysBetween(System.today())/31;
        
        Test.startTest();
        TouchSalesDatas t = new TouchSalesDatas();
        Database.executeBatch(t);
        Test.stopTest();
        
        Sales_Data__c UpdatedTest = [SELECT ID,Name,Months_Ago__c FROM Sales_Data__c WHERE ID IN :SDMap.keySet() Limit 1];
        System.assertEquals(Math.round(expectedValue), UpdatedTest.Months_Ago__c);
    }
}

 
Hi All,

when creating HTML templates I try to bring in pictures in my FIles.   The pictures are large, but in the HTML source of the template i adjust the size atribute to where i want it to be, and the template displays properly in the email preview.  Then when i send the template, it is recieved in the source file sizing (the images are giant).  How do I get the images to be recieved the same as they are displayed in the template.

<a href="https://www.facebook.com/"><img alt="FaceBook logo" src="https://test.555.content.force.com/file-asset-public/FaceBook_logo?oid=00B100000000001" style="max-width: 30px" title="FaceBook logo" /></a>

 
The goal here is to total the Sales Data related to each account for each distributor.  Then see which is the #1 distributor for each account then update the account to reflect that.  My code is throwing no errors, but it isnt updating the field.  Wheres the issue here? I cant find it at all.

Thought it was the SOQL query, but went throug hthat many times and thats definitely pulling the data i want.

Take a look.  Also im pretty new to this so stylistic recommendations are helpful too.
trigger DistributorIDs on Sales_data__c (after insert, after update) {
       Double Amount = 0;
       Double UpdateMap = 0;
       ID PrimaryDistributor;
       Date AfterDate = Date.today().addDays(-45);
       Set<Id>accountIds = new Set<Id>(); //account in trigger.new
       Map<ID, Double> DistributorsList = new Map<ID, Integer>(); //distributors in trigger.new
       for (Sales_data__c Sd: Trigger.new) {
           if(Sd.End_user__c!=null && Sd.Distributor__r!=null){
                accountIds.add(Sd.End_user__c); // adding accounts to set
                DistributorsList.put(Sd.Distributor__c, 0);  // adding distributors to map
           }
       } 
       Map<ID, Double> DistributorsCopy = DistributorsList.clone(); //clone 0'd map

       List<Account> accountToUpdate = new List<Account>(); //list for DML
       List<Account> accList = new List<Account>([SELECT Id,(SELECT Distributor__c,Amount__c,Date__c FROM 
                                                 Sales_data1__r WHERE Date__c >= :AfterDate) // used sub query on Sales Data
                                                 FROM Account WHERE Id IN :accountIds]);
       for(Account acc : accList){ //looping through every account of above list
            for(Sales_Data__c sd : acc.Sales_Data__r){  // looping thorugh every child of the account
                if(sd.Amount__c !=null && sd.Distributor__c!=''){
                    UpdateMap = DistributorsList.get(sd.Distributor__c) + sd.Amount__c; //populate var w updated amt for ea dist
                    DistributorsList.remove(sd.Distributor__c);  //remove old value from map
                    DistributorsList.put(sd.Distributor__c, UpdateMap); //push new one value to map
                 }  
            }
            for (Id key: DistributorsList.keySet()) { //iterate through distributors map
                if(DistributorsList.get(key) > Amount){ //if greater than 0 to start, then greater than max
                    PrimaryDistributor = key;  //set primarydist = key if its greatest so far
                    Amount = DistributorsList.get(key); //set amount equal to value
                }
            }           
            acc.Acct_Primary_Distributor__c = PrimaryDistributor;   // adding largest distributors ID to parent account
            accountToUpdate.add(acc);
            DistributorsList.clear(); //returning map values to 0 before next iteration
            DistributorsList.putAll(DistributorsCopy);
          } 
      if(accountToUpdate.size() > 0){
        update accountToUpdate;
      }
}

 
Hi All.  Long Time admin but Very new to apex and developing.  I have a child object for accounts "sales data" which essentially is just transactional line items of purchases (related to the distributor, and the account).  So now I want to have a trigger on the sales data object that takes the sum of the amt in last 45 days by account then by distributor.  So that i can update the primary & secondary relationship on the account to indicate the distributor that is doing the most and second most sales for that account respectively.  below is what i have so far.  Please take a look and help out with getting thsi to run, and also how do i bulkify this??  Thanks guys!
 
trigger DistributorIDs on Sales_data__c (after insert, after update) {
       Date AfterDate = Date.today().addDays(-45);
       List <Account> AccountsList = new List<Account>();
    For (Sales_data__c Sd: Trigger.new) {
        AccountsList.add(Sd.End_user__r);
    } 
       Set<Account> DeDupSet = new Set<Account>();
       List<Account> DeDupAccounts = new List<Account>();
       DeDupSet.addAll(AccountsList);
       DeDupAccounts.addAll(DeDupSet);
    For (Account Acc: DeDupAccounts) {
        List<AggregateResult> Distributors = [SELECT Distributor__c, SUM(Amount__c) FROM Sales_Data__c 
                                    WHERE Date__c >= :AfterDate AND End_user__c = :Acc.Id
                                    GROUP BY Distributor__c ORDER BY SUM(Amount__c) LIMIT 2];
        ID FirstPosition = (ID)Distributors[0].get('expr0');
        ID SecondPosition = (ID)Distributors[1].get('expr1');
        Acc.Acct_Primary_Distributor__c = FirstPosition;
        Acc.Acct_Secondary_Distributor__c = SecondPosition;
    }
    update DeDupAccounts;
}

 
Hi All,

Im trying to work with multi layer maps and have never done so before.  

What i want to do is get Category as top KeySet then for each category a Contract Name, then for each contract name Value of Sales.  

Im iterating through sales for each Account and trying to add them to the maps or purge and replace to add $$ to the Value of Sales.

it doesnt seem to be working and i cant figure why.  Please advise thanks!
Map<String,Map<String,Decimal>> CategoryList = new Map<String,Map<String,Decimal>>();
    Map<String,Decimal> SubGroup = new Map<String,Decimal>();
    Map<String,Map<String,Decimal>> TierList = new Map<String,Map<String,Decimal>>();
    Map<String,Decimal> TierSubGroup = new Map<String,Decimal>();
    

if(!CategoryList.keyset().contains(RelatedSale.Product_Group__c)){
                                SubGroup.put(RelatedSale.Related_Contract__r.External_Contract_Name__c, RelatedSale.Amount__c);
                                CategoryList.put(RelatedSale.Product_Group__c, SubGroup);
                                SubGroup.clear();
                            }
                            if(!TierList.keySet().contains(RelatedSale.Product_Group__c)) {
                            	TierSubGroup.put(RelatedSale.Related_Contract__r.Contract_Tier__c, RelatedSale.Amount__c);
                                TierList.put(RelatedSale.Product_Group__c, TierSubGroup);
                                TierSubGroup.clear();
                            }
                            if(CategoryList.keyset().contains(RelatedSale.Product_Group__c)) {
                                if(CategoryList.get(RelatedSale.Product_Group__c).keyset().contains(RelatedSale.Related_Contract__r.External_Contract_Name__c)) {
                                	UpdateAmount = CategoryList.get(RelatedSale.Product_Group__c).get(RelatedSale.Related_Contract__r.External_Contract_Name__c) + RelatedSale.Amount__c;
                                    CategoryList.get(RelatedSale.Product_Group__c).remove(RelatedSale.Related_Contract__r.External_Contract_Name__c);
                                    CategoryList.get(RelatedSale.Product_Group__c).put(RelatedSale.Related_Contract__r.External_Contract_Name__c, UpdateAmount);
                                }
                                if(!CategoryList.get(RelatedSale.Product_Group__c).keyset().contains(RelatedSale.Related_Contract__r.External_Contract_Name__c)) {
                                    CategoryList.get(RelatedSale.Product_Group__c).put(RelatedSale.Related_Contract__r.External_Contract_Name__c, RelatedSale.Amount__c);
                                }
                            }
                            if(TierList.keyset().contains(RelatedSale.Product_Group__c)) {
                            	if(TierList.get(RelatedSale.Product_Group__c).keyset().contains(RelatedSale.Related_Contract__r.Contract_Tier__c)) {
                                	UpdateAmount = TierList.get(RelatedSale.Product_Group__c).get(RelatedSale.Related_Contract__r.Contract_Tier__c) + RelatedSale.Amount__c;
                                    TierList.get(RelatedSale.Product_Group__c).remove(RelatedSale.Related_Contract__r.Contract_Tier__c);
                                    TierList.get(RelatedSale.Product_Group__c).put(RelatedSale.Related_Contract__r.Contract_Tier__c, UpdateAmount);
                                }
                                if(!TierList.get(RelatedSale.Product_Group__c).keyset().contains(RelatedSale.Related_Contract__r.Contract_Tier__c)) {
                                    TierList.get(RelatedSale.Product_Group__c).put(RelatedSale.Related_Contract__r.Contract_Tier__c, RelatedSale.Amount__c);
                                }	   
                            }

 
Hi all im trying to deploy a couple batch apex classes but they are not working on the change set.  The code works, the apex tests vailidate in the sandbox, the Code Coverage estimate is giving me about 85% for each, but when i deploy and Run Specified Test (the 2 test classes) i get 0% coverage.  why might this happen.  
@isTest
public class DeleteHalfYearSalesTest {
    public static testMethod void testSetup() {
        Test.startTest();
        List<Account> AccountList = new List<Account> (); 
        List<Sales_Data__c> SDList = new List<Sales_Data__c> (); 
        Account	Distributor = new Account(Name = 'Account Dist Test' ,
                                        RecordTypeID = '01280000000HeW4AAK');
        	insert Distributor;
        for (Integer a=0; a<5; a++) {
            Account acc = new Account(Name = 'Account ' + a,
                                      PDIid__c = 'PDI234' + a);
            AccountList.add(acc);
        }
        insert AccountList;
        for (Account ac : AccountList) {
            for (Integer s=0; s<5; s++) {
                Sales_Data__c sd = new Sales_Data__c (End_user__c = ac.Id,
                                                      Distributor__c = Distributor.ID,
                                                      Amount__c = 100,
                                                      Date__c = Date.today().addDays(-20),
                                                      Product__c = '01tC0000003HEvUIAW');
                SDList.add(sd);
            }
        }
        insert SDList;
        ID Var = SDList[0].ID;
                
        DeleteHalfYearSales d = new DeleteHalfYearSales();
        Database.executeBatch(d);

        Test.stopTest();
        
        Integer Actual = ([SELECT COUNT() FROM Sales_Data__c WHERE ID = :Var]);
        system.assertEquals(0, Actual);
        System.debug('Amount ' + Actual);
        
    }    
}

test class 2
 
@isTest
public class SalesDataBatchUpdateV3Test {
    public static testMethod void testSetup() {
        Test.startTest();
        List<Account> AccountList = new List<Account> (); 
        List<Sales_Data__c> SDList = new List<Sales_Data__c> (); 
        Account	Distributor = new Account(Name = 'Account Dist Test',
                                        RecordTypeID = '01280000000HeW4AAK');
        	insert Distributor;
        Account ParentAcc = new Account(Name = 'Account ParentAcc Test',
                                        RecordTypeID = '01280000000FzKfAAK',
                                        CMPNYid__c = 'CMPNY1234'); 
			insert ParentAcc;
        for (Integer a=0; a<5; a++) {
            Account acc = new Account(Name = 'Account ' + a,
                                      ParentID = ParentAcc.Id,
                                      CMPNYid__c = 'CMPNY234' + a);
            AccountList.add(acc);
        }
        insert AccountList;
        for (Account ac : AccountList) {
            for (Integer s=0; s<5; s++) {
                Sales_Data__c sd = new Sales_Data__c (End_user__c = ac.Id,
                                                      Distributor__c = Distributor.ID,
                                                      Amount__c = 100,
                                                      Date__c = Date.today().addDays(-20),
                                                      Product__c = '01tC0000003HEvUIAW');
                SDList.add(sd);
            }
        }
        Sales_Data__c Parentsd = new Sales_Data__c (End_user__c = ParentAcc.Id,
                                                    Distributor__c = Distributor.ID,
                                                    Amount__c = 100,
                                                    Date__c = Date.today().addDays(-20),
                                                    Product__c = '01tC0000003HEvUIAW');
        SDList.add(Parentsd);
        insert SDList;
        
        SalesDataBatchUpdateV3 t = new SalesDataBatchUpdateV3();
        Database.executeBatch(t);
        Test.stopTest();
        
        Account UpdatedTestChild = [SELECT ID, Sales_6_months__c, Acct_Primary_Distributor__c FROM Account WHERE ID = :AccountList[0].ID Limit 1];
        Account UpdatedTestParent = [SELECT ID, Rolling_12_months__c FROM Account WHERE ID = :ParentAcc.ID Limit 1];
        System.assertEquals(500, UpdatedTestChild.Sales_6_months__c);
        System.debug('Child Sales_6_months__c: 500' + ' Actual: ' + UpdatedTestChild.Sales_6_months__c);
        System.assertEquals(Distributor.id, UpdatedTestChild.Acct_Primary_Distributor__c);
        System.debug(Distributor.Id + ' : 01280000000HeW4AAK' + ' Actual: ' + UpdatedTestChild.Acct_Primary_Distributor__c);
        System.assertEquals(2600, UpdatedTestParent.Rolling_12_months__c);
        System.debug('Parent Rolling_12_months__c : 2600' + ' Actual: ' + UpdatedTestParent.Rolling_12_months__c);
    }
}


 
Hi All,

Im writing a test class for batch apex that will be scheduled to run on the first of each month.  Were testing it here, but i keep running into this error at line 25 "System.QueryException: List has no rows for assignment to SObject".  I dont know why that list would be blank.  Can anyone advise? thanks/
 
public class TouchSalesDatasTest {
	@isTest
    public static void Test() {
        Map<ID,Sales_Data__c> SDMap = new Map<ID,Sales_Data__c>();
        Account Acc = TestDataFactory.createAccount('TouchSalesDataTest EU ', 'desiredrecordtypeid', null, null, true);
        Account Dist = TestDataFactory.createAccount('TouchSalesDataTest Dist ', 'desiredrecordtypeid', null, null, true);
        for (Integer i = 0; i < 200; i++) {
            Sales_Data__c SD = new Sales_Data__c(End_User__c = Acc.ID,
                                                 Distributor__c = Dist.Id,
                                                 Amount__c = i,
                                                 Quantity__c = i,
                                                 Date__c = System.today().addDays(-25),
                                                 Product__c = 'desiredproductid');
            SDMap.put(SD.Id,SD);
        }
        insert SDMap.values();
        
        Decimal expectedValue = System.today().addDays(-25).daysBetween(System.today())/31;
        
        Test.startTest();
        TouchSalesDatas t = new TouchSalesDatas();
        Database.executeBatch(t);
        Test.stopTest();
        
        Sales_Data__c UpdatedTest = [SELECT ID,Name,Months_Ago__c FROM Sales_Data__c WHERE ID IN :SDMap.keySet() Limit 1];
        System.assertEquals(Math.round(expectedValue), UpdatedTest.Months_Ago__c);
    }
}

 
I'm trying to edit my formula field to only make it trigger if the List_A_Document__c picklist field = "List B and C Provided".

Can anyone help?


IF(Expiration_Date_of_List_C_Document__c > Acquisition_On_boarding_Application__r.Start_Date__c, 'No', 'Yes')

TIA

 
Given a Task has a OwnerId that ties to a User, and
User has a UserRoleId field that ties to a UserRole, and
UserRole has a string Name field, I'd like to extract the role of the owner of a given task. This loads without error:
 
public String getRoleOfOwner( Task task) {
    User user = [SELECT Id FROM User WHERE Id = :task.ownerId];
    UserRole roleObj = [SELECT Id FROM UserRole WHERE Id = :user.UserRoleId]; 
    return roleObj.Name;
}

But that seems so old-school and unnecessary. Isn't there some dot-notation like
String roleOfOwner = task.OwnerId.User.UserRole.Name

where I can get the value directly?

Thx,

Chris , SF Developer Day 20
trigger TriggerNew1Problem on Account(after insert)
{
    Set<String> setacc = new Set<String>();
    for(Account a : Trigger.new)
    {
        setacc.add(a.Name);
    }
    
    List<Account> acclist = [Select Id, Name, (Select Id, Name From Opportunities)
    From Account Where Name In : setacc
    ];
    List<Opportunity> opplist = new List<Opportunity>();
    
    Map<String, Account> nmap = new Map<String, Account>();
    for(Account a : acclist)
    {
        nmap.put(a.Name, a);
    }
    
    for(Account acc : Trigger.new)
    {
        if(nmap.get(acc.Name)!=null)
        {
            opplist.add(new Opportunity(AccountId =acc.Id, Name ='Duplicate Opportunity'+acc.Name, StageName = 'prospecting'
            , CloseDate = System.today()));         
        }
        else if(nmap.get(acc.Name)==null)
        {
             opplist.add(new Opportunity(AccountId =acc.Id, Name ='First Opportunity'+acc.Name, StageName = 'prospecting'
            , CloseDate = System.today()));
        }
    }
    if(opplist.size()>0)
    {
        insert opplist;
    }
}
when i inserted duplicate name in account then print duplicate opportunity name on the bases of account if not a duplicate account name then print first opportunity
But First Opportunity is not printed
Hi All.  Long Time admin but Very new to apex and developing.  I have a child object for accounts "sales data" which essentially is just transactional line items of purchases (related to the distributor, and the account).  So now I want to have a trigger on the sales data object that takes the sum of the amt in last 45 days by account then by distributor.  So that i can update the primary & secondary relationship on the account to indicate the distributor that is doing the most and second most sales for that account respectively.  below is what i have so far.  Please take a look and help out with getting thsi to run, and also how do i bulkify this??  Thanks guys!
 
trigger DistributorIDs on Sales_data__c (after insert, after update) {
       Date AfterDate = Date.today().addDays(-45);
       List <Account> AccountsList = new List<Account>();
    For (Sales_data__c Sd: Trigger.new) {
        AccountsList.add(Sd.End_user__r);
    } 
       Set<Account> DeDupSet = new Set<Account>();
       List<Account> DeDupAccounts = new List<Account>();
       DeDupSet.addAll(AccountsList);
       DeDupAccounts.addAll(DeDupSet);
    For (Account Acc: DeDupAccounts) {
        List<AggregateResult> Distributors = [SELECT Distributor__c, SUM(Amount__c) FROM Sales_Data__c 
                                    WHERE Date__c >= :AfterDate AND End_user__c = :Acc.Id
                                    GROUP BY Distributor__c ORDER BY SUM(Amount__c) LIMIT 2];
        ID FirstPosition = (ID)Distributors[0].get('expr0');
        ID SecondPosition = (ID)Distributors[1].get('expr1');
        Acc.Acct_Primary_Distributor__c = FirstPosition;
        Acc.Acct_Secondary_Distributor__c = SecondPosition;
    }
    update DeDupAccounts;
}