+ Start a Discussion
Harry1008Harry1008 

Having trouble on lead status update via apex trigger

Hi All,
I have written a Trigger to update the lead status from Engagement to Negotiation when a file is uploaded on Lead when the status is Engagement. I want to restrict the trigger to fire when the file is uploaded on Engagement status and update the status to negotiation thats it. The trigger should not fire if the lead is on another status and if a user upload the file. Could you please help me how to achieve this in the below code.
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()]){
    system.debug('lmap.get() '+ lmap.get(l.id) );
        l.status = lmap.get(l.id);
        leadlist.add(l);
        
    }
    
    if(leadlist.size()>0){
        system.debug('leadlist ' + leadlist );
        update leadlist;
    }
}

 
Best Answer chosen by Harry1008
Tad Aalgaard 3Tad Aalgaard 3
Wouldn't you just add the following to your SOQL?
and status = 'Engagement'

All Answers

Tad Aalgaard 3Tad Aalgaard 3
Wouldn't you just add the following to your SOQL?
and status = 'Engagement'
This was selected as the best answer
Harry1008Harry1008
@Tad Aalgaard3. Thank you for your response. I am getting an error if I add status = 'Engagement' in my SOQL
Tad Aalgaard 3Tad Aalgaard 3
And that error is...?
Harry1008Harry1008
@Tad Aalgaard3. thank you so much for your assistance. its all working now.