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
Erik RodgersErik Rodgers 

Would someone please explain the structure of this syntax

Below is an excerpt from some code we use to make a web service call to a Twilio API in our production environment. It appears very straight forward, but I am new to Force.com Apex syntax, so I'm trying to understand things that just don't make sense to me. So, how does the Map object accept two (2) String parameters for the name/value pair, but the code in the braces represents three (3) Strings - To, From, and Body. 2 is not equal to 3, so I am confused. I know this is a dumb newbie question, but I have to know.

    Map<String,String> params = new Map<String,String> {
        'To'   => toNumber,
        'From' => String.isBlank(twilioCfg.From_Phone_Number__c) ? '+15005550006' : twilioCfg.From_Phone_Number__c,
        'Body' => smsBody
    };

Thanks!
Erik
Best Answer chosen by Erik Rodgers
Himanshu ParasharHimanshu Parashar
aah ok.  This is not like that.

Map is not taking three parameters but actually it is containing three different key value pair. Once you will print this map map count will be 3

At 0 Place 'To'   String,String
At 1 Place <String,String>
at 2 Place <String,String>

Makes sense?


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S.  If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
 

All Answers

Himanshu ParasharHimanshu Parashar
Hi Erik,

Everything is in two string. 'From' will get single value becasue it is taking value using ternary operator (?) where only one value will be assigned in 'From'.

Makes sense ?


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S.  If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
 
Erik RodgersErik Rodgers

Himanshu, I still do not understand. Here is what I am seeing:
  • Map takes two (2) inputs
  • Assignment syntax assigns three parameters: (1) To (2) From (3) Body.
I understand what the ternary operator is doing for the From parameter, but there are still three parameters. What gives??
Himanshu ParasharHimanshu Parashar
aah ok.  This is not like that.

Map is not taking three parameters but actually it is containing three different key value pair. Once you will print this map map count will be 3

At 0 Place 'To'   String,String
At 1 Place <String,String>
at 2 Place <String,String>

Makes sense?


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S.  If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
 
This was selected as the best answer
Erik RodgersErik Rodgers
Man, that kind of answers my question, but I'm still confused how the 0,1, and 2 gets implicity set as the first map String value without explicitly setting it, and where can I get a good explanation of the syntax and whatever "=>"  is?
Erik RodgersErik Rodgers
Ahh, wait.... I see:
Himanshu ParasharHimanshu Parashar
Hi Erik,

First of all, Please accept my appologies that I tried to explain ternary operator to you. I did that because I thought that perhaps you missed that mistakenly. :) and I am again feel sorry to make you confused by my answer. I added that in index format only to explain no. of items map will hold. values never get as indexed they are there only in key,value pair as it happend in other language.


Thanks,
Himanshu