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
David Roberts 4David Roberts 4 

Assign an Account to a custom object's field that is an account lookup.

I'm having great difficulty assigning and testing account ids that I've created in a custom object.

I want to compare accounts
if (thisAc == thatAc){
} //endif

and tried
if (thisAc.id == thatAc.id){
​} //endif

My custom object has an account field: MyCustomObject__c has referenceCompany__c
I can't seem to copy it to another custom object - even a temporary Account

starting from MyCustomObject my1...

MyOtherCustomObject myOco = new(MyOtherCustomObject);
myOco.copiedCompany__c = my1.referenceCompany__c;

I've tried:
Account thisAc = .my1.referenceCompany__c;
and
Account thatAc= .my1.referenceCompany__r; (which seems close to the answer).


Any suggestions?

Thanks in advance.


 
Nayana KNayana K
Can you please post whole code?  
sandeep reddy 37sandeep reddy 37
send your code your probleme is not understanding 
David Roberts 4David Roberts 4
OK but it's full of different attempts at getting hold of the ID.
I had a thought thsat I'd try strings next...
Here's the controller class. I'll post the controller test and object class following.

public class RetailerLicencesController {
    // Called by apex page:
    // ...salesforce.com/apex/retailerslicences
    
    private List<RetailersLicences> myRetLicList; // = new List<RetailersLicences>();
    
    private string name;
    public string searchText {get;set;}
    
    private String qp;
       
    
     //constructor
    public RetailerLicencesController() {
        this.qp = ApexPages.currentPage().getParameters().get('qp');
        searchText = '';
    }

    public List<Licence__c> gettheirlicences(id co){
        
        system.debug('Get the licences of '+co);
        //retailer_of__c retof = new retailer_of__c();
        //retof.retailer__c = '0018E000008IK3N'; //BMF //retof.retailer__c
        Licence__c[] theirlicenceList = [Select ID,name,active__c,licensed_until__c,maintenance_expires__c from Licence__c where company__c = :co order by name ];
        return theirlicenceList;       
    }
 

