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
silpasilpa 

Update Problem on Custom Object

Hi..all
 
I am new to salesforce, plz reply me...thanks in advance
I have the following scenario.....
 
I have a custom object which contains a check box field. Upon selection of this checkbox, i need to update two fields on the Account Object. 
How can i do this?? any ideas. Currently we are using .NET
 
I thought of solving this by using Triggers. Any one plz give me an idea.........
 
Reply at your convienence. Thanks
 
 
IvarguIvargu
Yes you might want to consider using an apex-trigger on the custom object that fires after it has been created and updated. I am not too familiar with VB or C# (for .NET) but the "pseudo"-apex code might look something like this: (Note that this is not actual tested code, just an example to get you started)

Code:
trigger UpdateFields on CustomObjectName__c (after insert, after update) {
    CustomObjectName__c[] newobjects = Trigger.new;
    for( CustomObjectName__c o: newobjects ){
      Account a = [ //insert your soql statement here ];
      a.FieldToUpdate = Newvalue;

      update a;

    }
}

 

mavsmavs

Hi thankyou for your quick response.

I tried  the following but still i am stuck.Can you plz tell me where i am going wrong. Thanks

trigger CustomObject1AccountUpdates on CustomObject__c (after insert, after update) {

CustomObject__c  mc=Trigger.new;

if(mc.W_R==true)  //Here i am checking the condition i.e,if the field in custom object = true

{

    mc.C_A=true; //I am updating this checkbox fiels on the custom object (this is my requirement)

    for( CustomObject__c   o: newobjects ){

      Account a = mc.A_P; // My Custom object contains Account record whose fields i need to update.

      a.Status=XYZ; //updating field on Account

      update a;

 

      Account b = C_P_A_c;

      b.A_C_P= XYZ;

      update b;

    }

}

else

{

    mc.C_A=false;

    for( CustomObject__c  o: newobjects ){

      Account a = mc.A_P;

      a.Status=NO Value;

      update a;

 

      Account b =C_P_A_c;

      b.A_C_P = No Value;

      update b;

}

}

 

silpasilpa
Hello Ivargu
i tried this way. getting the following error...............any ideas.????? would be appriciated
Error: Compile Error: Invalid type: Client_c at line 3 column 7
 
trigger ClientAccountUpdates on Client__c (after insert, after update) {
List<Account> acctList = new List<Account>();
 for( Client_c  o: newobjects ){
if(o.Wholesale_Relationship=='true')
{
    o.Client_Authorization='true';
    Account a = new Account( Id = o.Accounting_Provider);
      a.Status='Wholesale';
    
Account b = new Account( Id = o.client_prospect_account);
b.Client_type = 'Wholesale';
   
     
         acctList.add ( a );
         acctList.add ( b );
}
else
{
    o.Client_Authorization='false';
    Account a = new Account( Id = o.Accounting_Provider);
      a.Status='NoValue';
   
Account b = new Account( Id = o.client_prospect_account);
b.Client_type = 'NoValue';
    
       acctList.add ( a );
         acctList.add ( b );

    }
    update acctList;
    
}
}
TCAdminTCAdmin
Hello silpa,

You only have a single underscore between the name and the lowercase c. It should be Client__c instead of Client_c
silpasilpa

hello i appriciate your idea. Plz reply at your convienence. Thanks

i tried placing two underscores. the trigger is getting compiled but i am getting runtime exception

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ClientAccountUpdates caused an unexpected exception, contact your administrator: ClientAccountUpdates: execution of AfterInsert caused by: System.Exception: Record is read-only: Trigger.ClientAccountUpdates: line 6, column 5

o.Client_Authorization__c=true; (This is a checkbox field in my custom object).
 
According to the requirement it should be automatically set to true , when o.Wholesale_Relationship__c=true(Which is also a checkbox) (this is the condition in the if statement) plz see the above code in my previous post.
 
I guess we cannot write a trigger on Checkbox field right. So i have written code for this in the 'if' block
 
My requirement is, when i save this record the following should occur
 
1.  o.Wholesale_Relationship__c=true     (field on Client__c) Checkbox
2. b.Client_type__c='Wholesale'   (This is a pick list field)
3. a.Status__c='Wholesale' (This is  also a pick list field)
silpasilpa
Excellent!!! I executed and i got the required output.
Thank you for  your hints  and help.........
LosintikfosLosintikfos
Hi silpa,
How did you resolve below error message, i am ending up with the same;

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ClientAccountUpdates caused an unexpected exception, contact your administrator: ClientAccountUpdates: execution of AfterInsert caused by: System.Exception: Record is read-only: Trigger.ClientAccountUpdates: line 6, column 5
mavsmavs

Hello Losintikfos            

Please go through your code again. there might be some issue with your code. still if did not work paste your code. i will have a look.      

osamanosaman

You can use an object to change its own field values using trigger.new, but only in before triggers. In all after triggers,
trigger.new is not saved, so a runtime exception is thrown.