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
vivek12345vivek12345 

Error: Cannot_Insert_Update_Activate_Entity. Need Help

Hello All,

 

I am getting this error from last 2 days now, I have tried every thing which I got from the forms but its not resolved.

 

I have a trigger on Opportunity after insert, after update.

 

Based on amount and Opportunity Start and end date I am generating monthly cash flow, which I insert in a new custom object Opportunities_cash_flow__c, this trigger is working fine in sandbox but when I try to move it in production I get error on 4 other test classes for different triggers.

 

On though triggers I am updating a different object called Project role and Opportunities.

 

I have no Idea why I am getting this error and how to solve it.

 

Thanks

Vivek Manchanda

kiranmutturukiranmutturu

this is mainly because You do not have permission to create, update, or activate the specified record... check it out which object record it is

steve456steve456

could you just show the part of code where you getting the error

vivek12345vivek12345

Hello Kiran,

 

Thanks for your reply, I have all the permissions as I am System Admin.

 

I also tried many different things like commenting my code and trying to do deployment validation to find the location of the error, but can't find the location.

 

Thanks

Vivek

 

vivek12345vivek12345

Hello Steve,

 

Here is my code,

 

	
	for(Opportunity newPR : trigger.new) {
		irecalc = 0;
			date MainStartdate;
		if (newPR.Start_Date__c == null ){
				MainStartdate = System.today();
			}else{
				MainStartdate = newPR.Start_Date__c;
			}
		
		if (trigger.isUpdate ){
			if (MainStartdate != Trigger.oldMap.get(newPR.Id).Start_Date__c){
				irecalc = 1;	
			}				
			if (newPR.ContractEndDate__c != Trigger.oldMap.get(newPR.Id).ContractEndDate__c){
				irecalc = 1;					
			} 
			if (newPR.Duration__c != Trigger.oldMap.get(newPR.Id).Duration__c){
				irecalc = 1;					
			} 
			if (newPR.FP_Amount__c != Trigger.oldMap.get(newPR.Id).FP_Amount__c){
				irecalc = 1;					
			} 
			if (newPR.Engagement_Type__c != Trigger.oldMap.get(newPR.Id).Engagement_Type__c){
				irecalc = 1;					
			} 
			
		} else {
			iRecalc=2;
		}
		//iRecalc=1;
			
		if (irecalc >0 ){
			thePRIds.add(newPR.Id); // add to list of Project Roles being updated
			
		
			date MainEnddate;
			
			
			
			if (newPR.ContractEndDate__c != null){
				MainEnddate = newPR.ContractEndDate__c;
			}
			if (newPR.ContractEndDate__c == null && newPR.Duration__c != null ){
				Datetime TempStartdate = Datetime.newInstance((MainStartdate).year(), (MainStartdate).Month(), (MainStartdate).day(), 17, 0, 0);
				Integer lenght = (newPR.Duration__c).intValue();
				MainEnddate = Date.newInstance(TempStartdate.addMonths(lenght).year(),TempStartdate.addMonths(lenght).month(),TempStartdate.addMonths(lenght).day());
			}
			
			if (MainStartdate != null && MainEnddate != null ){
				integer startMonth = MainStartdate.month(); 
				integer currentMonth = 0;
				integer currentYear = 0;
				decimal profit = 0;
				system.debug('PRFtrigger-StartMonth:' + startMonth);
				decimal monthRev= 0;
				decimal monthCost= 0; 
				decimal calculateddailyCost= 0;
				Datetime dEndOfMonth;
				date dateEndOfMonth; 
				date dateStartOfMonth;
				date IncomeMonth;
				date Invoicedate; 
				date Duedate;
				integer endMonth = MainEnddate.month();
				//integer totalMonths = endMonth - startMonth +1; 
				Decimal totalMonths = newPR.Role_length_months__c; 
				for (integer i = 0; i < totalMonths; i = i + 1){
					currentYear = MainStartdate.year();
					if (i+startMonth == startMonth) {
						// this is the first month 
						currentMonth = i+startMonth; 
						//calculateddailyCost =newPR.Calculated_Daily_Cost__c;
						dEndOfMonth = Datetime.newInstance(currentYear, startmonth, CalculateWorkingHours.lastDayofMonth(startmonth,currentYear), 17, 0, 0);
						dateEndOfMonth = Date.newInstance(currentYear, startmonth, CalculateWorkingHours.lastDayofMonth(startmonth,currentYear));
						
							Invoicedate = Date.newInstance(MainStartdate.year(), startmonth+1, 7);
							Duedate = Date.newInstance(MainStartdate.year(), startmonth+2, 7);
						if (MainEnddate > dEndOfMonth ){
							
							if(newPR.FP_Amount__c != null ){
								//monthRev = (CalculateWorkingHours.Workingdays2(MainStartdate, dateEndOfMonth ,newPR.Days_week__c,bh )) * (newPR.Effective_hourly_rate__c * newPR.Hours_Day__c);
								monthRev = newPR.FP_Amount__c / totalMonths;
							}
							
							if(newPR.Costs__c != null ){
								//monthCost = (CalculateWorkingHours.Workingdays2(MainStartdate, dateEndOfMonth ,newPR.Days_week__c,bh )) * (calculateddailyCost);
								monthCost = newPR.Costs__c / totalMonths;
							}
						} else {
							//last month of contract
							if(newPR.FP_Amount__c != null ){
								//monthRev = (CalculateWorkingHours.Workingdays2(MainStartdate, MainEnddate ,newPR.Days_week__c,bh )) * (newPR.Effective_hourly_rate__c * newPR.Hours_Day__c);
								monthRev = newPR.FP_Amount__c / totalMonths;
							}
							
							if(newPR.Costs__c != null ){
								//monthCost = (CalculateWorkingHours.Workingdays2(MainStartdate, MainEnddate ,newPR.Days_week__c,bh )) * (calculateddailyCost);
								monthCost = newPR.Costs__c / totalMonths;
							}
						}
						//}
						profit = monthRev - monthCost;//, Company_Profit__c = profit // Monthly_Salary__c = monthsal,Day__c =Lastfridayofthemonth ,
						prcf.add(new Opportunities_cash_flow__c (CurrencyIsoCode = newPR.CurrencyIsoCode ,Monthly_Income__c= monthRev ,Month__c = CalculateWorkingHours.readableMonth(startMonth), Due_date__c = Duedate,Invoice_date__c = Invoicedate,  Opportunity__c= newPR.Id,Projected_invoice_amount__c = monthRev,Projected_cost_amount__c = monthCost));
					} else {
						currentMonth = i+startMonth;
						
						if (currentMonth==1){
							currentYear = currentYear+1; 
						}
						dEndOfMonth = Datetime.newInstance(currentYear, currentMonth, CalculateWorkingHours.lastDayofMonth(currentMonth,currentYear), 0, 0, 0);
						dateEndOfMonth = Date.newInstance(currentYear, currentMonth, CalculateWorkingHours.lastDayofMonth(currentMonth,dEndOfMonth.year()));//CalculateWorkingHours.lastDayofMonth(dEndOfMonth.month())
						dateStartOfMonth = Date.newInstance(currentYear, currentMonth, 1);
						if (dateStartOfMonth < dateEndOfMonth ){
						
						
							Invoicedate = Date.newInstance(MainStartdate.year(), currentMonth+1, 7);
							Duedate = Date.newInstance(MainStartdate.year(), currentMonth+2, 7);
						
						if (MainEnddate > dEndOfMonth ){
							if(newPR.FP_Amount__c != null ){
								//monthRev = (CalculateWorkingHours.Workingdays2(MainStartdate, dateEndOfMonth ,newPR.Days_week__c,bh )) * (newPR.Effective_hourly_rate__c * newPR.Hours_Day__c);
								monthRev = newPR.FP_Amount__c / totalMonths;
							}
							
							if(newPR.Costs__c != null ){
								//monthCost = (CalculateWorkingHours.Workingdays2(MainStartdate, dateEndOfMonth ,newPR.Days_week__c,bh )) * (calculateddailyCost);
								monthCost = newPR.Costs__c / totalMonths;
							}	
						} else {
							//last month of contract
							if(newPR.FP_Amount__c != null ){
								//monthRev = (CalculateWorkingHours.Workingdays2(MainStartdate, dateEndOfMonth ,newPR.Days_week__c,bh )) * (newPR.Effective_hourly_rate__c * newPR.Hours_Day__c);
								monthRev = newPR.FP_Amount__c / totalMonths;
							}
							
							if(newPR.Costs__c != null ){
								//monthCost = (CalculateWorkingHours.Workingdays2(MainStartdate, dateEndOfMonth ,newPR.Days_week__c,bh )) * (calculateddailyCost);
								monthCost = newPR.Costs__c / totalMonths;
							}				
						}
						}
						profit = monthRev - monthCost;//, Company_Profit__c = profit ,Monthly_Salary__c= monthsal Day__c =Lastfridayofthemonth ,End_day__c = CalculateWorkingHours.readableDay(Duedate),Start_Day__c = CalculateWorkingHours.readableDay(Invoicedate),
						if ( monthRev >= 0 && monthCost >=0){
						prcf.add(new Opportunities_cash_flow__c (CurrencyIsoCode = newPR.CurrencyIsoCode ,Monthly_Income__c= monthRev ,Month__c = CalculateWorkingHours.readableMonth(currentMonth), Due_date__c = Duedate,Invoice_date__c = Invoicedate,  Opportunity__c= newPR.Id,Projected_invoice_amount__c = monthRev,Projected_cost_amount__c = monthCost));
						}
					} 
					
			
				}
			}
		}
	
	}

 Please let me know if you need any other information.

 