    public List<RetailersLicences> getmyRetLicList() {
        
        
        
        
        //String err = 'Error. Click <a href="' + Page.MyCustomPage.getURL() + '">here</a>.';
        String err = 'Hello';
        ApexPages.Message msgErr;
//msgErr = new ApexPages.Message(ApexPages.Severity.ERROR, err);
//ApexPages.addmessage(msgErr);

 
        
        System.debug('searchText='+searchText);
        if (myRetLicList<>NULL){
            System.debug('on entry to get, myRetLicList size='+myRetLicList.size());
        }
        else{System.debug('ret lic list is empty');}
        //System.assertEquals('Ideal',searchText,'string fnd='+searchText);
        //temp fix
        /*
        if(searchText == null || searchText == '') {
        
            searchText = 'Ideal';
            
        }
*/
        
        string fnd = ''; //Ideal%';
        
        if(searchText != null && searchText != '') {
            fnd = searchText+'%';
            myRetLicList = null;
        }
        //temp fix
        if(searchText == null || searchText == '') {
            fnd = '%';
            myRetLicList = null;
        }
        
        fnd = searchText+'%';
        myRetLicList = null;
        //System.assertEquals('Ideal%',fnd,'string fnd='+fnd);
        //ApexPages.addMessage (new ApexPages.Message(ApexPages.Severity.ERROR,'fnd = '+fnd));
        System.debug('string fnd='+fnd);

        //
        //
        if(myRetLicList== null) {
            myRetLicList = new List<RetailersLicences>();
            retailer_of__c retof1 = new retailer_of__c();
            List<licence__c> theirlicences = new List<licence__c>();
            
            integer i;
            //Get list of reailersof containing the manufacturer and the retailer
            List<retailer_of__c> myRetList = new List<retailer_of__c>();
            myRetList = [SELECT Id, Name, Retailer__c, Retailer__r.name,Manufacturer__c, Manufacturer__r.name FROM Retailer_Of__c where manufacturer__r.Name like :fnd order by Retailer__r.Name];
            
            System.debug('#retailers='+myRetList.size());
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, '#retailers='+myRetList.size());
            
            err = '#retailers='+myRetList.size();
            msgErr = new ApexPages.Message(ApexPages.Severity.INFO, err);
            ApexPages.addmessage(msgErr);
            
            
            //get all licences then filter on retailer inside loop
            theirlicences = [Select ID,name,company__c,company__r.id,active__c,licensed_until__c,maintenance_expires__c from Licence__c order by name ];
            System.debug('#their licences='+theirlicences.size());
    
            Account thisret; // = company__c = :ro.retailer__c
            Account thatret; // = company__c = :ro.retailer__c
            
            
            
            /*
            Id actId = [SELECT AccountId FROM Opportunity WHERE Id=:oppId].AccountId;
        Contract_Summary__c ContSumm = new Contract_Summary__c();
            ContSumm.Related_Opportunity__c = oppId;
            ContSumm.Account_Name__c = actId;
            
            
            */
            Id actId = [SELECT company__c FROM Licence__c limit 1].company__c;
            System.debug('quick test actID from select ='+actId);
            
            
            
            if (myRetList.size()>0){
                
            
            //Loop through the list
            for (retailer_of__c ro : myRetList) {
                
                //extract the retailer
                retof1 = ro;
                //thisret.id = ro.retailer__c;
                //relatedObject.put('ParentId',objectList[i].Id);
                //String sObjName = ro.retailer__r.Id.getSObjectType().getDescribe().getName();
                actId = ro.retailer__c;
                thisret.id = (Id) actId;
                
                //loop through the licences
                for (licence__c retlic : theirlicences){
                    //thatret.id = retlic.company__c;
                    actId = retlic.company__c;
                    thatret.Id = actId;
                    System.debug('thisret='+thisret);
                    System.debug('thatret='+thatret);
                    if (thisret == thatret){
                        RetailersLicences aretlic = new RetailersLicences();
                        aretlic.setRetailer(retof1);
                        aretlic.setLicence(retlic);
                        myRetLicList.add(aretlic);
                    } //endif same retailer
                } //next i
                
                
                }//endif myRetList>0
                
                /*

                 theirlicences = [Select ID,name,active__c,licensed_until__c,maintenance_expires__c from Licence__c where company__c = :ro.retailer__c order by name ];
                System.debug('#their licences='+theirlicences.size());
    
                err = '#their licences='+theirlicences.size();
                msgErr = new ApexPages.Message(ApexPages.Severity.INFO, err);
                ApexPages.addmessage(msgErr);
                
                //extract the retailer
                retof1 = ro;
                
                //loop through the licences
                for (licence__c retlic : theirlicences){
                    RetailersLicences aretlic = new RetailersLicences();
                    aretlic.setRetailer(retof1);
                    aretlic.setLicence(retlic);
                    myRetLicList.add(aretlic);
                } //next i
              */
                
            } //next ro
            
            
/*

* // Loop through the list and update the Name field
for(Account a : accs){
   a.Name = 'Oracle';
}
// Update the database
update accs;

             * 
             * 
             * */
            
            /*
            
                   
            //for each retailer, find the licences they have
            
            for(j = 0; j < myRetList.size(); j++){
                //now look up the licence details
                Licence__c[] theirlicence = [Select ID,name,active__c,licensed_until__c,maintenance_expires__c from Licence__c where company__c = :myRetList[j].retailer__c order by name ];
                
                //extract the retailer
                retof1 = myRetList[j];
                
                //loop through the licences
                for (i=0;i<theirlicence.size();i=i+1){
                    RetailersLicences aretlic = new RetailersLicences();
                    aretlic.setRetailer(retof1);
                    aretlic.setLicence(theirlicence[i]);
                    myRetLicList.add(aretlic);
                } //next i

            }//next j
           

            
            */
         } //endif myRetLicList== null
         return myRetLicList;
    }

