There is a way but its WAY more difficult to do than necessary if SC just allowed a variable in the SC_Macro.
What I did as a workaround is create 10 grid fields labelled 1-10, 10 being the maximum columns I can possibly have.
I then do a SQL lookup on the onApplicationInit event to give me a comma seperated list of the items I would like to use as column names.
I also do a count of the number of results of the SQL lookup.
You cannot run the column name update (sc_label({8}) = ‘something’) or the hide column (sc_field_display(8, off) functions in any loop or while function.
I therefore first wrote 10 if statements based on the count of the items to dynamically hide excess columns with a if function. If there are 3 items, the IF statement is hard-coded to hide columns 4-10.
Then I use explode function to get each column name into an array and then call the array value again in 10 seperate if statements to give each column its unique name.
Again, lots of work and not practical if you have an almost endless list of possibilities but worked in my scenario.
Sorry the code is messy and not cleaned up but if you searched as long as I did, it may be worth trying to follow to find a solution.
$updateloop = "SELECT * FROM B_Lease
";
sc_select(listofleases, $updateloop);
$numberoflease = {listofleases}->_numOfRows;
//echo $numberofcosts;
sc_lookup(listofleases2, $updateloop);
$key = ‘’;
$costs = ‘’;
$countofleases = ‘0’;
while($countofleases != $numberoflease){
$ID = {listofleases2[$countofleases][0]};
//echo $ID;
//echo '<br>';
$key = $key.','.$ID;
$countofleases = $countofleases + 1;
}
$key = substr($key, 1);
//echo $key;
//echo ‘
’;
$costloop = “SELECT DISTINCT (Costtype) FROM B_Lease_Cost
WHERE FIND_IN_SET(Linkfield
, ‘$key’)”;
sc_select(listofcosts, $costloop);
$numberofcosts = {listofcosts}->_numOfRows;
[nocosts] = $numberofcosts;
//echo $numberofcosts;
//echo ‘
’;
sc_lookup(listofcosts2, $costloop);
$costcount = ‘0’;
while ($costcount != $numberofcosts){
$Costname = {listofcosts2[$costcount][0]};
$costs = $costs.','.$Costname;
sc_label({1}) = '$costs';
$costcount = $costcount +1;
}
$costs = substr($costs, 1);
[listofcosts] = $costs;
echo $costs;
echo ‘
’;
//sc_field_display({1}, on);
//sc_label({1}) = ‘Turnover Rent %’;
$numberofcosts = [nocosts];
$numberofcostsdelete = $numberofcosts + 1;
echo $numberofcostsdelete;
while ($numberofcostsdelete < 11){
echo '<br>';
echo $numberofcostsdelete;
echo '<br>';
$numberofcostsdelete = $numberofcostsdelete+1;
}
if ($numberofcosts == 0){
sc_field_display(1, off)
sc_field_display(2, off)
sc_field_display(3, off)
sc_field_display(4, off)
sc_field_display(5, off)
sc_field_display(6, off)
sc_field_display(7, off)
sc_field_display(8, off)
sc_field_display(9, off)
sc_field_display(10, off)
}
if ($numberofcosts == 1){
sc_field_display(2, off)
sc_field_display(3, off)
sc_field_display(4, off)
sc_field_display(5, off)
sc_field_display(6, off)
sc_field_display(7, off)
sc_field_display(8, off)
sc_field_display(9, off)
sc_field_display(10, off)
}
if ($numberofcosts == 2){
sc_field_display(3, off)
sc_field_display(4, off)
sc_field_display(5, off)
sc_field_display(6, off)
sc_field_display(7, off)
sc_field_display(8, off)
sc_field_display(9, off)
sc_field_display(10, off)
}
if ($numberofcosts == 3){
sc_field_display(4, off)
sc_field_display(5, off)
sc_field_display(6, off)
sc_field_display(7, off)
sc_field_display(8, off)
sc_field_display(9, off)
sc_field_display(10, off)
}
if ($numberofcosts == 4){
sc_field_display(5, off)
sc_field_display(6, off)
sc_field_display(7, off)
sc_field_display(8, off)
sc_field_display(9, off)
sc_field_display(10, off)
}
if ($numberofcosts == 5){
sc_field_display(6, off)
sc_field_display(7, off)
sc_field_display(8, off)
sc_field_display(9, off)
sc_field_display(10, off)
}
if ($numberofcosts == 6){
sc_field_display(7, off)
sc_field_display(8, off)
sc_field_display(9, off)
sc_field_display(10, off)
}
if ($numberofcosts == 7){
sc_field_display(8, off)
sc_field_display(9, off)
sc_field_display(10, off)
}
if ($numberofcosts ==8){
sc_field_display(9, off)
sc_field_display(10, off)
}
if ($numberofcosts ==9){
sc_field_display(10, off)
}
$costsarray = [listofcosts];
$arr = explode(’,’, $costsarray);
$arraycount = count($arr);
//$count = 0;
//$colcount = 1;
//while ($count != $arraycount){
//sc_label({"$colcount"}) = $arr[$count];
//$colcount = $colcount+1;
// $count = $count+1;
// }
if ($arraycount == 10){
sc_label({1}) = $arr[0];
sc_label({2}) = $arr[1];
sc_label({3}) = $arr[2];
sc_label({4}) = $arr[3];
sc_label({5}) = $arr[4];
sc_label({6}) = $arr[5];
sc_label({7}) = $arr[6];
sc_label({8}) = $arr[7];
sc_label({9}) = $arr[8];
sc_label({10}) = $arr[9];
}
if ($arraycount == 9){
sc_label({1}) = $arr[0];
sc_label({2}) = $arr[1];
sc_label({3}) = $arr[2];
sc_label({4}) = $arr[3];
sc_label({5}) = $arr[4];
sc_label({6}) = $arr[5];
sc_label({7}) = $arr[6];
sc_label({8}) = $arr[7];
sc_label({9}) = $arr[8];
}
if ($arraycount == 8){
sc_label({1}) = $arr[0];
sc_label({2}) = $arr[1];
sc_label({3}) = $arr[2];
sc_label({4}) = $arr[3];
sc_label({5}) = $arr[4];
sc_label({6}) = $arr[5];
sc_label({7}) = $arr[6];
sc_label({8}) = $arr[7];
}
if ($arraycount == 7){
sc_label({1}) = $arr[0];
sc_label({2}) = $arr[1];
sc_label({3}) = $arr[2];
sc_label({4}) = $arr[3];
sc_label({5}) = $arr[4];
sc_label({6}) = $arr[5];
sc_label({7}) = $arr[6];
}
if ($arraycount == 6){
sc_label({1}) = $arr[0];
sc_label({2}) = $arr[1];
sc_label({3}) = $arr[2];
sc_label({4}) = $arr[3];
sc_label({5}) = $arr[4];
sc_label({6}) = $arr[5];
}
if ($arraycount == 5){
sc_label({1}) = $arr[0];
sc_label({2}) = $arr[1];
sc_label({3}) = $arr[2];
sc_label({4}) = $arr[3];
sc_label({5}) = $arr[4];
}
if ($arraycount == 4){
sc_label({1}) = $arr[0];
sc_label({2}) = $arr[1];
sc_label({3}) = $arr[2];
sc_label({4}) = $arr[3];
}
if ($arraycount == 3){
sc_label({1}) = $arr[0];
sc_label({2}) = $arr[1];
sc_label({3}) = $arr[2];
}
if ($arraycount ==2){
sc_label({1}) = $arr[0];
sc_label({2}) = $arr[1];
}
if ($arraycount ==1){
sc_label({1}) = $arr[0];
}
//Bring in number of costs
//Loop from 1-number , lookup in array of cost names and rename col name
//number of cost + 1 loop to x and hide each col.