Thanks

Vivek

kiranmutturukiranmutturu

just check your org wide settings of opportunity in production once... 

vivek12345vivek12345

Hey Kiran,

 

Is there any specific way through which I can check org wide setting or the only way is to go through individual profile.

 

Thanks

VIvek

kiranmutturukiranmutturu

is this trigger based on which events on which object?

vivek12345vivek12345

Hello Kiran,

 

Trigger is on Opportunity after insert, after update.

 

Thanks

Vivek

vivek12345vivek12345

Hello Kiran,

 

While deployment to production I am getting error on all the trigger and test class which are on Project Role object (Custom object). But on this trigger I am not even touching Project role.

 

But from trigger on project role I am updating Opportunity amount.

 

I am not sure but I think I am getting stuck in Infinite loop, as my old triggers (On Project Role) updates Opportunity and then this trigger invoke again. 

 

Thanks

Vivek

steve456steve456

When you get an error as cannot update create activity make sure you check for the field level security of the fields 

 

Go to the user and check field level security for the particular obj

vivek12345vivek12345

Hello Steve,

 

I have checked all the field level security settings as I am the system Admin I have all the rights.

 

Thanks

Vivek

kiranmutturukiranmutturu

 I don't think there is any problem with your code. Its just there is some problem at FLS(Field Level Security) of Activity. Please make sure all fields of Activity , Task and Event is visible for System Administrator on both orgs (Also opportunity fields which your are using). Hopefully this will resolve your problem.

 

If this still doesn't resolves your problem then make sure CRUD rights are given to objects which you are using in this code

kiranmutturukiranmutturu

 I don't think there is any problem with your code. Its just there is some problem at FLS(Field Level Security) of Activity. Please make sure all fields of ur respective objects is visible for System Administrator on prod orgs (Also opportunity fields which your are using). Hopefully this will resolve your problem.

 

If this still doesn't resolves your problem then make sure CRUD rights are given to objects which you are using in this code

vivek12345vivek12345

Hello Kiran /  Steve

 

Thanks for your help, The issue is still not resolve, I am trying to find any other way to resolve it. I have check all the settings but nothing seems wrong to me in settings, But still if you guys find any other solutions please let me know.

 

Thanks a lot for your advice and help.

 

Thanks

Vivek