0

I am new to Apex, and stuck at a very basic thing. I have 3 objects. Contract, Site Opportunity, and Sites. (last 2 with lookup with Contract). Opportunity is above contract. I want to check how many Site Opportunity are there, and i want to copy them to Sites using a button on Opportunity. But my button is entering duplicate records everytime its clicked. Here is the code:

Contract__c OppContract = [select id from contract__c where Ren_Opportunity__c =: RenOpptyId and Con_Stage__c = 'Executed' ]   ; // RenOpptyId is passed on the method

list<ConSite__c> ListCSite = new list<ConSite__c>([select id, Site__c, Deals__r.Contract__c, Deals__r.Contract__r.Opportunity__c, Deals__r.ContractV2__r.Ren_Opportunity__c
                                                from ConSite__c
                                                where Deal__r.Contract__c =:OppContract.id]);
list<Site_Opp__c> SiteOpp = new list<Site_Opp__c>select id, site__c, Opportunity__c from Site_Opp__c where Opportunity__c =: RenOpptyId

List<Site_Opportunity__c> ListSiteOpp =new List<Site_Opportunity__c>();

system.debug('OppContract=' +OppContract);
system.debug('ListCSite=' +ListCSite);


for(ConSite__c CS:ListCSite)
{
    // HERE I WANT TO COMPARE ListCSite.SITE__C != SiteOpp.Site__C, if true then copy the site from Consite and paste it in SiteOpportinty
    if(!string.isEmpty(CS.Deals__r.Contract__r.Ren_Opportunity__c))
    {
        Site_Opportunity__c NewSiteOpp = new Site_Opportunity__c();
        NewSiteOpp.Opportunity__c = CS.Deals__r.Contract__r.Ren_Opportunity__c;
        NewSiteOpp.Site__c = CS.Site__c;

        ListSiteOpp.add(NewSiteOpp);
        system.debug('ListSiteOpp=' +ListSiteOpp);
    }
}

insert ListSiteOpp;
1
 for(ConSite__c CS:ListCSite)
{
    // HERE I WANT TO COMPARE ListCSite.SITE__C != SiteOpp.Site__C, if true then copy the site from Consite and paste it in SiteOpportinty
    if(!string.isEmpty(CS.Deals__r.Contract__r.Ren_Opportunity__c))
    {
        Site_Opportunity__c NewSiteOpp = new Site_Opportunity__c();
        NewSiteOpp.Opportunity__c = CS.Deals__r.Contract__r.Ren_Opportunity__c;
        NewSiteOpp.Site__c = CS.Site__c;

        ListSiteOpp.add(NewSiteOpp);
        system.debug('ListSiteOpp=' +ListSiteOpp);
    }
}

it's your code//. make another loop which is not recommended but for beginning let's try.

for(ConSite__c CS:ListCSite){
    for(Site_Opp__c so: SiteOpp){

        if(!string.isEmpty(CS.Deals__r.Contract__r.Ren_Opportunity__c)){
             so.Opportunity__c = CS.Deals__r.Contract__r.Ren_Opportunity__c;
             if(!string.isEmpty(so.Site__c))
                so.Site__c = CS.Site__c;
        }
    }
 }
 update SiteOpp;

hope it will work, feel free to ask or contact me @ skype or my site

|improve this answer|||||
  • Hi Hasectic,It did work. Thanks a lot man. Just one question. i am using loop inside loop. as u said its not a good practice. and so my question line was "comparing two fields of two lists"...i wanted to avoid loop inside loop. Can u show me the next step of going to map. i saw the site u previously mentioned, and i m going through that. apart from that, do u have any code snippet where i can see similar thing using maps. Also, in your site, hoiw can i ask for maps in salesforce? by leaving commet? mailing in some way? – Abhishek Jul 7 '17 at 11:47
  • i will provide you the code and all later.till then please mark my answer as "accepted". – hasectic saif Jul 7 '17 at 11:52
  • hi abhi, i have code and material for you, learn and ask new things. my code is here click it – hasectic saif Jul 10 '17 at 20:52
  • 1
    Hey @hasectic Saif, thanks a lot man. Actually i thought you have forgotten about it, and i didnt want to bug you by asking again. But you actually took the time to right a post...and such a good post...marvelous...Thanks a ton man. really appreciate it. i learnt a new thing...two thumbs up – Abhishek Jul 14 '17 at 10:31
