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
Edward VazquezEdward Vazquez 

Creating a new record in a custom object

I want to automatically create a new record in custom Object B when a specific record type is selected in  custom Object A. How would I go about doing this? Please help
Best Answer chosen by Edward Vazquez
Abhishek BansalAbhishek Bansal
Hi Edward,

The lookup field "Properties" on the Resales object is having a filter on it and the value that yiou are trying to populate in this field is not matching with that filter so as a result you are facing this issue.
Please assign value in this field as per the lookup filter in order to get rid of the issue.

Let me know if you need more information or help on this.

Thanks,
Abhishek Bansal

All Answers

Dan1126Dan1126
To create a record automatically in Salesforce off a specific database event, you would need either an Apex Trigger or a Process Builder process. In your case, I would recommend the Process Builder (Setup > Create > Workflow & Approvals > Process Builder). This help doc should get you started: https://help.salesforce.com/HTViewHelpDoc?id=process_create.htm&language=en_US (https://help.salesforce.com/HTViewHelpDoc?id=process_create.htm&language=en_US)
claperclaper
Lightning Process builder is the way to go. Something like this, where Account would be "Custom Object A" and Opportunity would be "Custom Object B", this is fearly simple and the Lightning Process Builder also support custom objects.
User-added image

for more info on process builder check out this trailhead module: https://developer.salesforce.com/trailhead/business_process_automation/process_builder
 
Abhishek BansalAbhishek Bansal
Hi Edward,

You can achieve this with the help of trigger.
Your trigger will be written on the Custom Object A that will chk the record type of the inserted record and if the record type matches your condition than it will create and insert the record of custom object B within your trigger.
I am providing you a sample code below :
trigger createRecords on Custom_Object_A__c (after insert){
	List<Custom_Object_B__c> listOfRecordsToBeInserted = new List<Custom_Object_B__c>();
	Custom_Object_B__c newRecord;
	
	for(Custom_Object_A__c recA : trigger.new){
		if(recA.RecordTypeId == 'IdOfRecordTypeThatYouWantToCompare'){
			newRecord = new Custom_Object_B__c();
			newRecord.Name = 'New Record';//Add all the required fields of Custom_Object_B__c
			listOfRecordsToBeInserted.add(newRecord);
		}
	}
	
	if(listOfRecordsToBeInserted.size() > 0){
		insert listOfRecordsToBeInserted;
	}
}
// Replace Custom_Object_A__c with API name of Object
//Replace Custom_Object_B__c with Api name of object
//Replace 'IdOfRecordTypeThatYouWantToCompare' with Record Type Id

Please take help from the above sample code and also dont forget to make the necessary changes.
Please let me know if i can help you more on this.

Thanks,
Abhishek Bansal.

Arun KumarArun Kumar
Hi Edward,

Go for Lightning Process builder


https://developer.salesforce.com/trailhead/business_process_automation/process_builder


Thanks,
Arun
Edward VazquezEdward Vazquez
That's what I have done but I keep getting an error message. An error occurred at element myRule_1_A1 (FlowRecordCreate).
INSERT --- INSERT FAILED --- ERRORS : (FIELD_FILTER_VALIDATION_EXCEPTION) Value does not exist or does not match filter criteria., 

I will attach my process maybe you can pinpoint what I am doing wrong. 

Properties is object A and Resales is object B. When the properties record type 'Sold' is selected I want new record to automatically be created in the Resales object.

User-added image

User-added image
User-added image
claperclaper
Do you happen to have a filter lookup on the Properties object? 
claperclaper
Sorry, I meant to say is the "Properties" field a filtered lookup in the Resales custom object? That is probably why you are getting the error.
Abhishek BansalAbhishek Bansal
Hi Edward,

The lookup field "Properties" on the Resales object is having a filter on it and the value that yiou are trying to populate in this field is not matching with that filter so as a result you are facing this issue.
Please assign value in this field as per the lookup filter in order to get rid of the issue.

Let me know if you need more information or help on this.

Thanks,
Abhishek Bansal
This was selected as the best answer
Edward VazquezEdward Vazquez
Thank you so much. That solved my problem.