+ Start a Discussion
raj kiranraj kiran 

Javascript issue while fecthing and comparing

Hi All, 
Below  i present you the code logic whic i am performing in clicking and button , where javascript is performed.
ideally once its clicked as per busines logic it should update few values in case object , but we are receiving  the below error sometimes when the button is clicked, I think in the current logic we are not using any getarray to reteive the records,could this be casuing the issue ?
Please share your taughts.
User-added image

Code is
 
{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
var connection = sforce.connection;


sforce.connection.sessionId = '{!$Api.Session_ID}';

var caseId = '{!Case.CaseNumber}';
var status = 'CANCELLED';
var caseStatus = '{!Case.Status}';
var taskstat = '{!Case.Task_Status__c}';
var cs = new sforce.SObject("Case");

if (taskstat == 'Incomplete for Parts' || taskstat == 'Incomplete for Time' || taskstat == 'Onsite'){
alert('Not A valid Task Status to Cancel it');
}
else{
//Start of CRF 46420
var result = sforce.connection.query("SELECT ShippingCountry FROM Account WHERE Id = '{!Case.AccountId}'");
if (result.records.ShippingCountry.contains('CA') || result.records.ShippingCountry.contains('CAN') || result.records.ShippingCountry.contains('Canada')){
cs.id = "{!Case.Id}";
if ('{!Case.BAAN_Cancel_Reason__c}'== null || '{!Case.BAAN_Cancel_Reason__c}' == ''){
cs.BAAN_Cancel_Reason__c = prompt('Enter Cancel Reason','{!Case.BAAN_Cancel_Reason__c}');
if (cs.BAAN_Cancel_Reason__c == null || cs.BAAN_Cancel_Reason__c == ''){
alert('Please Enter Cancel Reason and click OK to proceed further');
}
else{
if (cs.BAAN_Cancel_Reason__c.length > 254){
alert('Only 255 characters allowed');
}
else{
sforce.connection.update([cs]);
}
}
}
else{
cs.BAAN_Cancel_Reason__c = '{!Case.BAAN_Cancel_Reason__c}';
}
}
//End of CRF 46420
var msg = 'This will cancel the SR and the service tech will not go onsite. There will be a delay while the cancellation is processed. Proceed?';
//Start of CRF 46420
if ((result.records.ShippingCountry.contains('CA') || result.records.ShippingCountry.contains('CAN') || result.records.ShippingCountry.contains('Canada'))){
if (cs.BAAN_Cancel_Reason__c != null && cs.BAAN_Cancel_Reason__c != ''){
var c=confirm(msg);
}
}
else if (!(result.records.ShippingCountry.contains('CA') || result.records.ShippingCountry.contains('CAN') || result.records.ShippingCountry.contains('Canada'))){
var d=confirm(msg);
}
//End of CRF 46420
if (c==true)
{
var c = new sforce.SObject("Case");

alert('Dispatch cancelled');

c.id = "{!Case.Id}";
//Start of CRF 46420
c.BAAN_Cancel_Reason__c = cs.BAAN_Cancel_Reason__c;
//End of CRF 46420
// make the field change
c.Task_Status__c = "Cancelled";

// save the change
sforce.connection.update([c]);

//refresh the page
var url = parent.location.href;
parent.location.href = url; 
}
if(d==true){
window.alert('Cancel Dispatch request has been submitted.Please Wait');
var res = sforce.apex.execute('SRUpdateWrapper','WebserviceFunction',{extno:caseId,Status:status});
if (res != 'SUCCESS')
{
alert('Dispatch not cancelled ,Please contact your system administrator');
var c = new sforce.SObject("Case");

c.id = "{!Case.Id}";
// make the field change
c.Task_Status__c = "Dispatch is not Cancelled due to : "+res;

// save the change
sforce.connection.update([c]);

//refresh the page
var url = parent.location.href;
parent.location.href = url

}
else if (res == 'SUCCESS'){
var c = new sforce.SObject("Case");


alert('Dispatch cancelled');

c.id = "{!Case.Id}";
//Start of CRF 46420
c.BAAN_Cancel_Reason__c = cs.BAAN_Cancel_Reason__c;
//End of CRF 46420
// make the field change
c.Task_Status__c = "Cancelled";

// save the change
sforce.connection.update([c]);

//refresh the page
var url = parent.location.href;
parent.location.href = url; 
}
}
else
{
}	
}

 
Magesh Mani YadavMagesh Mani Yadav
Hi Raj,
Can you just replace contains() method with includes(). Try and let me know if you still have issue
raj kiranraj kiran
Hi Magesh,
Thanks for yoru reply.
Since the ShippingCountry contain only specific words like either US or USA or CAN or CA or CANNADA i think code was written in such a way that it is not looking for substring but for whole word. and also this is not consistantly oberved in production.
Magesh Mani YadavMagesh Mani Yadav

Hi Raj,

I have done some modification to ur script. i have replaced your contains method with the match(/CA/gi) and also added to check the length of the shippingcountry.
So this will check for the whole word of the shippingcountry field even if it is lower or upper case. 

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
var connection = sforce.connection;


sforce.connection.sessionId = '{!$Api.Session_ID}';

var caseId = '{!Case.CaseNumber}';
var status = 'CANCELLED';
var caseStatus = '{!Case.Status}';
var taskstat = '{!Case.Task_Status__c}';
var cs = new sforce.SObject("Case");

if (taskstat == 'Incomplete for Parts' || taskstat == 'Incomplete for Time' || taskstat == 'Onsite'){
alert('Not A valid Task Status to Cancel it');
}
else{
//Start of CRF 46420
var result = sforce.connection.query("SELECT ShippingCountry FROM Account WHERE Id = '{!Case.AccountId}'");
var shippingCountry = result.records.ShippingCountry;
if ((shippingCountry.match(/CA/gi)!=null && shippingCountry.length==2) || (shippingCountry.match(/CAN/gi)!=null && shippingCountry.length==3) || (shippingCountry.match(/Canada/gi)!=null && shippingCountry.length==6)){
cs.id = "{!Case.Id}";
if ('{!Case.BAAN_Cancel_Reason__c}'== null || '{!Case.BAAN_Cancel_Reason__c}' == ''){
cs.BAAN_Cancel_Reason__c = prompt('Enter Cancel Reason','{!Case.BAAN_Cancel_Reason__c}');
if (cs.BAAN_Cancel_Reason__c == null || cs.BAAN_Cancel_Reason__c == ''){
alert('Please Enter Cancel Reason and click OK to proceed further');
}
else{
if (cs.BAAN_Cancel_Reason__c.length > 254){
alert('Only 255 characters allowed');
}
else{
sforce.connection.update([cs]);
}
}
}
else{
cs.BAAN_Cancel_Reason__c = '{!Case.BAAN_Cancel_Reason__c}';
}
}
//End of CRF 46420
var msg = 'This will cancel the SR and the service tech will not go onsite. There will be a delay while the cancellation is processed. Proceed?';
//Start of CRF 46420
if ((shippingCountry.match(/CA/gi)!=null && shippingCountry.length==2) || (shippingCountry.match(/CAN/gi)!=null && shippingCountry.length==3) || (shippingCountry.match(/Canada/gi)!=null && shippingCountry.length==6)){
if (cs.BAAN_Cancel_Reason__c != null && cs.BAAN_Cancel_Reason__c != ''){
var c=confirm(msg);
}
}
else if (!((shippingCountry.match(/CA/gi)!=null && shippingCountry.length==2) || (shippingCountry.match(/CAN/gi)!=null && shippingCountry.length==3) || (shippingCountry.match(/Canada/gi)!=null && shippingCountry.length==6))){
var d=confirm(msg);
}
//End of CRF 46420
if (c==true)
{
var c = new sforce.SObject("Case");

alert('Dispatch cancelled');

c.id = "{!Case.Id}";
//Start of CRF 46420
c.BAAN_Cancel_Reason__c = cs.BAAN_Cancel_Reason__c;
//End of CRF 46420
// make the field change
c.Task_Status__c = "Cancelled";

// save the change
sforce.connection.update([c]);

//refresh the page
var url = parent.location.href;
parent.location.href = url; 
}
if(d==true){
window.alert('Cancel Dispatch request has been submitted.Please Wait');
var res = sforce.apex.execute('SRUpdateWrapper','WebserviceFunction',{extno:caseId,Status:status});
if (res != 'SUCCESS')
{
alert('Dispatch not cancelled ,Please contact your system administrator');
var c = new sforce.SObject("Case");

c.id = "{!Case.Id}";
// make the field change
c.Task_Status__c = "Dispatch is not Cancelled due to : "+res;

// save the change
sforce.connection.update([c]);

//refresh the page
var url = parent.location.href;
parent.location.href = url

}
else if (res == 'SUCCESS'){
var c = new sforce.SObject("Case");


alert('Dispatch cancelled');

c.id = "{!Case.Id}";
//Start of CRF 46420
c.BAAN_Cancel_Reason__c = cs.BAAN_Cancel_Reason__c;
//End of CRF 46420
// make the field change
c.Task_Status__c = "Cancelled";

// save the change
sforce.connection.update([c]);

//refresh the page
var url = parent.location.href;
parent.location.href = url; 
}
}
else
{
}	
}
I hope my solution helped you.