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
davidesidavidesi 

A class has been 'magically' uncompiled


We have a class that yesterday was working ok, but today when we try to add a user to a campaign,
we get this error:
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CampaignUsersAfterInsert caused an unexpected exception, contact your administrator: CampaignUsersAfterInsert: execution of AfterInsert caused by: line 2, column 44: trigger body is invalid and failed recompilation: Dependent class is invalid and needs recompilation: Ap02Campaign: line 18, column 14: Field is not writeable: CampaignShare.CampaignAccessLevel

When you access to this class (Ap02Campaign) , now it is uncompiled, and if you try to save it you get the following error:
Field is not writeable: CampaignShare.CampaignAccessLevel
The fact is that the same class is saved and working in thifferent environments (Cert, Pre, production ...)
Anybody knows why of the error?
KaranrajKaranraj
As per the documentation campaignshare object is no writeable https://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_campaignshare.htm.
Can you share code here? it will be easy for us to assist you
davidesidavidesi
Thanks Karanraj

We have found that the error is due to the sharing settings of CampaingShare .

Following the link you have written I can see that campaignshare.CampaignId is not updatable, but I'm not updating but creating from zero.

The code is the following:
 
for (Campaign_Users__c cu : allUsers){
        	// MJB : Fixed Issue (only add share for non-owner users)
        	if(cu.core_campaign__r.OwnerId != cu.Core_Campaign_users__c) {
	            CampaignShare cs = new CampaignShare();
	            cs.CampaignAccessLevel=CoreHelper.EDIT;
	            cs.CampaignId=cu.Core_Campaign__c;
	            cs.UserOrGroupId=cu.Core_Campaign_users__c;
	            newCS.add(cs);
	            // MJB : Fixed Issue (where removing all campaigns and adding only Primary Issue)
				campaignIDs.add(cu.Core_Campaign__c);
        	}
        }