//next is holder....    
/*
    public List<RetailersLicences> getmyRetLicList1() {
        System.debug('searchText='+searchText);
        if (myRetLicList<>NULL){
            System.debug('myRetLicList size='+myRetLicList.size());
        }
        else{System.debug('ret lic list is empty');}
        //System.assertEquals('Ideal',searchText,'string fnd='+searchText);
        //temp fix
       
        if(searchText == null || searchText == '') {
        
            searchText = 'Ideal';
            
        }

        
        string fnd = ''; //Ideal%';
        
        if(searchText != null && searchText != '') {
            fnd = searchText+'%';
            myRetLicList = null;
        }
        //temp fix
        if(searchText == null || searchText == '') {
            fnd = '%';
            myRetLicList = null;
        }
        
        fnd = searchText+'%';
        myRetLicList = null;
        //System.assertEquals('Ideal%',fnd,'string fnd='+fnd);
        //ApexPages.addMessage (new ApexPages.Message(ApexPages.Severity.ERROR,'fnd = '+fnd));
        System.debug('string fnd='+fnd);

        //
        //
        if(myRetLicList== null) {
            List <Retailer_Of__c> lstRetailerOf = [select id, retailer__c, manufacturer__c from Retailer_of__c where manufacturer__r.name like :fnd];
            
            if (lstRetailerOf.size()>0) {
                System.debug('myRetLicList is Null so create it...');
                myRetLicList = new List<RetailersLicences>();
                Retailer_Of__c tempRetOf = new Retailer_Of__c();
                Licence__c tempLicence = new licence__c();
                //Account tempmanufacturer = new Account();
                //tempmanufacturer.AccountNumer = '0012400000Oc2KO';
                List <licence__c> lstLicencesWithRetailers = [select id,name , company__c, company__r.name, active__c,licensed_until__c,maintenance_expires__c from licence__c where company__c in (select retailer__c  from retailer_of__c where manufacturer__r.name like :fnd) order by company__r.name];
                
                //transfer to myRetLicList
                integer i,j;           
                for(j = 0; j < lstLicencesWithRetailers.size(); j++){
             
                       RetailersLicences aretlic = new RetailersLicences();
                    
                    tempRetOf.retailer__c = lstLicencesWithRetailers[j].company__c;
                    //gotta get the manufacturer
                    //search through lstRetailerOf for retailer to give manufacturer
                    //for test, use first manufacturer...
                    tempRetOf.manufacturer__c = lstRetailerOf[0].manufacturer__c;
                    aretlic.setRetailer(tempRetOf);
                    
                    tempLicence = lstLicencesWithRetailers[j];
                    aretlic.setLicence(tempLicence);
                    myRetLicList.add(aretlic);
                       
    
         
                }//next j
            } //endif found retailers of
            
         } //endif myRetLicList== null
         return myRetLicList;
    } //getmyRetLicList1
*/    
 /*   
    //what is this all about.....?
    // Inner class to hold details for item
    public class DisplayRetailersLicences{
    
        private RetailersLicences myRetLic;
        
        //constructor
        public DisplayRetailersLicences(RetailersLicences item) {
            this.myRetLic = item;
        }
            
        // Properties for use in the Visualforce view
        //public String retailer {
        //    get { return myRetLic.getRetailer(); }
        //}
        //public String licence {
        //    get { return myRetLic.getLicence().name; }
        //}
        
            
    
    }//inner Class DisplayRetailersLicences  
    
*/



}


/*
 *  Called by apex page:
 * ...salesforce.com/apex/retailerslicences
 * 
 * 

<apex:page controller="RetailerLicencesController">
<apex:pageMessages />

<br/>
<apex:form >
<apex:actionFunction name="af_reRenderSearchBlock" reRender="searchBlock"/>


<apex:pageBlock title="Retailers and their licences:" id="searchBlock" >

        <apex:pageBlockButtons location="top">
        <h1>Retailer Of:</h1>
            <input type="button" class="btn" value="Search" onclick="af_reRenderSearchBlock()"/>
        </apex:pageBlockButtons>
        
         <apex:pageBlockSection collapsible="false" columns="1">
            <apex:pageBlockSectionItem helpText="Retailers of manufacturer...use % as wildcard">
                <apex:outputLabel for="searchParam">Search String</apex:outputLabel>
                <apex:inputText id="searchParam" value="{!searchText}"/>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>       

 
        
  
<!-- <apex:pageBlock title="Retailers and their licences:"> -->


<!-- use the retailerof query result object from the controller class -->
<apex:pageBlockSection columns="1" title="Results" >
<apex:pageBlockTable value="{!myRetLicList}" var="ritem">
 
<!-- -->
<apex:column headerValue="Retailer">
    <apex:outputText value="{!ritem.retailer.retailer__r.name}"/>
</apex:column>
<!-- -->

<apex:column headerValue="Licence">
    <apex:outputText value="{!ritem.licence.name}"/>
</apex:column>
<apex:column headerValue="Active">
    <apex:outputText value="{!ritem.licence.Active__c}"/>
</apex:column>


<apex:column headerValue="Expires">
    <apex:outputText value="{!ritem.licence.Licensed_Until__c}"/>
</apex:column>

<apex:column headerValue="Maintained">
    <apex:outputText value="{!ritem.licence.Maintenance_Expires__c}"/>
</apex:column>


</apex:pageBlockTable>
 
 </apex:pageBlockSection>


</apex:pageBlock>
</apex:form>

</apex:page>
*/
David Roberts 4David Roberts 4
//RetailerLicencesControllerTest.test();
@istest(SeeAllData=true)
public class RetailerLicencesControllerTest {
    
