Search code examples
javascriptjavaapexapex-codeconditional-operator

Understanding java/apex complex ternary operator


This is apex code, but for the propose seems to work like in Java or JScript.

I've the following line of code (comment included):

// If GPMIR_fld_codigoOrigen__c 09 Fotofactura will not be assigned to agency for send csv

eLead.GPMIR_fld_assignAgency__c = (eLead.GPMIR_fld_codigoOrigen__c!='09') ? ((u.Contact != null && u.Contact.Account != null && rt == 'Agencia') ? u.Contact.Account.Id : null) : null;

Trying to translate that to regular if-else what i think it's happening here is (comment included):

// If GPMIR_fld_codigoOrigen__c != 09 then GPMIR_fld_assignAgency__c == null

if(eLead.GPMIR_fld_codigoOrigen__c!='09'){
    if(u.Contact != null && u.Contact.Account != null && rt == 'Agencia'){
        eLead.GPMIR_fld_assignAgency__c = u.Contact.Account.Id;
    }else{
        eLead.GPMIR_fld_assignAgency__c = null;
    }
}else{
    eLead.GPMIR_fld_assignAgency__c = null;
}

Probably i'm wrong but if anyone could help me to do the proper translation i would really appreciate it


Solution

  • Seems correct, but You can also do this inside a function

    Public string MyFunction(){
     if(eLead.GPMIR_fld_codigoOrigen__c!='09'){
        if(u.Contact != null && u.Contact.Account != null && rt == 'Agencia'){
            Return u.Contact.Account.Id;
        }
    }
    Return null;
    }```
    //Then in main(), eLead.GPMIR_fld_assignAgency__c = MyFunction()
    

    Hopefully I'm right as well.