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
WhiteFusion17WhiteFusion17 

apex scope if statement

Hello I have the below code but i cannot use the if statement basically when I use the if statement it no longers see the labelsIds (list variable) if I declare it in the method then it sees it as null so this does not work either.

It's all related to variable scope. 

I sure this is easy to fix but cannot work it out.
 
public static void trelloCreateNew(String ticketId, String incomingListId, String incomingLabelsID){
        listId = incomingListId;
        System.debug('Incoming Label IDs = ' +incomingLabelsID);

        //If (null == incomingLabelsID){
        List<String> labelIds = incomingLabelsID.split('; ');
        System.debug('labelids = ' +labelIds);
        //}else{
        //    list<string> labelIds = null;
        //} <--- this commented out code breaks if used

    	// Get the ticket details
    	ticket = [
                SELECT Id, Name, Trello_Card_Title__c, Trello_Card_Description__c, Trello_Card_URL__C, Trello_Card_Raised__C 
                FROM Ticketing__c 
                WHERE Id = :ticketId
        ];
        String fullticketURL = URL.getSalesforceBaseUrl().toExternalForm() +
         '/' + ticket.id;

        //Debug statements not needed in Live
    	System.debug('The ticket id = '+ticket.Id 
                     +'\n The Ticket number is '+ ticket.Name 
                     + '\n The Trello card title is '+ ticket.trello_card_title__C
                     + '\n The trello card description is ' + ticket.Trello_Card_Description__c
                     + '\n The Ticket Full URL is ' +fullticketURL);
    
    
    	endpointURL = getEnpointURL('NEWCARD');

        String requestbody;
        System.debug('labelids='+labelIds);
        if (labelIds.size() >= 1) {  <- USED HERE
            requestBody = '{"desc":'
            + Json.serialize(ticket.Trello_Card_Description__c)
            + ',"idLabels":' 
            +  Json.serialize(labelIds)             
            +'}';    
        } else {
            requestBody = '{"desc":'
            + Json.serialize(ticket.Trello_Card_Description__c)       
            +'}';    
        }

Cheers in advance for the help as always !!!
David Zhu 🔥David Zhu 🔥
Can you try 
 
        list<string> labelIds;
        If (String.isNotEmpty(incomingLabelsID)){
            LabelIds = incomingLabelsID.split('; ');
            System.debug('labelids = ' +labelIds);
        }