    @istest static void test(){
        //PageReference pageRef = Page.retailerslicences;
        Test.setCurrentPage(Page.RetailersLicences);
    
        //String searchText = 'Ideal';
        RetailerLicencesController myRetLicListController = new RetailerLicencesController();
        //next call is to dummy function. unused
        //PageReference refPage = myRetLicListController.save();
        //System.debug('refPage='+refPage);
        //String nextPage = myRetLicListController.save().getUrl();
        // Verify that page fails without parameters
        //System.assertEquals('/apex/failure?error=noParam', nextPage);
        Account theRetailer = [SELECT Id, Name FROM Account WHERE Name like '%' limit 1];
        //myRetLicListController.gettheirlicences(theRetailer);
    
    
        System.debug('set search to ideal');
        myRetLicListController.searchText = 'Ideal'; //'Ideal';
        
        string testReturn = myRetLicListController.searchText;
        System.debug('testreturn='+testReturn);
        
         //myRetLicList = new List<RetailersLicences>();
        List<RetailersLicences> rll = new List<RetailersLicences>();
        
        
        System.debug('set search to blank');
        myRetLicListController.searchText = '';
        rll = myRetLicListController.getmyRetLicList();
        
        //temp remove
        //myRetLicListController.searchText = 'Ideal%';
        //rll = myRetLicListController.getmyRetLicList();
        //System.assertNotEquals(null,rll,'Expected a list.');
        
        //example
        //relatedObject.put('ParentId',objectList[i].Id);
        

        string item = 'dummy content';
        RetailersLicences myRetLic = new RetailersLicences();
        //is this function really required?
        
        
        //duplicate test
        theRetailer = [SELECT Id, Name FROM Account WHERE Name like '%' limit 1];
        myRetLicListController.gettheirlicences(theRetailer);

        
    }//test
    
    

}


 
David Roberts 4David Roberts 4
public class RetailersLicences {
    private Retailer_Of__c retof;
    private Licence__c lic;
    
    //constructors
    public RetailersLicences(){
        retof = new Retailer_Of__c();
        lic = new Licence__c();
    }
    
   
    public RetailersLicences(Retailer_Of__c aretof,Licence__c alic) {
        retof = aretof;
        lic = alic;
    }
    //end constructors
    
    public Licence__c getLicence(){
        return lic;
    }
    
    public Id getLicenceID(){
        
        //system.debug('in getLicenceID='+lic.ID);
        return lic.Id;
    }
    
    public String getLicenceNumber(){
        
        //system.debug('in getLicenceNumber='+lic.Name);
        return lic.Name;
    }
    
    
    public string getRetailerID(){
        return retof.Id;
    }
    
    public Retailer_Of__c getRetailer(){
        return retof;
    }
    
    public string getRetailerString(){
        return retof.Retailer__c;
    }
    
    public string getRetailerName(){
        system.debug('in getRetailerName the retof ='+retof);
        system.debug('in getRetailerName the retailer__c ='+retof.Retailer__c);
        system.debug('in getRetailerName the retailer__r.name ='+retof.Retailer__r.Name);
        //system.debug('in getRetailerName the retailer__r.get ='+retof.Retailer__r.get('Name'));
        //String sObjName = retof.Retailer__c.getSObjectType().getDescribe().getName(); //returns 'Account'
        
        //can't find how to get name from retof directly so run a query...
        //e.g. SELECT Name FROM Account WHERE id = '0018E000008IK3NQAW'
        List<Account> lstAccount = new List<Account>();
        String AccntName;
        Id retID = retof.Retailer__c;
        System.debug('retID = '+retID) ; 
        lstAccount = [SELECT Id, Name FROM Account WHERE id = :retID];
        //lstAccount = [SELECT Id, Name FROM Account WHERE id = '0018E000008IK3NQAW'];
        
        System.debug('size = '+lstAccount.Size()) ;      
        if (lstAccount.Size()>0) {
            AccntName = lstAccount[0].Name;
        }
        system.debug('Retailer Name from query = '+AccntName);

        return AccntName;
    }
    
