Not sure exactly how to ask this as I am learning as I go. I am creating/updating user information from my app to a third party CRM system.
I have two methods that run successfully with an after_save
callback. During testing, I would comment one out so I can test the other but now I need to combine them with an if else statement.
What should happen when combined together is when User
is saved, the system will see if a user exists on the CRM system - if agile_id?
. If user exists, it will skip down to the update call and send over any updated contact data, but if it doesn't, it will create a new CRM contact record.
The error I am receiving in the browser is:
undefined method `agile_id?' for #<User:0x007ffe24cef318>
user.rb
...
after_save :sync_to_agilecrm
...
def sync_to_agilecrm
agile_id = AgileCRM.request :get, 'contacts/search/email/'+email, nil
if agile_id?
contact_data = {
'properties': [
{ 'type': 'SYSTEM', 'name': 'first_name', 'value': first_name },
{ 'type': 'SYSTEM', 'name': 'last_name', 'value': last_name },
{ 'type': 'SYSTEM', 'name': 'email', 'subtype': 'work', 'value': email },
{ 'type': 'SYSTEM', 'name': 'address', 'value': '{\"address\":\"225 George Street\",\"city\":\"NSW\",\"state\":\"Sydney\",\"zip\":\"2000\",\"country\":\"Australia\"}' },
]
}
parsed_contact_data = JSON.parse(contact_data.to_json)
print(AgileCRM.request :post, 'contacts', parsed_contact_data)
else
update_contact_data = {
'id': agile_id,
'properties': [
{ 'type': 'SYSTEM', 'name': 'first_name', 'value': first_name },
{ 'type': 'SYSTEM', 'name': 'last_name', 'value': last_name },
{ 'type': 'SYSTEM', 'name': 'email', 'subtype': 'work', 'value': email },
{ 'type': 'SYSTEM', 'name': 'address', 'subtype': 'work', 'value': address_line1 },
]
}
parsed_update_contact_data = JSON.parse(update_contact_data.to_json)
print(AgileCRM.request :put, 'contacts/edit-properties', parsed_update_contact_data)
end
end
...
agile_id
and agile_id?
aren't the same thing. You'll sometimes see ActiveRecord objects which have record.attribute?
which is enabled through some meta programming.
So, when defining a variable such as agile_id
, adding a question mark on the end won't just work, nor is it needed. a simple if agile_id
should be sufficient.