The CreatorCon Call for Content is officially open! Get started here.

Unable to fetch domain name

Chandrashekar_N
Tera Contributor

I have a requirement. in catalogue form If a user requests for a domain name which is available in 'NameStudio® API: A Smart API Solution That Delivers Relevant Domain Name Suggestions (namestudioapi....' it has to be populated in the catalogue variable field if not available in else it should populate error message. 

I have created a catalogue and created a script include and catalogue client script below.

But it is not working please suggest me the right approach.

Thanks in advance.

 

Script include:

var DomainAvailabilityCheck = Class.create();
DomainAvailabilityCheck.prototype = {
    initialize: function() {},
 
    checkAvailability: function(domainName) {
        try {
            var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://namestudioapi.com/' + domainName);
            request.setHttpMethod('GET');
           
            var response = request.execute();
            var responseBody = response.getBody();
            var responseObj = JSON.parse(responseBody);
           
            return responseObj.available;
        } catch (ex) {
            gs.error('Error in DomainAvailabilityCheck Script Include: ' + ex.message);
            return false;
        }
    },
 
    type: 'DomainAvailabilityCheck'
};
 

Client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
 
    // Make a GlideAjax call to the Script Include
    var ga = new GlideAjax('DomainAvailabilityCheck');
    ga.addParam('sysparm_name', 'checkAvailability');
    ga.addParam('domainName', newValue);
    ga.getXMLAnswer(function(response) {
        var isAvailable = response.responseXML.documentElement.getAttribute("answer") == 'true';
        var message = isAvailable ? 'Domain is available' : 'Domain is not available';
       
        // Display the message to the user
        g_form.showFieldMsg('domain_avaliablity', message, 'info');
    });
}
2 ACCEPTED SOLUTIONS

Sandeep Rajput
Tera Patron
Tera Patron

@Chandrashekar_N Are you sure if you are calling the right end point. I checked https://namestudioapi.com/' and found that it has a suggest API hosted at this URL https://sugapi.verisign-grs.com/ns-api/2.0/suggest?name=google here google is the actual domain name to check.

 

Here is the response sample.

results": [
{
"name": "ShGoogle.com",
"availability": "available"
}

According to this you need to update your scripts as follows.

 

Script Include:

var DomainAvailabilityCheck = Class.create();
DomainAvailabilityCheck.prototype = {
    initialize: function() {},
 
    checkAvailability: function(domainName) {
        try {
           var domainName = this.getParameter('sysparm_domainName');
            var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://sugapi.verisign-grs.com/ns-api/2.0/suggest?name=' + domainName);
            request.setHttpMethod('GET');
           
            var response = request.execute();
            var responseBody = response.getBody();
            var responseObj = JSON.parse(responseBody);
           
            return responseObj.results[0].availability; //property name is availability also response returns a result array
        } catch (ex) {
            gs.error('Error in DomainAvailabilityCheck Script Include: ' + ex.message);
            return false;
        }
    },
 
    type: 'DomainAvailabilityCheck'
};

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
 
    // Make a GlideAjax call to the Script Include
    var ga = new GlideAjax('DomainAvailabilityCheck');
    ga.addParam('sysparm_name', 'checkAvailability');
    ga.addParam('sysparm_domainName', newValue);
    ga.getXMLAnswer(function(response) {
        var isAvailable = response.responseXML.documentElement.getAttribute("answer") == 'available';
        var message = isAvailable ? 'Domain is available' : 'Domain is not available';
       
        // Display the message to the user
        g_form.showFieldMsg('domain_avaliablity', message, 'info');
    });
}

Hope this helps.

 

View solution in original post

@Chandrashekar_N Responded to you on the message. Please check your community inbox.

View solution in original post

5 REPLIES 5

Sandeep Rajput
Tera Patron
Tera Patron

@Chandrashekar_N Are you sure if you are calling the right end point. I checked https://namestudioapi.com/' and found that it has a suggest API hosted at this URL https://sugapi.verisign-grs.com/ns-api/2.0/suggest?name=google here google is the actual domain name to check.

 

Here is the response sample.

results": [
{
"name": "ShGoogle.com",
"availability": "available"
}

According to this you need to update your scripts as follows.

 

Script Include:

var DomainAvailabilityCheck = Class.create();
DomainAvailabilityCheck.prototype = {
    initialize: function() {},
 
    checkAvailability: function(domainName) {
        try {
           var domainName = this.getParameter('sysparm_domainName');
            var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://sugapi.verisign-grs.com/ns-api/2.0/suggest?name=' + domainName);
            request.setHttpMethod('GET');
           
            var response = request.execute();
            var responseBody = response.getBody();
            var responseObj = JSON.parse(responseBody);
           
            return responseObj.results[0].availability; //property name is availability also response returns a result array
        } catch (ex) {
            gs.error('Error in DomainAvailabilityCheck Script Include: ' + ex.message);
            return false;
        }
    },
 
    type: 'DomainAvailabilityCheck'
};

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
 
    // Make a GlideAjax call to the Script Include
    var ga = new GlideAjax('DomainAvailabilityCheck');
    ga.addParam('sysparm_name', 'checkAvailability');
    ga.addParam('sysparm_domainName', newValue);
    ga.getXMLAnswer(function(response) {
        var isAvailable = response.responseXML.documentElement.getAttribute("answer") == 'available';
        var message = isAvailable ? 'Domain is available' : 'Domain is not available';
       
        // Display the message to the user
        g_form.showFieldMsg('domain_avaliablity', message, 'info');
    });
}

Hope this helps.

 

Chandrashekar_N
Tera Contributor

Thanks for your input. I have tried your suggestion but it did not worked. please let me know what I have to do to achieve functionality. 

@Chandrashekar_N Responded to you on the message. Please check your community inbox.

Chandrashekar_N
Tera Contributor

Your solution really worked of my requirment.

Thankyou so much for your valuable feedback.