0

I just want to see if I am going about this the right way. Here is my code:

trigger WebToCaseCountry on Case (after insert)
{

    Set<Id> contactIds = new Set<Id>();
    for(Case c : Trigger.new)
    {
         contactIds.add(c.ContactId);
    }

    //Update Contacts
    Map<ID, Contact> mapContacts = new Map<ID, Contact>([SELECT Id FROM Contact where Id IN :contactIds]);
    System.debug(mapContacts);
    List<Contact> contactToUpdate = new List<Contact>();
    For(Case caseOb : Trigger.new)
    {
        Contact cont = mapContacts.get(caseOb.ContactId);
        cont.Country__c = caseOb.CountryWebFormText__c;
        contactToUpdate.add(cont);
    }

    update contactToUpdate;

    Map<Id, Contact> mapUpdatedContacts = new Map<Id,Contact>();
    For(Contact cons : contactToUpdate)
    {
        mapUpdatedContacts.put(cons.Id,cons);
    }

    //Update Cases
    List<Case>  caseList = [SELECT Id, CountryWebFormText__c, Newsletter__c, Country_of_Origin__c  FROM Case];
    List<Area__c> areaList = [SELECT Name, Id From Area__c];
    for(Case caseObj : caseList)
    {
        for(Area__c a : areaList)
        {
            if(caseObj.Newsletter__c == TRUE && caseObj.CountryWebFormText__c != NULL && a.Name.equals(caseObj.CountryWebFormText__c))
            {
                caseObj.Country_of_Origin__c.equals(a.Name);
            }
        }
    }

    update caseList;

}

What it is supposed to do is update the Case country field and the Contact(that is related to that particular Case) country field if text entered in a country field matches any of Area__c's list of countries.

  • While you update caseList, You need to perform an action in if statement. I am talking about this code: if(caseObj.Newsletter__c == TRUE && caseObj.CountryWebFormText__c != NULL && a.Name.equals(caseObj.CountryWebFormText__c)) { caseObj.Country_of_Origin__c.equals(a.Name); } – JavaAster Aug 18 '15 at 16:18
  • What are you trying to do with equal statement inside if blocks? – JavaAster Aug 18 '15 at 16:20
  • @JavaAster I should probably change that to '=' right? – Nik Aug 18 '15 at 16:24
  • n.b. since you are using for loops, the loop variable c, caseOb, caseObj do not need to be different and as such create readability issues; furthermore cons is not particularly evocative of a Contact (singular). Stick to consistent variable names for your sobjects - for example cs for Case and con for Contact (or anything else you are comfortable with). – cropredy Aug 18 '15 at 16:27
  • @Nik Yes to '=' and you should always bulify your code using list rather than iterating your code over loops. Take a note of crop1645's comment. – JavaAster Aug 18 '15 at 16:32
0

You should do following way while updating case:

    List<Case>  caseList = [SELECT Id, CountryWebFormText__c, Newsletter__c, Country_of_Origin__c  FROM Case];
    List<Area__c> areaList = [SELECT Name, Id From Area__c];
    List<Case>  bulkifyList = new List<Case>();
        for(Case caseObj : caseList)
        {
            for(Area__c a : areaList)
            {
                if(caseObj.Newsletter__c == TRUE && caseObj.CountryWebFormText__c != NULL && a.Name.equals(caseObj.CountryWebFormText__c))
                {
                    caseObj.Country_of_Origin__c = a.Name;
                    bulkifyList.add(caseObj);
                }
            }            
        }

        update bulkifyList;
  • Thank you @JavaAster I will keep that in mind from now on. – Nik Aug 19 '15 at 11:31

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.