Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set variable to group of field mappings in rails app with external api (salesforce)

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 
like image 318
Questifer Avatar asked Dec 02 '25 09:12

Questifer


1 Answers

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 
like image 91
Dan Kohn Avatar answered Dec 05 '25 00:12

Dan Kohn