In the below code i made some changes between lines 430 and 463 basically to update an existing record and insert only new records. I have used an upsert in line 463 but now its updating existing records but not inserting new ones. Please can someone help with this issue im having.

Many Thanks in Advance!!

    public void addMembers()
        guestList = new List<Guest_New__c>();
        set<ID> contactIds = new set<ID>();
        for(conWrapper cwrap : conWrapperList){
        map<ID, guest_new__c> contToGuestMap = new map<ID, guest_new__c>();
        for(guest_new__C g : [select contact__c from guest_new__c where contact__c = :contactIds])
            contToGuestMap.put(g.contact__c, g);
        for(conWrapper cwrap : conWrapperList){           
            if(cwrap.checked == true){            
                Guest_New__c objGuest = contToGuestMap.get(cwrap.objContact.Id);
                if(objGuest == null){
                    objGuest = new Guest_New__c();
                    objGuest.Event__c = EventId;


                objGuest.Name = cwrap.Name;
                objGuest.Email__c = cwrap.Email;
                objGuest.FirstName__c = cwrap.fName;
                objGuest.LastName__c = cwrap.lName;
                objGuest.CompanyOrAccount__c = cwrap.objContact.Account.Name;
                objGuest.Patch_Controller1__c = cwrap.Patchcontroller;
                objGuest.Title__c = cwrap.objContact.Title;
                objGuest.Status__c = statusVal;
                objGuest.Contact__c = cwrap.objContact.Id;           
        if(guestList != Null && guestList.size() > 0){
            showMsg = true;
            upsert guestList; 
            messageStr = 'You have successfully added '+ guestList.size() + ' member(s).';
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,messageStr));
            system.debug('guestList >>>>>> '+guestList);

        // logic to map contacts to guest
    }catch(DMLException e){
          //  system.debug('Insertion failed ' + e );
  • I removed your debug statements so the code could be properly formatted. Is there any way you could cut this down a bit? – sfdcfox Jun 29 '15 at 20:55
  • Please trim this down to just the relevant code. You cannot reasonably expect anyone to read through all of this and count line by line until they get to 430, 463, etc. – Adrian Larson Jun 29 '15 at 20:55
  • Normally I would flag this as off topic. Please read up about how to make your post more concise and relevant. – Adrian Larson Jun 29 '15 at 20:56

Off the top of my head, I could think that you're running into an exception, but since you didn't handle the exception properly, your update is silently failing. You're actually telling users that the update was successful and then trying to update those records, so if it fails, the user (and, more importantly, you) have no idea why it didn't work, unless you happen to not lose your debug data somewhere.

At minimum, change your catch block to:

    }catch(DMLException e){    // 466
        ApexPages.addMessages(e);  // 467: was System.debug('insertion failed...');
    }    // 468

Also, if showMsg controls the apex:message/apex:pageMessage block, make sure you set that to true.

Also, flip your success message to after the upsert:

            upsert guestList;    // 459 was 462
            messageStr = 'You have successfully added '+ guestList.size() + ' member(s).';  // 460 was 459
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,messageStr)); // 461 was 460
            system.debug('guestList >>>>>> '+guestList); // 462 was 461

This will prevent an erroneous success message from showing up if you hit a DML error. Also note that you're not rolling back changes, so partial successes might be problematic as well.

Finally, I think there may be a problem with Lines 451 and 453 (were 453 uncommented). Let's take a look:

           objGuest.Contact__c = cwrap.objContact.Id;  // 451

If Contact__c is a master-detail, non-reparentable field, this might cause an error. I don't think it applies in this case, because the value shouldn't be changing, but I wouldn't be writing to it arbitrarily.

//    objGuest.Guest_Address__r.Id = cwrap.objContact.Contact_Address__r.Id;

You almost never want to do this, unless you plan on using an upsert against an external ID value on the related object. This isn't what you're trying to do, so you want to use the more ordinary __c assignment:

objGuest.Guest_Address__c = cwrap.objContact.Contact_Address__c;

You'd get an error for trying to use upsert with a value specified that way.

  • Hi sfdcfox, thank u soo much for ur help.. Ive cut down my code to only necessary area of error , i actually bring contacts to a custom object called event__c and then change these contacts to an other custom object called guest__c with a status associated to the event. now my problem is contacts should be added as guests to multiple events ie)1contact-->Many guests to multiple events. Now contact is getting converted/insert to guest only if it is not part of any event. i want it to convert to guest of an event even if its associated with any other event. Hope Im clear.. Many Thanks. – Abraham Jun 30 '15 at 3:32

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.