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
SFDC-NOOBSFDC-NOOB 

Conditional Color Change + Debug Log

Hello,

 

I am attempting to change the color of names in a list based on whether the ID in the field "Team__c" on the carrier object matches any ID in the list of users. My controller is returning the color one time and this color applies to all records (See debug log). All values in the list are blue, some should be red. Any help is greatly appreciated, thank you in advance.

 

 

Visualforce:::

 

<apex:pageblocktable value="{!carriers}" var="c">

    <apex:column headervalue="Carrier">

        <font color="{!color2}">

            <apex:outputtext value="{!c.name}"/>

        </font>

    </apex:column>

 

 

 

Controller:::

 

public string getcolor2() {

    list<carrier__c> carriers = [select team__c from carrier__c where team__c != NULL];

 

    list<user> users= new list<user>();

        users = [select id from user where userrole.name = 'Executive'];

            set<string> uid = new set<string>();

            for(user us: users){

                uid.add(us.id);

            }

 

    string color = 'red';

 

    for(carrier__c car: carriers){

        system.debug('*****List of carriers: ' + carriers);

        system.debug('*****List of users: ' + uid);

        system.debug('*****Current carrier= '+car);

        if(uid.contains(car.team__c) ){

            color='blue';

            system.debug('***** Set color to:'+color);  

            }

    }

    system.debug('***** Returning color: ' + color);

    return color;

}

 

 

Debug Log::::

 *****List of carriers: (Carrier__c:{Team__c=005U0000001D3E5IAK,      Id=a0HJ0000003bl8nMAA}, Carrier__c:{Team__c=005J0000001EEIHIA4, Id=a0HJ0000003bitnMAA}, Carrier__c:{Team__c=005U0000001BHRKIA4, Id=a0HJ0000003eD64MAE})

 

 *****List of users: {005U0000001D3E5IAK}

 *****Current carrier= Carrier__c:{Team__c=005U0000001D3E5IAK, Id=a0HJ0000003bl8nMAA}

 ***** Set color to:blue

 

*****List of users: {005U0000001D3E5IAK}

*****Current carrier= Carrier__c:{Team__c=005J0000001EEIHIA4, Id=a0HJ0000003bitnMAA}

 

*****List of users: {005U0000001D3E5IAK}

*****Current carrier= Carrier__c:{Team__c=005U0000001BHRKIA4, Id=a0HJ0000003eD64MAE}

 

***** Returning color: blue

Best Answer chosen by Admin (Salesforce Developers) 
digamber.prasaddigamber.prasad

Hi, 

 

I will suggest you to wrap Carrier records in a wrapper class and have one additional field say 'Color'. For every record you can have some default value like 'BLUE' and if matched criteria you can set value of 'Color' for that record to say 'RED'.

 

Let me know if you need more help on this.

 

Happy to help you!

 

Records,

Digamber Prasad

All Answers

digamber.prasaddigamber.prasad

Hi, 

 

I will suggest you to wrap Carrier records in a wrapper class and have one additional field say 'Color'. For every record you can have some default value like 'BLUE' and if matched criteria you can set value of 'Color' for that record to say 'RED'.

 

Let me know if you need more help on this.

 

Happy to help you!

 

Records,

Digamber Prasad

This was selected as the best answer
SFDC-NOOBSFDC-NOOB

Thank you for your reply!!  I was able to use a wrapper class to solve the problem.  It broke my column sorting though, I guess it's time to learn the comparable interface! 

 

My functioning code:

 

********************************************Controller***********************************************************

 

public class CarrierList_Controller {
    public class MyCarrier {
        carrier__c c;
        public MyCarrier(carrier__c car) {c = car;}
        public Carrier__c getCarrier(){ return c; }
       
        public String getColor(){
            list<user> users= new list<user>();
            users = [select id from user where userrole.name = 'Executive' and user.alias = 'jberg'];
                set<string> uid = new set<string>();
                for(user us: users)
                {
                    uid.add(us.id);
                }
                if(uid.contains(c.Team__c)) return 'Blue';
                else return 'red';
        }
    }
           