1

I eye balled this but you should be using Maps otherwise you would of have to do nested for loops, I don't believe you you need that top oppContract query, if you do I believe you should be querying it differently it seems like a waste of a query. I didn't try and compile this so there may be some typos..

So just as a disclaimer, the way your querying your records it was hard for me to visualize your data structure, but what you should take out of this is use Maps ..

Contract__c OppContract = [select id from contract__c where Ren_Opportunity__c =: RenOpptyId and Con_Stage__c = 'Executed' ]   ; // RenOpptyId is passed on the method

Map<ID, ConSite__c> mapOfConSite = new Map<ID, ConSite__c>([select id, Site__c, Deals__r.Contract__c, 
    Deals__r.Contract__r.Opportunity__c, Deals__r.ContractV2__r.Ren_Opportunity__c FROM ConSite__c WHERE Deal__r.Contract__c =: OppContract.id]);

Map<ID, ConSite__c> siteIDToConMap = new Map<ID, ConSite__c>();

for(ID conID : mapOfConSite.keySet)
{
    siteIDToConMap.put(mapOfConSite.get(conID).Site__c, mapOfConSite.get(conID));
}

Map<ID, Site_Opp__c> siteOpp = new Map<ID, Site_Opp__c>([Select id, site__c, Opportunity__c from Site_Opp__c where Opportunity__c =: RenOpptyId];

List<Site_Opportunity__c> listSiteOpp = new List<Site_Opportunity__c>();

for(ID siteID: siteOpp.keySet())
{
   if(mapOfConSite.containsKey(siteID))
   {
        if(mapOfConSite.get(siteID).Deals__r.Contract__r.Ren_Opportunity__c != null)
        {
            Site_Opportunity__c newSiteOpp = new Site_Opportunity__c();
            newSiteOpp.Opportunity__c = mapOfConSite.get(siteID).Deals__r.Contract__r.Ren_Opportunity__c;
            newSiteOpp.Site__c = siteID;

            listSiteOpp.add(newSiteOpp);
        }  
   }
}

insert ListSiteOpp;
|improve this answer|||||
  • Hey EricSSH,Thanks a lot for the reply. I need some help with your code. i want to compare Site__C firld of ConSite__c and Site_Opportunity__c. I.e. copy from ConSite__c and paste it in Site_Opportunity__c. but in the lin: for(ID siteID: siteOpp.keySet()), i am not getting Site__c but id of Site_Opp__c. – Abhishek Jul 7 '17 at 9:42
0

your code seems to be true a little error is there as a naive or dummy developer. but i want to ask one thing who is the lookup of whom. Contract__c ,ConSite__c,Site_Opp__c : tell me the relation, and what is deal__c

yes use of map makes things easier. but if you are beginner you can use list and set, try to use set and contains. please tell me the realtion and what do you want to copy from contract and where should you want to copy it. please make me understand. meanwhile try to learn map , because it's recommended for future coding. let me know then i will solve this problem.

|improve this answer|||||
  • Hey Hasectic, here is the relation. Opportunity-> Contract (lookup). Contract-> Deal__c (lookup). Deal__c -> Site_Opp(lookup). Deal-> ConSite__C (lookup). makes sense? also, can u suggest some sites where i can learn more about maps...i know the basics, but how to implement in coding is at times pain area for me – Abhishek Jul 7 '17 at 10:19
  • Hey Hasectic, here is the relation. Opportunity-> Contract (lookup). Contract-> Deal__c (lookup). Deal__c -> Site_Opp(lookup). Deal-> ConSite__C (lookup). makes sense? I want to copy Site_c from CoSite__c ad paste it in SiteOpp__c. If site__c is already present in SiteOpp__c, then i dont w ant to paste it. also, can u suggest some sites where i can learn more about maps...i know the basics, but how to implement in coding is at times pain area for me – Abhishek Jul 7 '17 at 10:29
  • abhishek i got that, i will send the solution asap. [this link ](iterativelogic.com/working-with-apex-code-maps ) you can take help from there. also go to coderinme and ask for map in salesforce they will provide full tutorial and document free. – hasectic saif Jul 7 '17 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.