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
Tugay Topju 4Tugay Topju 4 

Creating a trigger which will take the data from Leads Object to a custom Object

Hi, so our salesforce system has a custom object, what we need to do is transfer the information from the Lead object to our custom object called momentum_Prospect__c . The trigger has to run when data has been added to the Lead Object from the web 2 Lead form. Does anybody have any idea how this can be executed? Please help!
mritzimritzi
Use Process builder to acheive this.

Type "Process Builder" in Quick Find box.
Click on "Process Builder".
Create New Process

Select Object -> Leads
Add your condition
In Immediate action box -> add -> Action Type = insert Record
select record type -> momentum_Prospect__c.

Add fields that you want to populate, in Type select Reference and select corresponding lead fields.

For example:
https://developer.salesforce.com/trailhead/en/business_process_automation/process_builder

If this solves your problem, Select this Best Answer
Ravi Dutt SharmaRavi Dutt Sharma
Hi Tugay,

First you need to identify the field which tells that this lead is coming from web to lead. Maybe if lead source equals 'Web Form', then you can consider it as a web to lead. You need to populate this lead source in your web to lead form by using hidden fields. Once this is done, you can create a process builder as suggested by Mritzi or you can write a trigger on lead object to insert records in Momentum_Prospect__c object,
Tugay Topju 4Tugay Topju 4
Hi Guys Thanks for the quick replies! So Okay i have implemented the process however for scheduling actions i have put 0 hours after the lead has been created. i have tried to submit through the form it submits to lead but does not create the new record in my custom object. can this be because i have set no criteria but selected it to execute straight away? or is there something else i am doing incrrectly?
Dheeraj ShoppeDheeraj Shoppe
Hi Tugay,

I guess you can now write a trigger on Lead object and create your custom record whenever a lead is created.
mritzimritzi
User-added image

Create new Process
At 3 -> select Lead ( Start the process -> only when a record is created, )
At 4 -> select criteria ( Criteria for executing actions -> filter conditions are met. You can have a hidden field on web-form whose value indicates that lead has been created from Web-from,  or  any means using which you can determine that the record has been created from web-form)

At 5 -> (Immediate Action) -> Add Action  (Don't select scheduled action, something like 0 hours, 1 hours or 1 month after record creation)
User-added image
Record type- > Momentum_Prospect__c

Below that a form would open wherein you have map fields of Momentum_prospectus__c to the reference of corresponding fields of Lead.

Remember: You have make it sure that all the required fields of Momentum_Prospectus__c are atleast mapped to corresponding Lead fields.

If it helps, mark it as Best Answer. 
mritzimritzi
Trigger:
trigger tranferLeadData on Lead(after insert){
	//List<Lead> leadList = new List<Lead>();
	List<momentum_prospect__c> mpList = new List<momentum_prospect__c>();
	for(Lead l:Trigger.new){
		//Add a value in  picklist "LeadSource" on Lead. something like 'webform'
		// hide this defaulted source field on web form.
		// so that everytime a lead is created from web form you can check this condition
		if(l.leadSource=='webform'){
			// map fields of momentum_prospect__c object to corresponding liead fields
			momentum_prospect__c mp = new momentum_prospect__c();
			mp.name = lead.name;
			mp.phone__c = lead.phone;
			//and more
			mpList.add(mp);
			//leadList.add(l);
		}
	}
	if(mpList.size()>0){
		insert mpList;
		//in case you want to delete lead record, uncomment line 2,15 & 21, otherwise remove those lines
		//delete leadList;
	}
	
}

If this helps you out, please mark this Best Answer