            public List<MyCarrier> results = new List<MyCarrier>{};
            public list<MyCarrier> getCarriers(){
            results.clear();
                for(carrier__c c : [select id, team__c, name from carrier__c where team__c != NULL]){
                    results.add(new myCarrier(c));
                }
               
            return results;
            }
}

 

**************************************************Visualforce Page****************************************************************

 

<apex:page controller="CarrierList_Controller"  
showHeader="true" sidebar="true" > 
    <apex:form >   
        <apex:pageBlock title="" id="pageBlock"> 
            <apex:pageMessages ></apex:pageMessages> 
            <apex:pageBlockTable value="{!carriers}" var="c"  
rendered="{!NOT(ISNULL(carriers))}"> 
                <apex:column > 
                    <apex:facet name="header">Carrier Name</apex:facet> 
                    <div style="color:{!c.color};font-weight:bold;text-decoration:underline;cursor:hand;cursor:pointer;"> 
                    {!c.carrier.Name} 
                    </div>  
                </apex:column> 
                <apex:column value="{!c.carrier.Team__c}"></apex:column>  
            </apex:pageBlockTable> 
        </apex:pageBlock> 
    </apex:form> 
</apex:page>

 

 

digamber.prasaddigamber.prasad

Hi,

 

Happy that it worked for you! :)

 

Will you please let me know what do you exactly mean by 'broke my column sorting'. If you explain it to me, probably I will be able to comment on this.

 

Happy to help you!

 

Regards,

Digamber Prasad

SFDC-NOOBSFDC-NOOB

Ok so I would like my users to be able to click the column names to sort the columns by ascending or descending order.  My code is as follows:

 

 

*******************************************************Controller******************************************************

 

public class carrierlist {

private String sortDirection = 'ASC';

private String sortF = 'name';

public String sortField { 

get

    {

       return sortF;

    }

    set

    {    

//if the column is clicked on then switch between Ascending and Descending modes

        if (value == sortF)

            sortDirection = (sortDirection == 'ASC')? 'DESC' : 'ASC';

        else

            sortDirection = 'ASC';

        sortF = value;

   }

}

 

public String getSortDirection() {

    //if no column is selected

    if (sortField  == null || sortField == '')

        return 'ASC';

    else

        return sortDirection;

}

 

   public void setSortDirection(String value) {

      sortDirection = value;

}

 

   public class MyCarrier {

    carrier__c c;

    public MyCarrier(carrier__c car) {c = car;}

    public Carrier__c getCarrier(){ return c; }

           

public String getColor(){

        list<user> users= new list<user>();

        users = [select id from user where userrole.name = 'Executive' and user.alias = 'jberg'];

            set<string> uid = new set<string>();

            for(user us: users)

            {

                uid.add(us.id);

            }

 

            if(uid.contains(c.Team__c))

                return 'red';

            else

                return 'blue';

    }

}

     public List<MyCarrier> carriers = new List<MyCarrier>{}; public list<MyCarrier> getCarriers(){

        string sortFullExp = sortfield  + ' ' + sortDirection;

        carriers.clear();

            for(carrier__c c : [select id, team__c, references__c, requester__c, fraud_websites__c, Insurance_received__c, conditional_letter__c,

                contract__c, MC_Number__c, compliance_action__c, attachment__c, name from carrier__c where team__c != NULL]){

 

            carriers.add(new myCarrier(c));

        }

        return carriers;      /**********************What I need to return ------>>

       //carrier = Database.query('select team__c, attachment__c, MC_number__c, contract__c, conditional_letter__c, references__c, insurance_received__c, fraud_websites__c, requester__c, compliance_action__c, Name from carrier__c where team__c != NULL ORDER BY ' + sortFullExp + ' limit 1000');

       return carrier;*****************************************/

}

}

 

 

