I am using the restforce gem to push data to salesforce once an object is saved in my rails application. Everything is working fine but in the process of making my code DRY I encountered an issue. I have a group of fields that I am submitting with every object I save. However, depending on the object type I want to submit other fields. I'm not sure how to set a group of field mappings to a variable.
I'm not getting any errors in console but nothing is pushing to salesforce.
model.rb
def create_application
constant_fields = Name: object.name, Email: object.email
if object.type == "car"
car_fields = Wheel_Size__c: object.wheel_size, Colour__c: object.car_colour)
Restforce.new.create!(constant_fields, car_fields)
else
plane_fields = Wing_Span__c: object.wing_span, Tail_Number__c: object.tail_number
Restforce.new.create!(constant_fields, plane_fields)
end
end
You're not using the proper syntax for Ruby hashes. Try:
def create_application
constant_fields = { Name: object.name, Email: object.email }
if object.type == "car"
car_fields = { Wheel_Size__c: object.wheel_size, Colour__c: object.car_colour }
Restforce.new.create!(constant_fields.merge(car_fields))
else
plane_fields = { Wing_Span__c: object.wing_span, Tail_Number__c: object.tail_number }
Restforce.new.create!(constant_fields.merge(plane_fields))
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With