• Saval Pathak
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
I am an Admin with no real Apex experience and need help to determine what exactly is happening in this Apex Trigger. I belive it is a trigger to share an Account record when some custom Shared With fields are populated. Trying to determine if we can do away with this trigger and utilize something simpler like Account Teams. Thanks!
 
public class ShareWithTriggerHandler {
    public static void afterInsert(Account[] accounts){
    List<AccountShare> sharesToadd = new List<AccountShare>();
    List<AccountShare> sharesToadd2 = new List<AccountShare>();
    List<AccountShare> sharesToadd3 = new List<AccountShare>();
    List<AccountShare> sharesToadd4 = new List<AccountShare>();
  AccountToContactSync acs = new AccountToContactSync();
  acs.UpdateSharedWith(accounts);
        for(Account acclist: [SELECT Id,Name,Shared_With2__c,Shared_With3__c,Shared_With4__c,Shared_With__c FROM Account WHERE Id IN : accounts])
            {
                if(acclist.Shared_With2__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With2__c);
                AccountShare accShare2  = new AccountShare();
                accShare2.AccountId = acclist.id;
                accShare2.UserOrGroupId = acclist.Shared_With2__c;
                accShare2.AccountAccessLevel = 'Edit';
                accShare2.OpportunityAccessLevel = 'Read';
                accShare2.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd2.add(accShare2);
                // Database.insert(accShr,false);
                }
                if(acclist.Shared_With3__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With3__c);
                AccountShare accShare3  = new AccountShare();
                accShare3.AccountId = acclist.id;
                accShare3.UserOrGroupId = acclist.Shared_With3__c;
                accShare3.AccountAccessLevel = 'Edit';
                accShare3.OpportunityAccessLevel = 'Read';
                accShare3.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd3.add(accShare3);
                }
                if(acclist.Shared_With4__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With4__c);
                AccountShare accShare4  = new AccountShare();
                accShare4.AccountId = acclist.id;
                accShare4.UserOrGroupId = acclist.Shared_With4__c;
                accShare4.AccountAccessLevel = 'Edit';
                accShare4.OpportunityAccessLevel = 'Read';
                accShare4.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd4.add(accShare4);
                }
                if(acclist.Shared_With__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With__c);
                AccountShare accShare  = new AccountShare();
                accShare.AccountId = acclist.id;
                accShare.UserOrGroupId = acclist.Shared_With__c;
                accShare.AccountAccessLevel = 'Edit';
                accShare.OpportunityAccessLevel = 'Read';
                accShare.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd.add(accShare);
                }
                system.debug('!!!!'+acclist);
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd, false);
            }
            if(!sharesToadd2.isEmpty())
            {
            Database.insert(sharesToadd2, false);
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd3, false);
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd4, false);
            }

        }
    public static void afterUpdate(Account[] accounts,Map<Id,Account> oldMap)
        {
        List<AccountShare> sharesToDelete = new List<AccountShare>();
    System.debug('******BEGIN UpdateSharedWith******');
    AccountToContactSync acs = new AccountToContactSync();
    acs.UpdateSharedWith(accounts);
    System.debug('******END UpdateSharedWith******');
        for(AccountShare acclist1: [SELECT Id FROM AccountShare WHERE AccountId IN :trigger.oldMap.keyset() AND RowCause = 'Manual'])
            {
            sharesToDelete.add(acclist1);
            // List<AccountShare> sharesToDelete = [SELECT Id FROM AccountShare WHERE AccountId IN :trigger.oldMap.keyset() AND RowCause = 'Manual'];
            }
        if(!sharesToDelete.isEmpty())
            {
            Database.Delete(sharesToDelete, false);
            }
        List<AccountShare> sharesToadd = new List<AccountShare>();
        List<AccountShare> sharesToadd2 = new List<AccountShare>();
        List<AccountShare> sharesToadd3 = new List<AccountShare>();
        List<AccountShare> sharesToadd4 = new List<AccountShare>();
        for(Account acclist: [SELECT Id,Name,Shared_With2__c,Shared_With3__c,Shared_With4__c,Shared_With__c FROM Account WHERE Id IN : accounts])
            {
            if(acclist.Shared_With2__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With2__c);
                AccountShare accShare2  = new AccountShare();
                accShare2.AccountId = acclist.id;
                accShare2.UserOrGroupId = acclist.Shared_With2__c;
                accShare2.AccountAccessLevel = 'Edit';
                accShare2.OpportunityAccessLevel = 'Read';
                accShare2.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd2.add(accShare2);
                // Database.insert(accShr,false);
                }
                if(acclist.Shared_With3__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With3__c);
                AccountShare accShare3  = new AccountShare();
                accShare3.AccountId = acclist.id;
                accShare3.UserOrGroupId = acclist.Shared_With3__c;
                accShare3.AccountAccessLevel = 'Edit';
                accShare3.OpportunityAccessLevel = 'Read';
                accShare3.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd3.add(accShare3);
                }
                if(acclist.Shared_With4__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With4__c);
                AccountShare accShare4  = new AccountShare();
                accShare4.AccountId = acclist.id;
                accShare4.UserOrGroupId = acclist.Shared_With4__c;
                accShare4.AccountAccessLevel = 'Edit';
                accShare4.OpportunityAccessLevel = 'Read';
                accShare4.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd4.add(accShare4);
                }
                if(acclist.Shared_With__c != null)
                {
                // manualShareEdit(acclist.id,acclist.Shared_With__c);
                AccountShare accShare  = new AccountShare();
                accShare.AccountId = acclist.id;
                accShare.UserOrGroupId = acclist.Shared_With__c;
                accShare.AccountAccessLevel = 'Edit';
                accShare.OpportunityAccessLevel = 'Read';
                accShare.RowCause = Schema.AccountShare.RowCause.Manual;
                sharesToadd.add(accShare);
                }
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd, false);
            }
            if(!sharesToadd2.isEmpty())
            {
            Database.insert(sharesToadd2, false);
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd3, false);
            }
            if(!sharesToadd.isEmpty())
            {
            Database.insert(sharesToadd4, false);
            }

        }
    
  /*  public static void manualShareEdit(Id recordId, Id userOrGroupId)
        {
        AccountShare accShr  = new AccountShare();
        accShr.AccountId = recordId;
        accShr.UserOrGroupId = userOrGroupId;
        accShr.AccountAccessLevel = 'Edit';
        accShr.OpportunityAccessLevel = 'Edit';
        accShr.RowCause = Schema.AccountShare.RowCause.Manual;
        Database.insert(accShr,false);
        }    */
    }

 
