The Ninja Forms Salesforce plugin lets you add attachments automatically to Accounts and Contacts. If, however, you have a custom object, it can’t automatically add it out of the box because I don’t know the name of your custom object that you want to use. For that reason, I built in the ability for you to create your own link so that you can attach an uploaded file to your custom object.
How To Link an Attachment to a Custom Object
add_filter('nfsalesforcecrm_filter_child_object_array', 'lb3_attachment_to_custom_object'); function lb3_attachment_to_custom_object($default_child_object_array){ $modified_array = $default_child_object_array; $modified_array[ 'my_custom_object_name']=array( 'Attachment'=>'ParentId' ); return $modified_array; }
Note that in this example, you would replace my_custom_object_name with the name of your custom object as displayed in the Salesforce Object list in the Salesforce Settings of your Ninja Forms Settings page.
Setting an Appropriate Object Order
Because we are taking the custom object we just created and putting its ID into the attachment object, we need to ensure that we create the custom object before trying to create the attachment. This is accomplished by filtering the Salesforce Object Order to add our custom object in the appropriate order. Attachments are created at sequence number ’50’ so a good order number for a custom object is ’15’. See the snippet below.
add_filter('nfsalesforcecrm_filter_object_order', 'lb3_add_custom_object_order'); function lb3_add_custom_object_order($default_object_order){ $modified_array = $default_object_order; $modified_array[ 'my_custom_object_name']='15'; return $modified_array; }
How It Works
The objects in Salesforce are linked but adding the ID of the parent object as a field in the child object. For attachments, the field used is called ‘ParentId’. The plugin has a configured array that contains an array of standard Salesforce objects, each of which has an array of child objects that holds the name of the field used for linking. When one of the parent objects is created, the Ninja Forms Salesforce extension looks through this array and then adds the newly created ID into any available child objects using the specified field.
In our example, we add a new key called my_custom_object_name and specify that if an Attachment is also being created by the form submission, we want to to that the my_custom_object_name ID and add that as the ParentId field for the attachment, thus linking the two together.
As always, if you have any questions, please don’t hesitate to contact me.
Sincerely,
Stuart Sequeira