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
JagadeesJagadees 

To check the previous lead status while updating the related object field: "CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY"

I am trying to check the status of Lead while updating a task field. My requirement is, I need to check the lead status while creating a task for the concerned lead. The task subjects are mapped to the lead status. And once the lead reaches a Working-Contacted status the Trigger should not update the lead status further and just to add the task entry to the lead. When I try to do an entry, system throws me an error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY.  And Below is the trigger for the same. Please help me if I miss something here.

 for(task t : trigger.new)
    {
        if(t.whoid!=null && string.valueof(t.whoid).startsWith('00Q'))
        {
           //system.debug('test_1 for status' +Trigger.oldMap.get(t.whoid).Status );
           
           if(t.Call_Disposition__c == 'Wrong Number'||t.Call_Disposition__c == 'Number Busy'||t.Call_Disposition__c == 'Person Busy')
      
               {
                   if(!Trigger.oldMap.get(t.whoid).Status.contains ('Working - Contacted'))
                   {
                    leadtoupdate.add(new lead(id=t.whoid,status='Open - Not Contacted'));
                    system.debug('test for status' +Trigger.oldMap.get(t.whoid).Status );   
                   }
               }  
if(Call_Disposition__c == 'Call Again Later'||t.Call_Disposition__c == 'Follow-up On a Later Date'||t.Call_Disposition__c == 'Schedule a Call'')   
               {
                    leadtoupdate.add(new lead(id=t.whoid,status='Working - Contacted'));
               }     
}
 update leadtoupdate;

}

Best Answer chosen by Jagadees
Shruti SShruti S
Great start! Gone are the times when we had to write Apex Triggers for such situations. Enter - Process Builder! Welcome to the world of Admins ;)
Believe me this one can be done via Process Builder [Setup | Create | Workflows & Approvals | Process Builder] without writing a single line of code. I just replicated this thing in my Org and it works like a charm. Take a look at this - 
 
User-added image

User-added image

User-added image

User-added image

User-added image

Feel free to let me know if you have any more doubts.

Happy Salesforcin ;) 

All Answers

Shruti SShruti S
Great start! Gone are the times when we had to write Apex Triggers for such situations. Enter - Process Builder! Welcome to the world of Admins ;)
Believe me this one can be done via Process Builder [Setup | Create | Workflows & Approvals | Process Builder] without writing a single line of code. I just replicated this thing in my Org and it works like a charm. Take a look at this - 
 
User-added image

User-added image

User-added image

User-added image

User-added image

Feel free to let me know if you have any more doubts.

Happy Salesforcin ;) 
This was selected as the best answer
Sukanya BanekarSukanya Banekar
Hi Steve,
I think You are writting trigger on before insert so you are getting this error.
You need to write trigger on after insert on task (I don't know on which event you are writting the trigger)
You can try with following code.
map<Id,Lead> mapId_lead = map<Id,Lead>();

for(Lead objLead :[SELECT Id, status from Lead]){
	mapId_lead.put(objLead.Id,objLead.status)
}
for(Task t : trigger.new){
	if(t.whoid!=null && string.valueof(t.whoid).startsWith('00Q')){
		 if(t.Call_Disposition__c == 'Wrong Number'||t.Call_Disposition__c == 'Number Busy'||t.Call_Disposition__c == 'Person Busy'){
			if(mapId_lead.get(t.whoid).status=='Working - Contacted'){
				mapId_lead.get(t.whoid).status='Open - Not Contacted';
			}
			else if(t.Call_Disposition__c == 'Call Again Later'|| t.Call_Disposition__c == 'Follow-up On a Later Date'||t.Call_Disposition__c == 'Schedule a Call''){
				if(mapId_lead.get(t.whoid).status <>'Working - Contacted'){
				mapId_lead.get(t.whoid).status='Working - Contacted';
			}
		 }
	}
}
update mapId_lead.values();

I hope this will help to solve you problem.

Thanks,
Sukanya Banekar
Salesforce Developer


 
JagadeesJagadees

@Shruti S,

Many thanks for your post. It works well. Now I have a follow-up question. In my present system, I have a VF page for my Lead home page from where the user view the lead list. And when he clicks a lead (Open Not contacted status), he will be taken to second VF page where he can see the primary details of the lead which are just a read-only. From here the user clicks a custom button to log his call outcomes when he gets a positive response, which in turn triggers the lead status change. I have page redirect utility class which helps the user to navigate to the custom lead details page now. All works well now.

Now when I do this process builder flow, I am triggering the lead status change from an update in the task object. Is there a way in process builder to do a page change/ navigation as well when there is a change in lead status? or I need to achieve this through code? Can you help me with this?     

Shruti SShruti S
No problem, happy to help.

In Process Builder you cannot do any page rediriction, everything runs on the backend. In your case, I am thinking you will have to write seperate code to handle that.