****************************************************Visualforce Page************************************************************************************

 

 

 <apex:page controller="carrierlist" showHeader="true" sidebar="true" > 
    <apex:form >   
        <apex:pageBlock title="" id="pageBlock"> 
            <apex:pageBlockTable value="{!carriers}" var="c" rendered="{!NOT(ISNULL(carriers))}"> 
                <apex:column > 
                    <apex:facet name="header">Carrier Name</apex:facet> 
                    <div style="color:{!c.color};font-weight:bold;text-decoration:underline;cursor:hand;cursor:pointer;"> 
                    <apex:outputlink style="color:{!c.color}" value="/{!c.carrier.id}" target="_blank" > {!c.carrier.name} </apex:outputlink>
                    </div>  
                </apex:column> 
                <apex:column value="{!c.carrier.References__c}">
    <apex:facet name="header">
        <apex:commandlink value="References{!IF(sortField =='References__c',IF(sortDirection='ASC','▼','▲'),'')}">
            <apex:param value="References__c" name="column" assignto="{!sortField }" ></apex:param>
        </apex:commandlink>
    </apex:facet>
</apex:column>

<apex:column value="{!c.carrier.Compliance_Action__c}">
    <apex:facet name="header">
        <apex:commandlink value="Compliance Action{!IF(sortField =='compliance_action__c', IF(sortDirection='ASC','▼','▲'),'')}">
            <apex:param value="compliance_action__c" name="column" assignto="{!sortField}" ></apex:param>
        </apex:commandlink>
    </apex:facet>
</apex:column>
<apex:column value="{!c.carrier.MC_Number__c}">
    <apex:facet name="header">
        <apex:commandlink value="MC Number{!IF(sortField =='MC_Number__c', IF(sortDirection='ASC','▼','▲'),'')}">
            <apex:param value="MC_Number__c" name="column" assignto="{!sortField}" ></apex:param>
        </apex:commandlink>
    </apex:facet>
</apex:column>

 

 

digamber.prasaddigamber.prasad

Hi,

 

Did you put debug log and check how query build on run time based upon clicking on column name?

 

Regards,

Digamber Prasad

SFDC-NOOBSFDC-NOOB
The problem is I have to iterate through every record and add it to "carriers.add(new myCarrier(c));"

Therefore, I cannot use the database.query(' select . . . from carrier__c Order by ' + SortFullExp);

When I use the query I need, I get this error:

carrierlist Compile Error: line breaks not allowed in string literals at line 61 column -1


Code generating error:::

for(carrier__c c : database.query('select id, team__c, references__c, requester__c, fraud_websites__c, Insurance_received__c, conditional_letter__c, contract__c, MC_Number__c, compliance_action__c, attachment__c, name from carrier__c where team__c != NULL order by ' + sortFullExp ){
carriers.add(new myCarrier(c));
}
digamber.prasaddigamber.prasad

Hi,

 

I found there was a ')' missing in for statement. Could you please try below code snippet:-

 

for(carrier__c c : database.query('select id, team__c, references__c, requester__c, fraud_websites__c, Insurance_received__c, conditional_letter__c, contract__c, MC_Number__c, compliance_action__c, attachment__c, name from carrier__c where team__c != NULL order by ' + sortFullExp )){
	
	carriers.add(new myCarrier(c));
	
}

 

Let me know if it doesn't work for you.

 

Regards,

Digamber Prasad

 

digamber.prasaddigamber.prasad

Just a small change:--

 

for(carrier__c c : database.query('select id, team__c, references__c, requester__c, fraud_websites__c, Insurance_received__c, conditional_letter__c, contract__c, MC_Number__c, compliance_action__c, attachment__c, name from carrier__c where team__c != NULL order by ' + sortFullExp )){
	
	carriers.add(c);
	
}

 

digamber.prasaddigamber.prasad

Below could be one more possible solution:-

 

for(SObject c : database.query('select id, team__c, references__c, requester__c, fraud_websites__c, Insurance_received__c, conditional_letter__c, contract__c, MC_Number__c, compliance_action__c, attachment__c, name from carrier__c where team__c != NULL order by ' + sortFullExp )){
	
	carriers.add((carrier__c)c);
	
}

 

SFDC-NOOBSFDC-NOOB
It works! The first one you posted is the solution! I can't believe I missed a ')'

Thanks so much!! KUDOS TO YOU!
digamber.prasaddigamber.prasad

Happy that I was able to help you! :)

 

Happy coding.

 

Regards,

Digamber Prasad