Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Salesforce by (11.9k points)

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 the 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 

1 Answer

0 votes
by (32.1k points)
edited by

It seems you're not using 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 are preparing for the Salesforce admin certification exam, you can take up this Salesforce training by Intellipaat! 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...