AnsweredAssumed Answered

Set value for Taxrate dropdown

Question asked by Sarika Tandale on Jan 29, 2018

Hello, 

This is the functionality I am trying to achieve-

 

I am trying to Edit a record's tax rate and address fields already present in the Quotes module.

I have created a custom button "Get_Tax" which when clicked, sends a request to a custom API to get the tax rate at a specific location. The tax received as response is to be set as the tax rate in the Quotes module. I am able to get this working.

However, I have also written few lines of code to override the save button. On clicking the save button, a request is sent to ANOTHER custom API , which sends a validated address as response. This address is set in the address field in the Quotes module.

The problem here is when I click on save, it resets the tax rate (This tax rate was set before using the response received from the 1st custom tax API). So the on-click on the save button is not carrying the value that I set for the tax rate drop-down using another custom API. However, on-click on save does properly set the address fields.

 

I have attached the code herein. Is there a way to pass the "getTaxResponse" variable value from get_tax function into the save_button function? That I suppose will solve this issue.

 

Thank you for taking the time out to go through the post patiently

 

 

The code- 

({
extendsFrom: 'RecordView',

initialize: function(options) {
this._super('initialize', [options]);

// Calling the get_tax function when the "Get Tax" button is clicked.
this.context.on('button:get_tax:click', this.get_tax, this);

// Overrding save button
this.model.addValidationTask('addressValidation', _.bind(this.save_button, this));


},

save_button: function(fields, errors, callback) {

/* Getting few variable values using this.model.get

.
.
*/


// This ajax call validates the address and sets the fields according to the response received from a custom API.

$.ajax({
type: 'GET',
async: 'false',
url: 'someURL',
crossDomain: 'true',
headers: {
'key': 'value'
},
success: function(response) {
cityNow = response.city;
streetNow = response.street1;
stateNow = response.state;
zipNow = response.postalcode;
countryNow = response.country;

t.model.set('shipping_address_city',cityNow);
t.model.set('shipping_address_street',streetNow);
t.model.set('shipping_address_state',stateNow);
t.model.set('shipping_address_postalcode',zipNow);
t.model.set('shipping_address_country',countryNow);
t.model.set('taxrate_name',taxRate);

callback(null, fields, errors);

},

error: function($xhr) {
app.alert.show('address-ok', {
level: 'error',
messages: $xhr.responseJSON.message,
autoClose: true
});
}
})
},

get_tax: function() {

if (currentCountry.match('USA') {
passCountry = 'US';

} else {
passCountry = 'usa';
}


// AJAX call to get the value from another custom API
$.ajax({
type: 'GET',
async: 'false',
url: 'anotherURL',
crossDomain: 'true',
headers: {
'key': 'value'
},
statusCode: {
// If response is 200, set the tax rate in sugar accordingly.
200: function(response) {
getTaxResponse = response.taxSales*1000;
t.model.set('taxrate_name',getTaxResponse);
},
// Display an error message if incorrect address is sent to the service.
400: function(response) {
app.alert.show('address-not-ok', {
level: 'error',
messages: 'Invalid Address',
autoClose: false
});
}
}

})
}

})

Attachments

Outcomes