Trace links mentioned in catalog variable

Puneet Hegde1
Tera Guru

Hi All,
Can you please let me know how to meet the requirements below?
Q) there is a catalogue variable which need to trace the link in it.
variable called:  X
variable type: rich text label
content: it has 6 hyperlinks one below the other

task need to achieve: there is a check box variable check only needs to check true if all the above hyperlinks has been clicked is there any way to achieve it

Thank you,
Puneet

2 ACCEPTED SOLUTIONS

@Puneet Hegde1 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

Hi Ankur,
i have tried above method i was not able to capture the clicks I have added the code below for reference can you help me out where exactly I'm making mistake on the script.
Client control:

api.controller = function($scope, $timeout) {
var c = this;

// Ensure the form is fully loaded before accessing g_form
$timeout(function() {
if ($scope.page && $scope.page.g_form) {
console.log(" g_form is available!");

// Initialize click_counter if it's not already set
let currentValue = $scope.page.g_form.getValue('click_counter') || '0';
console.log("Initial Click Counter:", currentValue);

if (parseInt(currentValue) === 0) {
$scope.page.g_form.setValue('click_counter', '0');
}
} else {
console.error("g_form is NOT available inside the widget!");
}
}, 500); // Wait 500ms to ensure g_form is ready

// Function to track link clicks
c.trackClick = function(index) {
if ($scope.page && $scope.page.g_form) {
let clickedLinks = JSON.parse(sessionStorage.getItem('clickedLinks')) || [];

if (!clickedLinks.includes(index)) {
clickedLinks.push(index);
sessionStorage.setItem('clickedLinks', JSON.stringify(clickedLinks));
}

// Calculate updated count
let updatedCount = clickedLinks.length.toString();

// Ensure the update happens immediately
$scope.$applyAsync(function() {
$scope.page.g_form.setValue('click_counter', updatedCount);
});

console.log(" Updated Click Counter:", updatedCount);
} else {
console.error(" g_form is undefined - Unable to update click_counter");
}
};
};


Thank you in advance

View solution in original post

6 REPLIES 6

@Puneet Hegde1 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,
i have tried above method i was not able to capture the clicks I have added the code below for reference can you help me out where exactly I'm making mistake on the script.
Client control:

api.controller = function($scope, $timeout) {
var c = this;

// Ensure the form is fully loaded before accessing g_form
$timeout(function() {
if ($scope.page && $scope.page.g_form) {
console.log(" g_form is available!");

// Initialize click_counter if it's not already set
let currentValue = $scope.page.g_form.getValue('click_counter') || '0';
console.log("Initial Click Counter:", currentValue);

if (parseInt(currentValue) === 0) {
$scope.page.g_form.setValue('click_counter', '0');
}
} else {
console.error("g_form is NOT available inside the widget!");
}
}, 500); // Wait 500ms to ensure g_form is ready

// Function to track link clicks
c.trackClick = function(index) {
if ($scope.page && $scope.page.g_form) {
let clickedLinks = JSON.parse(sessionStorage.getItem('clickedLinks')) || [];

if (!clickedLinks.includes(index)) {
clickedLinks.push(index);
sessionStorage.setItem('clickedLinks', JSON.stringify(clickedLinks));
}

// Calculate updated count
let updatedCount = clickedLinks.length.toString();

// Ensure the update happens immediately
$scope.$applyAsync(function() {
$scope.page.g_form.setValue('click_counter', updatedCount);
});

console.log(" Updated Click Counter:", updatedCount);
} else {
console.error(" g_form is undefined - Unable to update click_counter");
}
};
};


Thank you in advance