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
Harry1008Harry1008 

Trigger on ContentVersion on Lead

Hi All,
I wrote a Trigger on ContentVersion. The requirement is when I upload a file on the lead when the lead is only on the status "Engagement" then the status should change to "Negotiation". with the below trigger which ever the status is if I upload the file the lead status is changing to "Converted".
Could someone help please.
trigger FileUploadonLead on ContentVersion (after insert) {
map <id,string> lmap = new map <id,string>();
    list<lead> leadlist = new list <lead>();
    for(contentversion cv : trigger.new){
        id x = cv.FirstPublishLocationId;
        if(string.valueof(x.getsobjecttype()) == 'lead'){
            system.debug('inside test');
            lmap.put(cv.FirstPublishLocationId,'Negotiation');
            
            
        }
        
        
    }
    
    for(lead l : [select id,status from lead where id in : lmap.keySet()]){
        l.status = lmap.get(l.id);
        leadlist.add(l);
        
    }
    
    if(leadlist.size()>0){
        
        update leadlist;
    }
}

 
Best Answer chosen by Harry1008
Andrew GAndrew G
OK, as noted, your code is straightforward, I've only made a small change in the logic, that is adding the check on the previous status of 'Engagement'
trigger FileUploadonLead on ContentVersion (after insert) {
	Map <Id,String> lmap = new Map <Id,String>();
	List<Lead> leadlist = new List <Lead>();
	for(ContentVersion cv : trigger.new){
		Id x = cv.FirstPublishLocationId;
		if(String.valueof(x.getsobjecttype()) == 'Lead'){
			System.debug('inside test');
			lmap.put(cv.FirstPublishLocationId,'Negotiation');
		}
	}
	
	for(Lead l : [SELECT Id,Status FROM Lead Where Id IN : lmap.keySet() AND Status = 'Engagement']){
		l.status = lmap.get(l.id);
		leadlist.add(l);
	}
	
	if(leadlist.size()>0){
		Update leadlist;
	}
}
I have tested in the UI by creating a lead with a status not 'Engagement' or 'Negotiation', added a file, and the status remained unchanged.  Changed the status to 'Engagement' and added a 2nd file - status changes to 'Negotiation'.

I would suggest making the above change and running the same UI test as noted.  

*note: the check for the Status could also be made when you are creating the Map.

Regards
Andrew

All Answers

Andrew GAndrew G
Hi Harry
The trigger look innocuos - not quite the way I would write it, but the logic is there.

My questions would be 

1.  What is the behaviour if the Trigger is commented out and does not run?  
2.  With the trigger in place, have you done a debug on list prior to the update, to see what the status is prior to Update?  Something like
if(leadlist.size()>0){
        for (lead l : leadlist) {
            System.debug('lead id and status ' + l.Id + ':' + l.status);
        }
        update leadlist;
    }
3.  On your Status Picklist values, is Converted an actual status or are you saying the lead is converted (i.e. Account created).?

If answer to 1 is that the Status still ends up as 'Converted', good news - the trigger is not the culprit.  Bad news - what is?  perhaps check Process builder or check the debug logs for any other triggers firing. Maybe on the Lead itself.

If answer to 1 is that status does not go 'Converted', and the answer to the question 2 is that the debug log shows the values and statuses correctly, then digging in the debug logs for what is changing after that.  Check in your Lead object for other triggers perhaps.

question 3 is just for clarity on where your question is - example :
Showing lead Picklist values with Qualified entry as converted

HTH 
Andrew
Harry1008Harry1008
@Andrew G, Thank you so much for your reply Andrew.

1) The behaviour is normal and the status not moving to Converted. We have different statuses on Lead.
2) The status prior to the update is "Engagement".
3) On status picklist it ends up with Converted not converted to Account.

There are no other triggers or Process builder that changes the status.
Could you please assist me on how to solve the issue.
The requirement is when I upload the file only when the status is "Engagement" it should update the status to "Negotiation".
Thank you for your time in advance.
Kind Regards
Harry
Andrew GAndrew G
OK, as noted, your code is straightforward, I've only made a small change in the logic, that is adding the check on the previous status of 'Engagement'
trigger FileUploadonLead on ContentVersion (after insert) {
	Map <Id,String> lmap = new Map <Id,String>();
	List<Lead> leadlist = new List <Lead>();
	for(ContentVersion cv : trigger.new){
		Id x = cv.FirstPublishLocationId;
		if(String.valueof(x.getsobjecttype()) == 'Lead'){
			System.debug('inside test');
			lmap.put(cv.FirstPublishLocationId,'Negotiation');
		}
	}
	
	for(Lead l : [SELECT Id,Status FROM Lead Where Id IN : lmap.keySet() AND Status = 'Engagement']){
		l.status = lmap.get(l.id);
		leadlist.add(l);
	}
	
	if(leadlist.size()>0){
		Update leadlist;
	}
}
I have tested in the UI by creating a lead with a status not 'Engagement' or 'Negotiation', added a file, and the status remained unchanged.  Changed the status to 'Engagement' and added a 2nd file - status changes to 'Negotiation'.

I would suggest making the above change and running the same UI test as noted.  

*note: the check for the Status could also be made when you are creating the Map.

Regards
Andrew
This was selected as the best answer
Harry1008Harry1008
@Andrew G, Thank you so much for your assistance. Its working.