function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jadenjaden 

Trigger will not deploy to Production

Hi,

 

I have a trigger which I and the user have tested in the Sandbox. 

 

I also have a test class for it and it passes that.

 

It is failing on deployment and I believe i have tracked it to once statement but cannot see issue:

 

Here is the trigger:

 

trigger TerrZipAdd on Territory_ZIP_Code__c (after insert, after update) {
	
    User mrOpen = [select id,name from User where name=:RequestOwnershipService.MR_OPEN_NAME]; 
    AccountShare[] newShares = new AccountShare[0];   

	set<ID> idsn = Trigger.newmap.keySet();
	list<Territory_Zip_Code__c> tzcRecsn = [SELECT Id, Name, GroupId__c, SharingMode__c FROM Territory_Zip_Code__c WHERE ID in :idsn];
	for (Territory_Zip_Code__c tzcn: tzcRecsn) {
  
    //JJB Blackiron 8-2011 Per Brian he wanted the hard exclusion of the 3 profiles        
    //for(Territory_Zip_Code__c tzcn : Trigger.new) {  
        List<Account> AccountZipRecs =
            [SELECT id,  OwnerId
            FROM Account  
            //WHERE BillingPostalCode = :tzcn.Name];
            WHERE BillingPostalCode = :tzcn.Name 
            and Owner.ProfileId  not in 
			('00e30000000mIYBAA2', '00e700000017F9PAAU', '00e50000000nDWbAAM')];
            
        Group gr;
        if(tzcn.GroupId__c != null) { 
            try {
                system.debug('about to try name match');
                gr = [Select id from Group where Name = :tzcn.GroupID__c];
            }
            catch (Exception ex) {
                   system.debug('about to try id');
 	            try
	            {
	                gr = [Select Id, Name from Group where Id = :tzcn.GroupID__c];
	            }
	            catch (Exception ex1) {
	            }   
            } 
        } 
        system.debug('the group id is ' + gr.id);                 
        if(gr.id != null) {
	       // We have our list of accounts affected; now add shares for the new group ID
			
			for (Account accshare: AccountZipRecs)
			{
		    	// Set the access level
		        string accountAccessLevel = null;
		        string opportunityAccessLevel = null;
		        Boolean fullAccess = false; 
	                
		        if(accshare.ownerId == mrOpen.id)
		        	fullAccess = true;
	                // SharingMode can have 3 values: null, 'Default', 'Telesales'
	                if(tzcn.SharingMode__c == 'Telesales')
	                {
	                    accountAccessLevel = 'Edit';
	                    opportunityAccessLevel = 'None';
	                }
	                else
	                {
	                   accountAccessLevel = fullAccess ? 'Edit' : 'Read';
	                   opportunityAccessLevel = fullAccess ? 'Read' : 'None';
	                }
	                // Create new share
	                AccountShare newShare = new AccountShare();
	                newShare.UserOrGroupId = gr.id;
	                newShare.AccountId = accshare.id;
	                newShare.AccountAccessLevel = accountAccessLevel;
	                newShare.OpportunityAccessLevel = opportunityAccessLevel;
	                // Copy the share
	                newShares.add(newShare);
		                
	        }  //For loop bracket  
	    }    // If gr.id !null 
    }
     
    // Anything to insert?
    if (newShares.size() == 0)
        return;
 
    // Insert safely
    try
    {
        Database.SaveResult[] results = Database.insert(newShares, false);
        
        for (Database.SaveResult sr : results)
        {
            if (sr.isSuccess())
                continue;
            for (Database.Error edb : sr.getErrors())
 	           System.debug('-->     sr: ' + edb.getMessage());
            }
    }
    catch (DmlException edb) {
    }     
}

 

 

 Now in trying to see what was wrong I commented the lower half of the trigger where I go through the for loop and do insert out and it passed validation, so it would seem the top portion is okay. 

 

It seems it does like the for statement:

for (Account accshare: AccountZipRecs)

 

any help greatly appreciated, I would like to be able tp get the deploayed.

Best Answer chosen by Admin (Salesforce Developers) 
jadenjaden

Hi,

 

I found the issue with this trigger:

 

I was checking to see if gr.id !=null but it was not it was whatever value came in from user.  I needed to assign what came back in the query (if something did) to another variable and test that variable. 

 

I replaced

if(gr.id 1= null)  

 

with

if(groupid != null)

 

groupid is filled when I query the group object and get a Succesful read.

 

thanks for the help.

All Answers

Ankit AroraAnkit Arora

What is the error you are facing? And if it regarding code coverage then please paste the test class code also.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

sfdcbynitesfdcbynite
WHERE BillingPostalCode = :tzcn.Name 
            and Owner.ProfileId  not in 
			('00e30000000mIYBAA2', '00e700000017F9PAAU', '00e50000000nDWbAAM')];

 

 

Could this be the problem? Hard-coded Ids will cause a test to fail in deployment if the ids are different in the production org. Is this a full copy sandbox (then this is probably not the problem) or an config/dev sandbox (in which case it might be your problem)?

jadenjaden

Hi,

 

It is not coverage related, it is causing an error further down in other triggers: this is what I am getting:

Run Failures:

  MindstormTest.onAccountAfterUpdateTest System.ListException: Missing id at index: 0

  MindstormTest.onLeadAfterUpdateTest System.ListException: Missing id at index: 0

  MindstormTest.zipShareManagerTest System.ListException: Missing id at index: 0

 

If I comment out everthing between

if(gr.id != null) {

 

 } // If gr.id !null 

 

The process passes. 

 

I then opened it all back up and then just commented out what is between the 

for (Account accshare: AccountZipRecs)

 

} //For loop bracket 

 

This failes; I a baffled. 

Thanks for your help and the the owner.profileid's are in production so those are not an issue.  As mentioned I can get it to pass with commenting out that lower block.

jadenjaden

Thanks for your repsone.

 

Please see my other post, but the profileId's are correct for production,  The error seems to be in the secind bloack of code/.

 

Thanks again.

jadenjaden

Hi,

 

I found the issue with this trigger:

 

I was checking to see if gr.id !=null but it was not it was whatever value came in from user.  I needed to assign what came back in the query (if something did) to another variable and test that variable. 

 

I replaced

if(gr.id 1= null)  

 

with

if(groupid != null)

 

groupid is filled when I query the group object and get a Succesful read.

 

thanks for the help.

This was selected as the best answer