[Salesforce]カスタム設定getInstanceメソッドによる取得

カスタム設定getInstanceメソッドによる取得

getInstance()使用ない場合

public class CountryCodeHelper {
  public static String getCountryCode(String country) {

    Country_Code__mdt countryCode = [
      SELECT Id, MasterLabel, Country_Code__c
      FROM Country_Code__mdt
      WHERE MasterLabel = :country
      LIMIT 1
    ];
  
    return countryCode.Country_Code__c;
  }
}

getInstance()使用する場合

public class CountryCodeHelper {
  public static String getCountryCode(String country) {
    Country_Code__mdt countryCode = Country_Code__mdt.getInstance(country);
  
    return countryCode.Country_Code__c;
  }
}

[Javascript]axios使い方

axios使い方

axiosとは、HTTP通信(データの更新・取得)を簡単に行うことができる、JavaScriptのライブラリです。

APIを提供するクラウドサービスに対して、データを送受信することができます。

例として、ライブラリは以下を使っています。

<script src=”https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js”></script>

            try {
                let rt = axios.post("http://localhost:3000/test", {},{
                    headers: {
                        "key": "testkey"
                    }
                });
                tk = rt.data.tk;
            }catch(error){
                console.log(error);
            }

[Javascript]非同期関数を同期関数っぽく呼び出す(async/await)

非同期関数を同期関数っぽく呼び出す(async/await)

<apex:page >
    <html>

        <head>

        </head>

        <body>

            <script type="text/javascript">

                function aFunc2(data) {
                    return new Promise(function(callback) {
                        setTimeout(function() {
                            callback(data * 2);
                        }, Math.random() * 1000);
                    });
                }

                async function sample_async_await() {
                    var val = 100;
                    val = await aFunc2(val);
                    console.log(val);                    // 200
                    val = await aFunc2(val);
                    console.log(val);                    // 400
                    val = await aFunc2(val);
                    console.log(val);                    // 800
                }

                sample_async_await();
                
            </script>

        </body>

    </html>
</apex:page>

結果

[Javascript]promise.then,awaitその3

promise.then,awaitその3

<apex:page >
    <html>

        <head>

        </head>

        <body>

            <script type="text/javascript">

                function resolveAfter2Seconds() {
                    return new Promise(resolve => {
                        setTimeout(() => {
                        resolve('resolved');
                        }, 2000);
                    });
                }

                const promise1 = new Promise((resolve, reject) => {
                    resolve('Success!');
                });

                async function asyncCall() {
                    console.log('calling');
                    const result = await resolveAfter2Seconds();
                    console.log(result);
                    // expected output: "resolved"
                    promise1.then((value) => {
                        console.log(value);
                        // expected output: "Success!"
                    });
                }

                asyncCall();

                const p = new Promise((res, rej) => {
                    res(1);
                })

                async function asyncReturn() {
                    return p;
                }

                function basicReturn() {
                    return Promise.resolve(p);
                }

                console.log(p);
                console.log(basicReturn());
                console.log(asyncReturn());

                console.log(p === basicReturn()); // true
                console.log(p === asyncReturn()); // false

            </script>

        </body>

    </html>
</apex:page>

結果

[Javascript]promise.then,awaitその2

promise.then,awaitその2

<apex:page >
    <html>

        <head>

        </head>

        <body>

            <script type="text/javascript">

                function resolveAfter2Seconds() {
                    return new Promise(resolve => {
                        setTimeout(() => {
                        resolve('resolved');
                        }, 2000);
                    });
                }

                const promise1 = new Promise((resolve, reject) => {
                    resolve('Success!');
                });

                async function asyncCall() {
                    console.log('calling');
                    const result = await resolveAfter2Seconds();
                    console.log(result);
                    // expected output: "resolved"
                    promise1.then((value) => {
                        console.log(value);
                        // expected output: "Success!"
                    });
                }

                asyncCall();

            </script>

        </body>

    </html>
</apex:page>

結果

[Javascript]promise.then,await

promise.then,await

<apex:page >
    <html>

        <head>

        </head>

        <body>

            <script type="text/javascript">

                function resolveAfter2Seconds() {
                    return new Promise(resolve => {
                        setTimeout(() => {
                        resolve('resolved');
                        }, 2000);
                    });
                }

                const promise1 = new Promise((resolve, reject) => {
                    resolve('Success!');
                });

                async function asyncCall() {
                    console.log('calling');
                    promise1.then((value) => {
                        console.log(value);
                        // expected output: "Success!"
                    });
                    const result = await resolveAfter2Seconds();
                    console.log(result);
                    // expected output: "resolved"
                }

                asyncCall();

            </script>

        </body>

    </html>
</apex:page>

結果

[Javascript]promiseのcatch,then

promiseのcatch,then

<apex:page>
    <html>

        <head>

        </head>

        <body>

            <script type="text/javascript">

                Promise.resolve()
                .then(() => {
                    // Makes .then() return a rejected promise
                    throw new Error('Oh no!');
                })
                .catch(error => {
                    console.error('onRejected function called: ' + error.message);
                })
                .then(() => {
                    console.log("I am always called even if the prior then's promise rejects");
                });
            </script>

        </body>

    </html>
</apex:page>

結果

[Javascript]promiseのerror

promiseのerror

<apex:page>
    <html>

        <head>

        </head>

        <body>

            <script type="text/javascript">

                Promise.resolve()
                .then(() => {
                    // Makes .then() return a rejected promise
                    throw new Error('Oh no!');
                })
                .then(() => {
                    console.log('Not called.');
                }, error => {
                    console.error('onRejected function called: ' + error.message);
                });
                
            </script>

        </body>

    </html>
</apex:page>

結果