+ Start a Discussion
Newbie 2019Newbie 2019 

Urgent requirement please help..

The requirement is when I insert  records,if  they meets the criteria records should gets updated else they should be inserted.
I have written a trigger on before insert event but what is happening is I am able to update the records but I am not able to stop them from being inserted. Means with update they are also getting inserted. But on update criteria I only want to update the records that are already in Salesforce.
Best Answer chosen by Newbie 2019
Amnon KruviAmnon Kruvi
This logic should be in whatever page creates the record. It should insert OR update depending on whether a record exists.

All Answers

Amnon KruviAmnon Kruvi
You can't make the insert operation pretend like the records you've updated are the ones that were inserted, so unless you're willing to build an entire custom page, you will have to block the insertion of the record after a successful update. You do that by using the .addError() method on the record that you want to cancel within the trigger context. For example:
myInsertedRecord.addError('The existing record was updated.');
Rama Gaikwad 1Rama Gaikwad 1
Hi  @Newbie,

Your Question is : 
1. The requirement is when I insert  records,if  they meets the criteria records should gets updated else they should be inserted.
I have written a trigger on before insert event but what is happening is I am able to update the records but I am not able to stop them from being inserted.
2. Means with update they are also getting inserted. But on update criteria I only want to update the records that are already in Salesforce.

It means you are not updating records , you are creating two record (with old value and new value), can you check code where you  are iterating  your records and updating its values on the basis of criteria . otherwise can you share you code .

Thanks.

 
Simon RangiSimon Rangi
It means you are not updating records , you are creating two record (with old value and new value), can you check code where you  are iterating  your records and updating (https://movies.t3b.in) its values on the basis of criteria . otherwise can you share you code .
Newbie 2019Newbie 2019
Here is my code


 
-----------------------

trigger chekSrc on Diff_Sources__c (before insert) {
chekSrc.check_Src(trigger.new);
}

-----------------------

public class chekSrc {
    
    public static void check_Src(List<Diff_Sources__c> newData){
        system.debug('----------- what came '+newData);
        List<Diff_Sources__c> updateNewData = new List<Diff_Sources__c>();
        
        
        Map<string,Diff_Sources__c> mapOfOldData = new Map<string,Diff_Sources__c>();
        
        for(Diff_Sources__c ds : [select ExtId__c, name, city__c from Diff_Sources__c]) {
            mapOfOldData.put(ds.ExtId__c, ds);
        }
        
        for(Diff_Sources__c checking : newData) {
            
            if(mapOfOldData.containsKey(checking.ExtId__c) && mapOfOldData.get(checking.ExtId__c).Name == checking.Name ) {
                              
                mapOfOldData.get(checking.ExtId__c).city__c = checking.city__c;
                updateNewData.add(mapOfOldData.get(checking.ExtId__c)); 
                
            }
            else {
               checking.addError('----------- Error ');
                
            }
        }
        update updateNewData;
    }
}


 
Amnon KruviAmnon Kruvi
The code is irrelevant - the problem is the general approach of how you're doing it. You can't get a good result by performing this logic in a trigger - only in the component where the user enters the record details.
Newbie 2019Newbie 2019
@Amnon Kruvi.
I really appreciate Your support.

But in this way also i have to run a trigger and in trigger if i  addError then everything will be roll back. Im not sure about this but i have read somewhere.

 
Amnon KruviAmnon Kruvi
It won't roll back if you use .addError, but it will roll back if you throw an exception. For instance, you can try using .addError here:
if(mapOfOldData.containsKey(checking.ExtId__c) && mapOfOldData.get(checking.ExtId__c).Name == checking.Name ) {
    mapOfOldData.get(checking.ExtId__c).city__c = checking.city__c;
    updateNewData.add(mapOfOldData.get(checking.ExtId__c));
    checking.addError('The original record was updated');
}
Newbie 2019Newbie 2019
public class chekSrc {
    
    public static void check_Src(List<Diff_Sources__c> newData){
        system.debug('----------- what came '+newData);
        List<Diff_Sources__c> updateNewData = new List<Diff_Sources__c>();
        
        
        Map<string,Diff_Sources__c> mapOfOldData = new Map<string,Diff_Sources__c>();
        
        for(Diff_Sources__c ds : [select ExtId__c, name, city__c from Diff_Sources__c]) {
            mapOfOldData.put(ds.ExtId__c, ds);
        }
        
        for(Diff_Sources__c checking : newData) {
            
            if(mapOfOldData.containsKey(checking.ExtId__c) && mapOfOldData.get(checking.ExtId__c).Name == checking.Name ) {
                mapOfOldData.get(checking.ExtId__c).city__c = checking.city__c;
                updateNewData.add(mapOfOldData.get(checking.ExtId__c));
                checking.addError('The original record was updated');
            }
        }
        update updateNewData;
    }
}

I did this but records are not getting updated nor getting  inserted.
Amnon KruviAmnon Kruvi
Ahhh, I see - it does roll back if we .addError on all the records in the transaction. In that case, the only way I can think of might be to do this asynchronously (in a queueable or future method) and then delete the new record.
Newbie 2019Newbie 2019
I had suggested this thing to my client, that we can delete the one of either updated or  got inserted record in after trigger. But this the thing he is not allowing me to do. So im restricted with deleting the records.
Amnon KruviAmnon Kruvi
Well as I said twice, the trigger method will not work. Trying to bang the head against that wall is a waste of your time.
Newbie 2019Newbie 2019
Then what should i do...?
 
Amnon KruviAmnon Kruvi
This logic should be in whatever page creates the record. It should insert OR update depending on whether a record exists.
This was selected as the best answer
Newbie 2019Newbie 2019
means i have to make a new Lightning component.
is it..?
 
Amnon KruviAmnon Kruvi
It does, yes.
Newbie 2019Newbie 2019
Okay thanks kruvi..
I really appreciate your help..
Thanks a lot.