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
yearzeroyearzero 

Update related Account Account fields from a Custom Object

Hi everyone, 

I'm a new developer so please bear that in mind! I'm trying to update an Account's custom fields from a Custom Object called Pub_Opportunity__c. The account has a lookup relationship to the pub_opportunity__c. But I only want the Account to update once with these fields as sometimes there are many pub opportunities related to one Account. I just want to retain the original values. 

When I use the code below, it keeps updating the Account every single time with the new Pub Opportunity's fields regardless of the condition. For example, when I change the Account_Status__c field on the Account to 'Active', when I create a new Pub Opportunity, it still updates the Account_Status__c to 'Closed Won - Implementing'. Even when the Link_to_Contract__c field on the Account is filled out, it still updates the field with the new value from the Pub Opportunity. 

Would appreciate your help!


public static void updatePubs(Map<Id,Pub_Opportunity__c> pubOppMap){
        List<Account> aList = new List<Account>();
        
        for(Pub_Opportunity__c p : pubOppMap.values()){
            Account a = new Account();
            if(a.Account_Status__c != 'Active'){
                a.Account_Status__c = 'Closed Won - Implementing';
                a.id = p.Publisher__c;
               if(String.Isblank(a.Link_to_Contract__c)){
                a.Link_To_Contract__c = p.Link_to_Contract__c;
            }
          
            aList.add(a);
        }
        
        if(aList.size() > 0){
            update alist;
        }
    }
    }
    
Best Answer chosen by yearzero
v varaprasadv varaprasad
Change MAp like below.


Map<id,Account> accMap = new map<id,account>([slect id,Account_Status__c,Link_to_Contract__c from account where id in :  accountIds]);

All Answers

v varaprasadv varaprasad
Hi,

Try This: 
public static void updatePubs(Map<Id,Pub_Opportunity__c> pubOppMap){
       
        set<id> accountIds = new set<id>();
        for(Pub_Opportunity__c p : pubOppMap.values()){
            //Add here more conditions means based which fields you want to update
			//example i am taking here Link_to_Contract__c
			
			if(p.Link_to_Contract__c != null && p.Publisher__c != null){
			   accountIds.add( p.Publisher__c);
			}           
         }
           Map<id,Account> accMap = new map<id,account>[slect id,Account_Status__c,Link_to_Contract__c from account where id in :  accountIds];
		  
        for(Pub_Opportunity__c p : pubOppMap.values()){
		    if(accMap.containskey(p.Publisher__c) && accMap.get(p.Publisher__c).Account_Status__c != 'active'){

			accMap.get(p.Publisher__c).Account_Status__c = 'Closed Won - Implementing';
            accMap.get(p.Publisher__c).Link_To_Contract__c = p.Link_to_Contract__c;	
			
			
			}
		}
		
		update accMap.values();
		
      }


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1



 
yearzeroyearzero
Thanks for the quick reply. I'm getting two problems both in the line of code where it says: 

Map<Id,Account> accMap = new map<Id,Account> [SELECT Id, Account_Status__c, Link_to_Contract__c from Account where Id in : accountIds];

The problems are: 
1) Unexpected token '[' . 
2) Expression cannot be a statement. 

I did change the word to Select from the original code as I assumed there was a spelling error. 
v varaprasadv varaprasad
Change MAp like below.


Map<id,Account> accMap = new map<id,account>([slect id,Account_Status__c,Link_to_Contract__c from account where id in :  accountIds]);
This was selected as the best answer
yearzeroyearzero
Amazing that worked - thank you very much for your help! :)