2

im having trouble with my webservice when updating my form, when i click update the message service says it has modified the data within salesforce, ive had success doing this abit further up in the form where the prices do update in salesforce but when ive tried to repeat this with the rest of the form it just doesnt update.

static WebService GecoAccountFormOnlyMessage modifyFields(formSubmission mform){
    List<AccountProduct__c> getaccountinfofrompicklist = new List<AccountProduct__c>();
     List<AccountProduct__c> accountinfoselect = [SELECT id,Name, PriceInitial__c, PriceRecurring__c, Project__c FROM AccountProduct__c WHERE Name = 'ACCPROD00000240'];
    for(priceField price : mform.priceFields){

    GecoAccountFormOnlyMessage response = new GecoAccountFormOnlyMessage();

        for(priceField price1 : mform.priceFields){
            for(AccountProduct__c cfd : accountinfoselect){
                system.debug('cfd.priceinitial__c =' + cfd.PriceInitial__c);
                if (price1.PInitial != cfd.PriceInitial__c){

                    cfd.PriceInitial__c = price1.PInitial;
                    system.debug('is this working?');

                }
            }  
            for(AccountProduct__c cfd : accountinfoselect){
                system.debug('cfd.priceRecurring__c =' + cfd.PriceRecurring__c);
                if (price1.PriceRec != cfd.PriceRecurring__c){

                    cfd.PriceRecurring__c = price1.PriceRec;
                    system.debug('is this working?');

            }
            system.debug('outside if statement');
            }
        }  
    }
    upsert accountinfoselect;
    GecoAccountFormOnlyMessage response = new GecoAccountFormOnlyMessage();
    response.returnCode = GecoProductFormConnection.STATUS_OK;
    response.message = 'Data modified successfully';
    return response;
}

the above form updates the both prices no problem and works fine however the below code is where im having problems..

static WebService GecoAccountFormOnlyMessage changeFields(formSubmission formupdate){
    List<FieldData__c> getupCogFieldData = new List<FieldData__c>();
    List<FieldData__c> CogUpdateFieldValues = [SELECT Value__c FROM FieldData__c];
    List<CogFieldDataLink__c> CogFieldValues = [SELECT CogFieldLink__r.FieldID__r.id, FieldData__r.Value__c,CogFieldLink__r.FieldID__r.Name__c FROM CogFieldDataLink__c WHERE AccountProduct__r.name = 'ACCPROD00000240'];
    //system.debug('Running WebService?');
    String debugstr = '';
    for(valueField cog : formupdate.valueFields){

        for(CogFieldDataLink__c pls : CogFieldValues){
            //system.debug('pls.FieldData__r.Value__c =' + pls.FieldData__r.Value__c);
            if (cog.formValue != pls.FieldData__r.Value__c){

                pls.FieldData__r.value__c = cog.formValue;
                //getupCogFieldData.add(pls);

            }               
        }
    }
    upsert CogFieldValues;
    GecoAccountFormOnlyMessage response = new GecoAccountFormOnlyMessage();
    response.returnCode = GecoProductFormConnection.STATUS_OK;
    response.message = 'Data modified successfully';

    return response;
}

any help would be brilliant thanks

2
  • The intention of the logic is a bit hard to follow. You want to update all the Value__c field on FieldData__c records where the value doesn't match the valueField? Commented Apr 11, 2014 at 8:32
  • Hi, yes in salesforce ive got a form which already has inputted data like name: Dan for example and within my external site ive got so it shows what that data is and i can modify it from the external site and it would then change the data in salesforce Commented Apr 11, 2014 at 8:37

1 Answer 1

1

It seems like the most likely issue is trying to update the FieldData__c value via the reference from the CogFieldDataLink__c.

Instead, keep track on the FieldData__c records you want to update and do the DML on them rather than the CogFieldDataLink__c list.

//...

// Careful with the hard-coded name here...
List<CogFieldDataLink__c> CogFieldValues = [SELECT CogFieldLink__r.FieldID__r.id, FieldData__r.Value__c,CogFieldLink__r.FieldID__r.Name__c FROM CogFieldDataLink__c WHERE AccountProduct__r.name = 'ACCPROD00000240'];
//system.debug('Running WebService?');
String debugstr = '';

// Keep a list of records that need to be updated. 
// Assumption is that each FieldData__c will only be added once.
// If not, use a Map from the Id or a Set to keep track of the used Ids.
List<FieldData__c> fieldDataToUpdate = new List<FieldData__c>();

for(valueField cog : formupdate.valueFields){

    // Aside, nested for loops should be avoided as a general rule of thumb.
    for(CogFieldDataLink__c pls : CogFieldValues){
        //system.debug('pls.FieldData__r.Value__c =' + pls.FieldData__r.Value__c);
        if (cog.formValue != pls.FieldData__r.Value__c){

            pls.FieldData__r.value__c = cog.formValue;

            // Queue this record for update
            fieldDataToUpdate.add(pls.FieldData__r);

        }               
    }
}
update fieldDataToUpdate;
//upsert CogFieldValues;

Alternatively, rework your SOQL query pull retrieve primarly from the FieldData__c records.


As an aside, try and avoid nested for loops. In your first example you are looping over every accountinfoselect record twice for every priceField. You could easily do both if tests in the one loop.

Better yet, move one section of data into a Map and then look it up in the remaining for loop.

4
  • Hi, going to try this now, the reason its hardcoded is because im testing the webservices before i move it into our real servers ill tell you how this goes :) Commented Apr 11, 2014 at 8:44
  • Thank you your a lifesaver, been trying to figure this out for quite awhile :) Commented Apr 11, 2014 at 8:54
  • No problem. Glad it helped. I'm still finding the overall logic a bit confusing. Won't the last loop of the formUpdate valueFields overwrite all the FieldData values from the previous loops? It feels like the inner loop is missing something like a guard if condition with an external Id. Commented Apr 11, 2014 at 9:03
  • well it updates the first time, then if i wanted to modify it a second time comes up with error: Before Insert or Upsert list must not have two identically equal elements Commented Apr 11, 2014 at 9:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .