Mapping ACF custom fields
-
Hi,
Thank you for creating and sharing this plugin. I think it is going to prove very useful for a project I am working on. I know from looking at other posts that you don’t officially support ACF – I hope this changes, as I think they would be a powerful combination. Anyway, I have noticed that you do still try to assist with ACF-related queries.
I have used CPT UI to create a WordPress CPT called App. I made sure it supports Custom Fields. I then used ACF to add the following fields to it:
– Company (type: Text)
– LastName (type Text)I have then used Object Sync for Salesforce to map (Fieldmaps) the above fields to the corresponding ones on the standard Salesforce Lead object. NB the above are the required fields on the Salesforce Lead object.
However, I am getting the following error message in the log:
Required fields are missing: [LastName, Company]Note, if I change the mapping to use standard WordPress fields, i.e. post_title -> Last name and post_content -> Company, then it works.
Any help in fixing the error would be appreciated.
Thanks,
rt.
-
@rtd2 Thanks. I can say for sure that as long as MinnPost maintains this plugin, it won’t “officially” support ACF, although it is generally usable with ACF. But we would happily link to anyone who decides to use the hooks we’ve included in this plugin to further build out the support this plugin does include (which is already decent as I say, just not intentional I guess). It’s probably possible, but it would have to ensure that it doesn’t conflict with any of the other countless methods of doing custom fields in WordPress, especially the more default ones.
I’ve done some testing to see what your issue is, and here’s what I think is happening. I created a new “app” post with values for the ACF fields.
1. The way ACF apparently works, the initial save has no values for the ACF fields.
2. Immediately after the first save (within the same millisecond, according to my local testing), ACF runs a post update to add the ACF field values.So because Object Sync for Salesforce doesn’t have a way to detect what ACF is doing, it works like this:
1. It tries to immediately send the values to Salesforce, but then because there are no values for the required fields in Salesforce, it fails to create a Lead.
2. It immediately then tries to update the Lead in Salesforce (because the post save ran again) but it is unable to update it because it doesn’t exist in Salesforce.I realize it’s probably not possible to make LastName and Company optional fields on the Lead object in Salesforce. So here’s what I think are possible options:
1. Map the WordPress User object to the Salesforce Lead instead, since it already has firstname/lastname fields and ACF wouldn’t have to worry about adding its own fields.
2. Try to determine if there are ACF hooks that tell you whether a post is new. I looked around a little, but I’ve been unable to find such a thing.
3. You could probably use the acf_loadfield hook (https://www.advancedcustomfields.com/resources/acf-load_field/) to set a default value for the required fields if they are empty, and then allow ACF to update them when it is ready. You’d have to do this at least for each field type, as far as I can tell. This will require some knowledge of how ACF works, but it is probably the cleanest way to avoid having to change the setup.
4. If you aren’t very dedicated to ACF, you could try using a different custom field plugin that doesn’t create and update posts separately to get the fields in there. CMB2 might work, for example.Jonathan,
I took your suggestion of using CMB2 to create my custom fields, instead of ACF – so I am no longer using ACF. However, the issue persists, so I don’t think it is ACF related.
Regardless of whether I enter values in the custom fields directly via the WordPress admin panel or use a form builder to submit them via the frontend, the Company and the LastName values are stored in the custom fields in WordPress but not pushed to Salesforce, causing it to throw the same error.
In terms of form builders I have tried Gravity Forms, Toolset CRED, Pods, Caldera Forms, Frontend Publishing Pro, BuddyForms, WP User Frontend and Access Press Anonymous Post Pro. The only form builder I have been able to get to work is Gravity Forms (with Gravity Forms + Custom Post Types and Gravity Forms Sticky List). With it, values submitted on the frontend get stored in the custom fields and pushed to Salesforce. Also, values are pulled from Salesforce. The only issue though is because it creates (its own) entries, I cannot get it to push back changes to values it originally pulled from Salesforce (as opposed to those that were submitted via the frontend).
I would be happy to create a test account on my site for you to look into this.
I hope to hear from you soon.
Thanks,
rt.I’ve looked into this off and on a bit. Here’s what I’ve discovered:
1. As you say, with the App type it doesn’t seem to make a difference whether it uses CMB2, ACF, etc. A Lead just isn’t created because of the missing required fields.
2. However, I’ve discovered that I can map a post to a Lead, with First Name, Last Name, and Company as ACF fields (in fact, using the same fieldset that was previously applied to App).This plugin treats custom post types in the same way it treats standard posts, so as far as I can tell, either:
1. There’s something different about how WordPress saves custom post type rows, or
2. There’s an unintentional difference in this plugin with custom type rows.I’d like to investigate this further as I can, but so far I do not have a full solution for you.
Edit: now that I’ve said this, as I test I’ve discovered this:
Currently, this plugin uses the
save_post_{$post->post_type}
hook on custom post objects, and thesave_post
hook on standard posts.I notice that in my initial testing, when I switch custom posts to also use
save_post
instead, it seems to work as expected. I’d like to do some further testing to figure out if this is reliable, and if it has any negative implications before I release it as a fix, but it’s possible this will be the fix for the issue you’ve discovered.-
This reply was modified 7 years ago by
Jonathan Stegall. Reason: an initial test result
-
This reply was modified 7 years ago by
Jonathan Stegall. Reason: code comment
-
This reply was modified 7 years ago by
Jonathan Stegall.
Okay, I feel pretty good about a fix here. The hook’s syntax was incorrect. I’ll be releasing an update to this plugin shortly, and it should fix this issue for you. It’ll be version 1.3.1.
Hi Jonathan,
I have installed v1.3.1.
True, I am no longer getting the error, but the values are still not being saved to Salesforce. Note this is the case both with ACF and Custom Field Suite, which I have started using.
NB Gravity Forms (with Gravity Forms + Custom Post Types and Gravity Forms Sticky List) is still the only form builder I can get to work, but as I pointed out before it’s entries get in the way of updates to/from Salesforce, so it is not a long term solution (for me). I mention it again though, as you might like to examine what it is doing which is working.
Thanks,
rt.It’s odd, then. It works for me, syncing a custom type called App with Salesforce leads, using ACF. It sends the fields back and forth as it should.
Have you cleared the plugin cache? You might want to delete/reinstall the plugin, even.
In addition, I’ve sometimes found that Salesforce can have permissions issues that the plugin can’t detect. You might need to look into that as well, if the data is still not being synced.
At your suggestion, I tried deleting and reinstalling the plugin, but to no avail. I am constantly clearing the cache. Also, my Gravity Forms instance no longer works with v1.3.1, but it does with v1.2.8. The last point suggests to me that something has changed in v1.3.1 that possibly you are not aware of. As before, I am happy to provide you access to my test site.
rt.
We do not provide that kind of support. Here’s what I can do.
1. Here’s an export of the App content type I’m using. You could use this in functions.php
function cptui_register_my_cpts_app() { /** * Post Type: Apps. */ $labels = array( "name" => __( "Apps", "twenty-seventeen-child" ), "singular_name" => __( "App", "twenty-seventeen-child" ), ); $args = array( "label" => __( "Apps", "twenty-seventeen-child" ), "labels" => $labels, "description" => "", "public" => true, "publicly_queryable" => true, "show_ui" => true, "show_in_rest" => false, "rest_base" => "", "has_archive" => false, "show_in_menu" => true, "exclude_from_search" => false, "capability_type" => "post", "map_meta_cap" => true, "hierarchical" => false, "rewrite" => array( "slug" => "app", "with_front" => true ), "query_var" => true, "supports" => array( "title", "editor", "thumbnail" ), ); register_post_type( "app", $args ); } add_action( 'init', 'cptui_register_my_cpts_app' );
2. Here’s an export from ACF that creates the App type’s field group. I’m not an ACF user, but presumably you could import this into ACF or use it directly in functions.php.
if(function_exists("register_field_group")) { register_field_group(array ( 'id' => 'acf_app-fields', 'title' => 'App Fields', 'fields' => array ( array ( 'key' => 'field_5aa4c207211a4', 'label' => 'First Name', 'name' => 'first_name', 'type' => 'text', 'default_value' => '', 'placeholder' => '', 'prepend' => '', 'append' => '', 'formatting' => 'html', 'maxlength' => '', ), array ( 'key' => 'field_5aa4c22b211a5', 'label' => 'Last Name', 'name' => 'last_name', 'type' => 'text', 'default_value' => '', 'placeholder' => '', 'prepend' => '', 'append' => '', 'formatting' => 'html', 'maxlength' => '', ), array ( 'key' => 'field_5aa5e97996c5d', 'label' => 'Company', 'name' => 'company', 'type' => 'text', 'default_value' => '', 'placeholder' => '', 'prepend' => '', 'append' => '', 'formatting' => 'html', 'maxlength' => '', ), ), 'location' => array ( array ( array ( 'param' => 'post_type', 'operator' => '==', 'value' => 'app', 'order_no' => 0, 'group_no' => 0, ), ), array ( array ( 'param' => 'post_type', 'operator' => '==', 'value' => 'post', 'order_no' => 0, 'group_no' => 1, ), ), ), 'options' => array ( 'position' => 'normal', 'layout' => 'no_box', 'hide_on_screen' => array ( ), ), 'menu_order' => 0, )); }
3. Here’s a Gist with an export of the fieldmap I’m using to map the App object to the Lead object in this plugin. You can try saving it as a JSON file and then importing it into this plugin. https://gist.github.com/jonathanstegall/2905b53ca0cdcbb5f2da890b98fafbca
I can’t offer much beyond that, at this point, but if none of this works I would recommend to investigate the permissions and other setup in Salesforce itself.
-
This reply was modified 7 years ago by
Jonathan Stegall. Reason: export php instead of json for content type
Jonathan,
I created a new instance of WordPress, connected it to Salesforce and then imported your three files. Same result. With V1.3.2 of the plugin, nothing works, even my previously working combination of Gravity Forms (with Gravity Forms + Custom Post Types and Gravity Forms Sticky List) and Custom Field Suite. With V1.2.8 Gravity Forms still works, but ACF plus all the other form builders do not. Sigh!
rt.
I’m noticing that the fix I thought I had to fix your issue does not consistently work, so I’m going to reverse it.
I expect this will restore the previously working combination you mention with Gravity Forms. However I still do not have a consistently working solution for custom post types with custom meta fields, either with CMB2 or ACF. The fix works rarely enough that I certainly can’t rely on it.
What I’ve noticed is that custom fields push to required Salesforce fields as they should on a default post type, but not on custom post types. Nothing I’ve been able to try thus far has resolved this, but at least it isolates the problem.
Current plan is to wait a bit and see if I can discover a fix for this, but otherwise I’ll reverse the fix and release an update.
Thanks for the update. Makes sense.
It might be worth looking at how Gravity Forms works, for clues, as I have found it to perform consistently (with V1.2.8).
rt.
My hope is that the recent update, 1.3.4, will fix this issue with custom posts getting metadata. I’ve tested with CMB2 and ACF.
Jonathan,
Just tried V1.3.4. Gravity Forms (with Gravity Forms + Custom Post Types and Gravity Forms Sticky List) and Custom Field Suite is working again, but sadly ACF and Custom Field Suite with other form builders still does not work.
rt.
I’m not sure, but I’m inclined to think there’s something else happening. I’m able to create a custom post type – with custom fields first name, last name, and company name – using ACF, CMB2, or Custom Field Suite and map it to a Salesforce lead, and it creates and updates the leads in Salesforce on push, and updates the app posts in WordPress on pull.
Be sure to let us know if you discover what is happening here, but I don’t think I can offer any further ideas.
-
This reply was modified 7 years ago by
- The topic ‘Mapping ACF custom fields’ is closed to new replies.