Hi all,

I have a problem with this challenge : 

Write an Apex trigger that fires before records are inserted and ensures that the ShippingState field has the same value as the BillingState field.
Create an Apex class named AccountTriggerHandler that contains a public static method called CreateAccounts to accept the List of Account objects.
For each Account record, before saving, ensure that the ShippingState field has the same value as the BillingState field.
Write an Apex trigger named AccountTrigger that fires before records are inserted.
The Apex trigger must call the AccountTriggerHandler.CreateAccounts() method with the collection of new records.
Create a test class named AccountTriggerTest that inserts 200 Account records with a BillingState of CA. After the insert, test to ensure that all 200 records have a ShippingState of CA.
Before verifying this challenge, run your test class at least once using the Developer Console Run All feature.

and this is my classes :
 
public class AccountTriggerHandler {
    
    public static void CreateAccounts(List<Account> acclist){
        
        
        for(Account a:acclist){
            
            if(a.ShippingState!=a.BillingState){
                a.BillingState=a.ShippingState;
              
            }
        }
    }

}
 
trigger AccountTrigger on Account (before insert) 
{
    if (Trigger.isBefore && Trigger.isInsert) {
			AccountTriggerHandler.CreateAccounts(Trigger.new);
		}	
	}
 
@isTest
public class AccountTriggerTest {
    
    @isTest static void TestCreate200Records(){
        
        // Test Setup data
        // Create 200 new Accounts
        List<Account> accts = new List<Account>();
        for(Integer i=0; i < 200; i++) {
            Account acct = new Account(Name='Test Account ' + i, BillingState = 'CA');
            accts.add(acct);
        
            Test.startTest();
            //insert acct;
        	Test.stopTest();
            
            for (Account a:accts){
                
                System.assertEquals('CA', a.ShippingState, 'ERROR');
            }
            
    }

        
}
}
And this error :
Challenge Not yet complete... here's what's wrong: 
The 'AccountTrigger' Trigger does not appear to be working correctly. Inserted records did not have matching BillingState and ShippingState values.

Anyone can help me please??
Thanks!