ruckus / quickbooks-ruby

Quickbooks Online REST API V3 - Ruby
MIT License
374 stars 302 forks source link

how to reference customer email address #466

Closed dougq closed 5 years ago

dougq commented 5 years ago

I have the following code

qb_records = @service.query("Select * from Customer order by id")

and cannot figure out how to reference email addresses in the results. I have a loop of the form qb_records.each do |qb_record| and if I try to reference the email address via qb_record.email_address, I get something along the lines of #

If I do qb_record.email_address.inspect, I get something like #<Quickbooks::Model::EmailAddress address: name@gmail.com> (So I can see that there is an email address there.)

But if I do qb_record.email_address.address, I get "undefined method `address' for nil:NilClass"

What code returns the actual email address as a string?

Thanks!

ruckus commented 5 years ago

What happens when you look at the address property of your Quickbooks::Model::EmailAddress object?

dougq commented 5 years ago

What's the notation for doing that?

ruckus commented 5 years ago

Give this a shot:


qb_records = @service.query("Select * from Customer order by id")
qb_records.entries.each do |qb_record|
   if qb_record.email_address
     puts "The email address is: #{qb_record.email_address.address}"
   end
end
ruckus commented 5 years ago

I think whats happening is in some cases your Customer records do not have an email address, thus the outer container email_address itself is non-existent (QB won't return that node if it doesnt exist; its not like it returns but its blank).

So in your case you just need to check if it exists before you attempt to reference it.

dougq commented 5 years ago

Thank you thank you thank you--success! I was obviously looking at the error message WAY too narrowly!