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

I am using restforce gem to transfer data to salesforce after saving an object in my rails app. Everything works fine, but in the process of creating my DRY code, I ran into a problem. I have a group of fields that I ship with every object that I save. However, depending on the type of object, I want to send other fields. I'm not sure how to set the field mappings group to a variable.

I don't get any errors in the console, but nothing pushes 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 

      

+3


source to share


1 answer


You are not using the correct 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 

      

+3


source







All Articles