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
Brent Woodard 10Brent Woodard 10 

Need to create my first trigger

I have a custom field on my Lead object, it's a pick list named LeadSource.

On my custom object "Checkout" whenever someone uses a lookup relationship to select the lead, I want it to automatically fill out a custom field on that called marketing source. I need it to do something like the code written below. I'm a C# developer by history and I'm not even sure if I need to create a new object here or if everything is already loaded.

Here is the code I wrote, I wouldn't think it would be much more complicated than this but I need help.
 
trigger <SetMaketingSource> on Checkout__c (before insert, after insert, after update) {
Lead.LeadSource = Checkout__c.Marketing_Source__c
}

 
Raj VakatiRaj Vakati
You no need an trigger for this .. you can able to do it process builder please refer this link 

https://help.salesforce.com/articleView?id=000213419&type=1
https://douglascayers.com/2017/04/02/use-process-builder-to-update-related-records-when-activities-logged/
https://salesforcesidekick.com/2016/07/11/how-to-work-with-related-records-in-process-builder-criteria/
Brent Woodard 10Brent Woodard 10
I can't get that to work.
User-added image
Raj VakatiRaj Vakati
You need to update the related record .. not same record .. 

 
Brent Woodard 10Brent Woodard 10
No, I need to set the checkout field to the lead marketing field.
@Amit Kumar Giri@Amit Kumar Giri
@Brent- There are many different way to do this. You dont need code for this

Approach 1 - Formula Field
>> I hope u alreday created a loop up fiel;d on CheckOut object to make a relationship with Lead
>> If u want to make "marketing source" custom field in Checkout object as a formula field then it will be prety easy
>> Craete a formula field Class "Marketing Source" and make formula return type as "Text" and use below formula. (FYI- There is a standard field called LeadSource present in lead object. i hope u are talking baout this. If not also no issue, u can just update the field in formula)
Text(Lead__r.LeadSource)
>> Now if you create a Checkout record and selected a Lead, then this formula field will auto populate with the lead source of the lead that u selected.
>> Result 
User-added image
Approach 2- Process Builder/Workflow
>> If u dont want formula field, then either workflow or a process builder will do the work
>> Create a process buulder on "Checkout" Object
>> Select "when a record is created or edited"
User-added image
>> Then in criteria select as below (Lead__c is the custom look up field on checkout object which having relationship with Lead)
User-added image
>> Then in immediate action do as below (set LeadSource from Lead to the Marketing Source of custom object Checkout)
User-added image
>> Now activate the process builder and test it
>> Result
User-added image
**** below are the custom field in Checkout Object i used for above. Hope it help u achive ur requirement
Field Label                                                          API Name                                             Data Type    
Lead                                                                    Lead__c                                               Lookup(Lead)            
Marketing Source(Formula Field)                        Marketing_Source_Formula__c          Formula (Text)            
Marketing Source(Update By Process Bldr)        Marketing_Source__c                         Text(100)            
 
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Brent,

Try the below code:
trigger <SetMaketingSource> on Checkout__c (before insert, after insert, after update) {
for(Checkout__c c:trigger.new)
{
if(c.Lead__c!=trigger.oldMap(c.Lead__c)
{
Checkout__c.Marketing_Source__c=c.Lead__r.LeadSource;
}
}

}