1

We have a custom button called send an email at the activity level. It will work based on the case fields. Now i have a requirement to check some conditions based on a field called total active units from the account.

I have written the query but it's not giving me any results like undefined.

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
var Results= sforce.connection.query("SELECT Id,Total_Active_Units_new__c from account where id ='{!Case.AccountId}' LIMIT 1");
var AccountResults = Results.getArray("records");
var Total_units = parseInt("AccountResults.Total_Active_Units_new__c");

Please let me know where i went wrong.

1

I've not used the Ajax Toolkit much, but this seems to be a problem of misunderstanding what Results.getArray("records") gets you.

Looking at the Ajax Toolkit dev guide, after having performed a sforce.connection.query, you always have an array of results, even if your query can only retrieve 1 record.

So problem 1, is you need to get the element out of the array. I might use array destructuring if it was me, just because I like that syntax.

const [totalUnitsFieldValue] = Results.getArray("records");

Then you can parseInt (if you need).

You'll also want to make sure you pass the variable name into parseInt, not a string.

The following will always produce a return of NaN:

parseInt("AccountResults.Total_Active_Units_new__c");
parseInt("Anything here that is a string will also produce NaN unless it can actually be parsed to a Number type");

So following on from the above, you'd then do something like:

const totalUnitsNum = parseInt(totalUnitsFieldValue.Total_Active_Units_new__c); 

But you might want to check to make certain this is even necessary.

1
  • I was scratching my head from the past few hours. Yes, I've misunderstood the result.thanks for sharing knowledge pchittum Much Appreciated!
    – Pavan tej
    Feb 19 '20 at 12:20
1

Results.getArray("records") returns the array, So you will have to use 0th index, use below snip, It will work fine. Change (AccountResults[0])

var Total_units = parseInt(AccountResults[0].Total_Active_Units_new__c);

Edit: removed double quotes from parseInt method.

2
  • I have already tried this & it's not working
    – Pavan tej
    Feb 19 '20 at 12:09
  • I just tried and it gives me result for Id field. could you check without parseInt function and let me know if issue persists with parseInt function. and make sure that you have not used double quotes inside parseInt function.
    – RKM
    Feb 19 '20 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.