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
jevelethjeveleth 

Trigger for populating field on Lead creation

My Lead object contains a custom field Sales_Development_Rep__c that should contain the name of one of the two people on our staff whose user role is Sales Development (as represented by a specific UserRoleID). Sometimes Leads are created by a Marketo flow, and sometimes they are created by the Sales Development staff.

I'm trying to create a trigger that will ensure that when a Lead is created by someone whose role is Sales Development *and* the Sales Development Rep field is blank, the record will autopopulate the Sales Development Rep field with the appropriate person's name (via their User.Id). I've tried several approaches, but I haven't been able to get the trigger to work. The latest version of my code is below. Can anyone help?

trigger FillBlankSDRField on Lead (before insert) { 

for (l : [Select l.Owner.UserRoleId, l.Owner.Username, l.OwnerId, l.Sales_Development_Rep__c, l.Id From Lead l) { if (l.Sales_Development_Rep__c != null) continue; if !(l.Owner.UserRoleId = '00E50000000kyI8') continue; l.Sales_Development_Rep__c = l.OwnerId; }
}

 

NJ101NJ101

Hi,

 

To fulfill this, you could use both declarative and development options.

 

Declarative:

you could create a workflow and perform a field update to set the ownerID.

 

Development:

Following is an example of bulkified code..

 

trigger fillblankSDRfield on lead (before insert){

 

map<id, Lead>  m_map = new map<id, sObj>{};

for(Lead aObj : trigger.new){
    m_map.put(aObj.id, aObj);
}


Map<id, user> m_ownerObj =new map<id, user> [select Id, <all other fields needed> from User where id=: m_map.keyset() ]

for(sObject aObj : triger.new){
    if(aObj.sales_Development_rep__c != null && m_ownerObj.get(aObj.ownerID).UserRoleId =='00E50000000kyI8'){
        aObj.sales_Development_rep__c = m_map.get(aObj.id).name;
    }
    
}

}

 

// Best to use a field to get the role ID than hardcoding it..

NJ101NJ101

Please replace the sObject with Lead.

 

Cheers,
NJ.

jevelethjeveleth

Thanks, but I'm still unable to get the code to fire.

trigger fillblankSDRfield on Lead (before insert){
	// create map of Lead Ids
	Map<Id, Lead> leadMap = new Map<Id, Lead>{};

	// access map to replace Lead ids w/ new values from Lead map
		for(Lead l : Trigger.New){
    		    leadMap.put(l.Id, l);
		}
		// create map of user Ids where id is in Lead Keyset
		Map<Id, User> userMap = new Map<Id, User> 
			([SELECT Id, UserRoleId FROM User WHERE Id = :leadMap.keySet() ]);

		for(Lead l : Trigger.New){
    		    if(l.Sales_Development_Rep__c != null 
			&& userMap.get(l.OwnerID).UserRoleId =='00E50000000kyI8'){
        	  	l.Sales_Development_Rep__c = leadMap.get(l.Id).name;
    			}
    
		}

}

 

NJ101NJ101

Here we go... Like I said earlier, extend the SQL to grap the role name and use that to check. This is because when you deploy this in Live environment, the Id might be changed..

 

trigger fillblankSDRfield on Lead (before insert){
    // create map of Lead Ids
    Map<Id, Lead> leadMap = new Map<Id, Lead>{};

    // access map to replace Lead ids w/ new values from Lead map
        for(Lead l : Trigger.New){
                leadMap.put(l.ownerID, l);
        }
        
        set<id> idKeys = leadMap.keySet();
        // create map of user Ids where id is in Lead Keyset
        Map<Id, User> userMap = new Map<Id, User>
            ([SELECT Id,name, UserRoleId FROM User WHERE Id in : idKeys ]);

        for(Lead l : Trigger.New){               
                if(l.Sales_Development_Rep__c == null &&
                userMap.get(l.OwnerID).UserRoleId == '00E90000000kxVGEAY'){                
                if(userMap.values().size()>0)
                l.Sales_Development_Rep__c =  userMap.get(l.ownerID).userRoleId;
                }
    
        }

}