function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PlainviewPlainview 

Logic to return "null" if Contact Country is not United States.

Hello,

I am a new System Admin. obliged to function above my ability - so what else is new?

We use an Apex Class "ProvisioningInfo," which is tied to a Visualforce page button users may select on the Contract object layout that feeds the Billing Contact information into a system called Back Office. I would like to know the "what" and "where" to add some logic to the class so that it returns "null" if the Billing Contact country is not the United States.  Thanks for your time!

Here are some screen shots of what I believe are the relevant areas in the Apex code:

User-added image
User-added image


Best Answer chosen by Plainview
PlainviewPlainview
This just in! The code above had a few issues, so we cooked-up some more and it worked beautifully. Here's the change; remove what's above and in its place:

if(admincontact.contact.mailingcountry == 'United States')
{ boma.StateId = BOlookupMaps.getStateISO(adminContact.Contact.MailingState); }
else
{ boma.StateId = ''; }

All Answers

Daniel B ProbertDaniel B Probert
so just to be clear you are saying you only want to see records with the country United States - all the rest should be ignored?

if so then add

AND Countact.MailingCountry == 'United States'

on the end of line 649 & 670

thanks
dan
PlainviewPlainview
Hi Dan,

Thanks for your response. Ah, I wish that was it but ... no, that's not quite it.

If the country of the Billing Contact is not the United States, we want to automatically fill in the field with "null" or "n/a" so tha that the user will have to complete it and so we won't get an error message. Back Office is not great at recognizing foreign country codes. Is that clearer? Sorry for the confusion.

Best,
Julien


Daniel B ProbertDaniel B Probert
in which case i wouldn't do that in apex i'd do it with a simple workflow / field update.

that basically fires when the country is not equal to united states - to avoid it firing again and again do it when the record is created only..

then you can set the field update to change country to null or n/a

hope that helps



PlainviewPlainview
Thanks again. By the time the user is at this point in the process, the Contact records have already been created, so I don't think a workflow rule will work.

So, our process is:

A contract is created;

From the contract object, the user selects an Apex Class button entitled "Create Provisioning Case";
A VisualForce page is then opened (Apex Class "ProvisioningInfo") which has another button available entitled "Create Back Office Master Account";
When the user selects this button, the system is currently returning the following error message when the Billing Contact country is not USA:

User-added image

We want the system to fill in "null" or "n/a" for the Back Office Master Account when the country is not USA.

Our instance is heavily customized at this point, so even the simplest things become complex it seems.

Any and all advice is appreciated.
Daniel B ProbertDaniel B Probert
i think i'd need to see the complete code to fully understand but the reason your getting this error specifically around the US. I can't see anthing in what you've shared would cause an error nor anywhere i could put logic to resolve it.

you've highlighted something that i'd like to see the method it's calling

Bolookupmaps.getcountryiso - any chance you can share that bit of code - i reckon in there i'll should be able to advise on some logic..
Daniel B ProbertDaniel B Probert
although having said that you could try this.

on line 152 update to something like this.

if(admincontact.contact.mailingcountry == 'United States'){
boma.CountryID = bolookupmaps.getcountryiso(admincontact.contact.mailingcountry);
}
if(admincontact.contact.mailingcountry != 'United States'){
bome.CountryID = null; // or you could put 'n/a'
}

i'd still like to see the bolookupmaps as i think this still might through an error but in theory the logic will only get the isocode the United States..

cheers
dan
PlainviewPlainview
OK, it's 1000 lines, so let me figure out how. I really appreciate the help!



Daniel B ProbertDaniel B Probert
no worries if you can try the logic i just sent and see if that works first - might save you the hassle of sending 1000 lines
PlainviewPlainview
Alright, I'll try it - thanks!

PlainviewPlainview
The code makes a 12 page .pdf - that's how huge. 
Daniel B ProbertDaniel B Probert
lol thats crazy not even sure i want to see a copy of it now :)

although it would be interesting to see what the code is doing.. if you have an account on https://gist.github.com/ - you could just paste it there and share the link..
PlainviewPlainview
Code saved o.k. QA person is testing. We'll see.

Last line should be: "boma" right? (you had "bome.CountryID = null; // or you could put 'n/a'", so I'm just thinking typo and fixed it).
Daniel B ProbertDaniel B Probert
yep type long day for me already it's about to turn midnight here..
PlainviewPlainview

OK, it seems to be working in our Sandbox but we can't test it in Production until next Tuesday probably (I'm leaving soon for a four day birthday weekend - woohoo!) I'll circle back and let you know! Thanks again, Dan, so very much.

Cheers,

J

Daniel B ProbertDaniel B Probert
awesome glad it worked without having to dive into 1000 lines of code :)

happy birthday

note. pre deployment you should check your test class to make sure that your testing for my if statement - i.e. intentionally through in a non United States country to make sure the new lines are tested..

have a good one

dan
PlainviewPlainview
Thanks! 

Secret GIST fyi:

https://gist.github.com/jcamp6/39728b3f6c1c65e04774

Midnight! Yikes! Good God Man, go to sleep!

I'll let you know on Tuesday or Wednesday.
PlainviewPlainview
So, QA finally tested with the following results. Not completely working yet:

Three cases tested. Results, in summary:
1.)
City: Curitiba
Country: Brazil
State: PR (Parana)
--> The create account button was there and worked to create the BO Account but it entered Puerto Rico because "PR" state is Puerto Rico on the pull-down (or the code?).
2.)
For Any City, Puerto Rico the button did not come up at all.
3.)
https://c.na0.visual.force.com/apex/ProvisioningInfo?id=80000000000OS5f&caseID=500000000098YKh&caseNum=00372874&isdtp=vw&nonce=f6c25f7a639ff373f258cf950de4552571d0a1aa31f30c5db2cc63fd09a60b12&sfdcIFrameOrigin=https%3A%2F%2Fssl.salesforce.com
City: Curitiba
Country: Brazil
State: None entered
It defaulted the Country to USA instead of Brazil.
PlainviewPlainview
This just in! The code above had a few issues, so we cooked-up some more and it worked beautifully. Here's the change; remove what's above and in its place:

if(admincontact.contact.mailingcountry == 'United States')
{ boma.StateId = BOlookupMaps.getStateISO(adminContact.Contact.MailingState); }
else
{ boma.StateId = ''; }
This was selected as the best answer