    public void setLicence(Licence__c alic) {
        lic = alic;
    }
    
    public void setRetailer(Retailer_Of__c aretof) {
        retof = aretof;
    }
    

 

}
David Roberts 4David Roberts 4
While we're here, please could you tell me how to get the message to update on the vf page? I'm trying to get debug info on the page.

<apex:page controller="RetailerLicencesController">


    <strong>    <apex:outputPanel id="messageBlock" >
        
        <apex:pageMessage title="Message Panel" summary="This pageMessage will always display. Any validation error messages will appear in the pageMessages component." severity="warning" strength="3" />
<apex:pageMessages />
        
    </apex:outputPanel> </strong>  

    <br/>



<br/>
<apex:form >
<apex:actionFunction name="af_reRenderSearchBlock" reRender="searchBlock"/>


<apex:pageBlock title="Retailers and their licences:" id="searchBlock" >

        <apex:pageBlockButtons location="top">
        <h1>Retailer Of:</h1>
            <input type="button" class="btn" value="Search" onclick="af_reRenderSearchBlock()"/>
        </apex:pageBlockButtons>
        
         <apex:pageBlockSection collapsible="false" columns="1">
            <apex:pageBlockSectionItem helpText="Retailers of manufacturer...use % as wildcard">
                <apex:outputLabel for="searchParam">Search String</apex:outputLabel>
                <apex:inputText id="searchParam" value="{!searchText}"/>
                
               
                
                
            </apex:pageBlockSectionItem>
            
            
             
</apex:pageBlockSection>       


<!-- <apex:pageBlock title="Retailers and their licences:"> -->


<!-- use the retailerof query result object from the controller class -->
<apex:pageBlockSection columns="1" title="Results" >
<apex:pageBlockTable value="{!myRetLicList}" var="ritem">
        <apex:actionSupport event="onchange" rerender="messageBlock"/>
 
<apex:column headerValue="Manufacturer">
    <apex:outputText value="{!ritem.retailer.manufacturer__r.name}"/>
</apex:column>

<apex:column headerValue="Retailer">
    <apex:outputText value="{!ritem.retailer.retailer__r.name}"/>
</apex:column>


<apex:column headerValue="Licence">
    <apex:outputText value="{!ritem.licence.name}"/>
</apex:column>
<apex:column headerValue="Active">
    <apex:outputText value="{!ritem.licence.Active__c}"/>
</apex:column>


<apex:column headerValue="Expires">
    <apex:outputText value="{!ritem.licence.Licensed_Until__c}"/>
</apex:column>

<apex:column headerValue="Maintained">
    <apex:outputText value="{!ritem.licence.Maintenance_Expires__c}"/>
</apex:column>


</apex:pageBlockTable>
<b>rerender="messageBlock"</b>   
 
 </apex:pageBlockSection>


</apex:pageBlock>
</apex:form>

</apex:page>
David Roberts 4David Roberts 4
Got a little further using strings for the account:

holdAct1 = ro.retailer__c;
                String holdAct2;
                
                //loop through the licences
                for (licence__c retlic : theirlicences){
                    //thatret.id = retlic.company__c;
                    //actId = retlic.company__c;
                    //thatret.Id = actId;
                    //System.debug('thisret='+thisret);
                    //System.debug('thatret='+thatret);
                    holdAct2 = retlic.company__c;
                    
                    if (holdAct1 == holdAct2){
                        System.debug('holdAct1='+holdAct1+'   holdAct2='+holdAct2);
                        RetailersLicences aretlic = new RetailersLicences();
                        aretlic.setRetailer(retof1);
                        aretlic.setLicence(retlic);
                        myRetLicList.add(aretlic);
                    } //endif same retailer
David Roberts 4David Roberts 4
Now it's saying: RetailerLicencesControllerTest.test System.LimitException: Apex CPU time limit exceeded
says at  line
for (licence__c retlic : theirlicences){
}

My work around SOQL limits was to query first and then loop compare. Seems I've hit another limit.

Slightly worrying other diagnostic:
  Number of SOQL queries: 5 out of 100
  Number of query rows: 40567 out of 50000 ******* CLOSE TO LIMIT
and I'm not sure why that is so high.
I have 738 retailers of and 4482 licences to inspect.